Skip to content

Commit

Permalink
clar: check for failing calloc() calls
Browse files Browse the repository at this point in the history
We're not consistently checking for failing calls to `calloc()`. Do so
and adapt all callsites to use `clar_abort()`.
  • Loading branch information
pks-t committed Sep 20, 2024
1 parent 3daf348 commit f4979a6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)

_clar.active_test = test[i].name;

report = calloc(1, sizeof(struct clar_report));
if ((report = calloc(1, sizeof(*report))) == NULL)
clar_abort("Failed to allocate report.\n");
report->suite = _clar.active_suite;
report->test = _clar.active_test;
report->test_number = _clar.tests_ran;
Expand Down Expand Up @@ -476,9 +477,10 @@ clar_parse_args(int argc, char **argv)

switch (action) {
case 's': {
struct clar_explicit *explicit =
calloc(1, sizeof(struct clar_explicit));
assert(explicit);
struct clar_explicit *explicit;

if ((explicit = calloc(1, sizeof(*explicit))) == NULL)
clar_abort("Failed to allocate explicit test.\n");

explicit->suite_idx = j;
explicit->filter = argument;
Expand Down Expand Up @@ -661,7 +663,10 @@ void clar__fail(
const char *description,
int should_abort)
{
struct clar_error *error = calloc(1, sizeof(struct clar_error));
struct clar_error *error;

if ((error = calloc(1, sizeof(*error))) == NULL)
clar_abort("Failed to allocate error.\n");

if (_clar.last_report->errors == NULL)
_clar.last_report->errors = error;
Expand Down

0 comments on commit f4979a6

Please sign in to comment.