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:
@@ -32,7 +32,7 @@ test_writesame10_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_writesame10_check(void)
|
||||
logging(LOG_VERBOSE, LOG_BLANK_LINE);
|
||||
logging(LOG_VERBOSE, "Test WRITESAME10 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_writesame10_check(void)
|
||||
|
||||
CU_ASSERT_EQUAL(0, memcmp(read_buf, scratch, i));
|
||||
}
|
||||
|
||||
free(read_buf);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user