Avoid overflow warnings in test/unit/safety_check.

This commit is contained in:
Qi Wang 2022-01-26 18:40:49 -08:00 committed by Qi Wang
parent 8c59c44ffa
commit 20f9802e4f

View File

@ -13,6 +13,13 @@ void fake_abort(const char *message) {
fake_abort_called = true; fake_abort_called = true;
} }
static void
buffer_overflow_write(char *ptr, size_t size) {
/* Avoid overflow warnings. */
volatile size_t idx = size;
ptr[idx] = 0;
}
TEST_BEGIN(test_malloc_free_overflow) { TEST_BEGIN(test_malloc_free_overflow) {
test_skip_if(!config_prof); test_skip_if(!config_prof);
test_skip_if(!config_opt_safety_checks); test_skip_if(!config_opt_safety_checks);
@ -20,7 +27,7 @@ TEST_BEGIN(test_malloc_free_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = malloc(128); char* ptr = malloc(128);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
free(ptr); free(ptr);
safety_check_set_abort(NULL); safety_check_set_abort(NULL);
@ -36,7 +43,7 @@ TEST_BEGIN(test_mallocx_dallocx_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = mallocx(128, 0); char* ptr = mallocx(128, 0);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
dallocx(ptr, 0); dallocx(ptr, 0);
safety_check_set_abort(NULL); safety_check_set_abort(NULL);
@ -52,7 +59,7 @@ TEST_BEGIN(test_malloc_sdallocx_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = malloc(128); char* ptr = malloc(128);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
sdallocx(ptr, 128, 0); sdallocx(ptr, 128, 0);
safety_check_set_abort(NULL); safety_check_set_abort(NULL);
@ -68,7 +75,7 @@ TEST_BEGIN(test_realloc_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = malloc(128); char* ptr = malloc(128);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
ptr = realloc(ptr, 129); ptr = realloc(ptr, 129);
safety_check_set_abort(NULL); safety_check_set_abort(NULL);
free(ptr); free(ptr);
@ -85,7 +92,7 @@ TEST_BEGIN(test_rallocx_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = malloc(128); char* ptr = malloc(128);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
ptr = rallocx(ptr, 129, 0); ptr = rallocx(ptr, 129, 0);
safety_check_set_abort(NULL); safety_check_set_abort(NULL);
free(ptr); free(ptr);
@ -102,7 +109,7 @@ TEST_BEGIN(test_xallocx_overflow) {
safety_check_set_abort(&fake_abort); safety_check_set_abort(&fake_abort);
/* Buffer overflow! */ /* Buffer overflow! */
char* ptr = malloc(128); char* ptr = malloc(128);
ptr[128] = 0; buffer_overflow_write(ptr, 128);
size_t result = xallocx(ptr, 129, 0, 0); size_t result = xallocx(ptr, 129, 0, 0);
expect_zu_eq(result, 128, ""); expect_zu_eq(result, 128, "");
free(ptr); free(ptr);