summaryrefslogtreecommitdiffstats
path: root/benchmark/dict_bench.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-10-27 17:19:36 -0400
committerDavid Robillard <d@drobilla.net>2021-10-27 17:19:36 -0400
commit24f60b6d827ca71350493c59b829ac3d0c6ced6a (patch)
treed8b65d1ae6fcc4a9c6d6fdf87950baa676319765 /benchmark/dict_bench.c
parenta1e11f68467c541a99179190a12af372bb97d71a (diff)
downloadzix-24f60b6d827ca71350493c59b829ac3d0c6ced6a.tar.gz
zix-24f60b6d827ca71350493c59b829ac3d0c6ced6a.tar.bz2
zix-24f60b6d827ca71350493c59b829ac3d0c6ced6a.zip
Gracefully handle realloc failure in benchmark
Not really a relevant case here, but it resolves issues found by cppcheck.
Diffstat (limited to 'benchmark/dict_bench.c')
-rw-r--r--benchmark/dict_bench.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/benchmark/dict_bench.c b/benchmark/dict_bench.c
index 5902de9..a5d17a4 100644
--- a/benchmark/dict_bench.c
+++ b/benchmark/dict_bench.c
@@ -73,8 +73,15 @@ run(FILE* const fd)
continue;
}
- chunks = (ZixChunk*)realloc(chunks, (n_chunks + 1) * sizeof(ZixChunk));
+ ZixChunk* const new_chunks =
+ (ZixChunk*)realloc(chunks, (n_chunks + 1) * sizeof(ZixChunk));
+ if (!new_chunks) {
+ free(chunks);
+ return 1;
+ }
+
+ chunks = new_chunks;
chunks[n_chunks].buf = (char*)malloc(buf_len);
chunks[n_chunks].len = this_str_len;
memcpy(chunks[n_chunks].buf, buf, buf_len);
@@ -86,7 +93,14 @@ run(FILE* const fd)
++this_str_len;
if (buf_len < this_str_len + 1) {
buf_len = this_str_len + 1;
- buf = (char*)realloc(buf, buf_len);
+
+ char* const new_buf = (char*)realloc(buf, buf_len);
+ if (!new_buf) {
+ free(buf);
+ return 1;
+ }
+
+ buf = new_buf;
}
buf[this_str_len - 1] = (char)c;
buf[this_str_len] = '\0';