Use dynamic memory allocation instead of variable-length arrays

Since it is easy to trigger a stack overflow with variable-length arrays,
use dynamic memory allocation instead of VLAs. Add -Wvla to the compiler
options such that no new VLAs get introduced.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
This commit is contained in:
Bart Van Assche
2019-11-03 14:12:20 -08:00
parent 88f67e8cf8
commit 9d2493248b
9 changed files with 70 additions and 17 deletions
+8 -1
View File
@@ -32,7 +32,7 @@ test_writesame16_check(void)
{
int i;
int ws_max_blocks = 256;
unsigned char read_buf[ws_max_blocks * block_size];
unsigned char *read_buf;
CHECK_FOR_DATALOSS;
CHECK_FOR_SBC;
@@ -40,6 +40,11 @@ test_writesame16_check(void)
logging(LOG_VERBOSE, LOG_BLANK_LINE);
logging(LOG_VERBOSE, "Test WRITESAME16 of 1-256 blocks at the start of the LUN");
read_buf = malloc(ws_max_blocks * block_size);
CU_ASSERT(read_buf != NULL);
if (!read_buf)
return;
for (i = 1; i <= ws_max_blocks; i++) {
/*
* fill the full buffer so that memcmp is straightforward,
@@ -71,4 +76,6 @@ test_writesame16_check(void)
CU_ASSERT_EQUAL(0, memcmp(read_buf, scratch, i));
}
free(read_buf);
}