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

View File

@@ -62,7 +62,7 @@ test_async_write(void)
int blocks_per_io = 8;
int num_ios = 1000;
/* IOs in flight concurrently, but all using the same src buffer */
unsigned char buf[block_size * blocks_per_io];
unsigned char *buf;
CHECK_FOR_DATALOSS;
CHECK_FOR_SBC;
@@ -74,7 +74,10 @@ test_async_write(void)
return;
}
memset(buf, 0, block_size * blocks_per_io);
buf = calloc(block_size, blocks_per_io);
CU_ASSERT(buf != NULL);
if (!buf)
return;
for (i = 0; i < num_ios; i++) {
uint32_t lba = i * blocks_per_io;
@@ -111,4 +114,6 @@ test_async_write(void)
ret = iscsi_service(sd->iscsi_ctx, pfd.revents);
CU_ASSERT_EQUAL(ret, 0);
}
free(buf);
}