Skip to content

Commit

Permalink
clar: check for failing strdup() calls
Browse files Browse the repository at this point in the history
We're not checking for failing calls to `strdup()`. Fix this.
  • Loading branch information
pks-t committed Sep 20, 2024
1 parent f4979a6 commit 453aa90
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions clar.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ clar_parse_args(int argc, char **argv)
case 'r':
_clar.write_summary = 1;
free(_clar.summary_filename);
_clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
if (*(argument + 2)) {
if ((_clar.summary_filename = strdup(argument + 2)) == NULL)
clar_abort("Failed to allocate summary filename.\n");
} else {
_clar.summary_filename = NULL;
}
break;

default:
Expand All @@ -563,11 +568,13 @@ clar_test_init(int argc, char **argv)
if (!_clar.summary_filename &&
(summary_env = getenv("CLAR_SUMMARY")) != NULL) {
_clar.write_summary = 1;
_clar.summary_filename = strdup(summary_env);
if ((_clar.summary_filename = strdup(summary_env)) == NULL)
clar_abort("Failed to allocate summary filename.\n");
}

if (_clar.write_summary && !_clar.summary_filename)
_clar.summary_filename = strdup("summary.xml");
if ((_clar.summary_filename = strdup("summary.xml")) == NULL)
clar_abort("Failed to allocate summary filename.\n");

if (_clar.write_summary)
_clar.summary = clar_summary_init(_clar.summary_filename);
Expand Down Expand Up @@ -681,8 +688,9 @@ void clar__fail(
error->line_number = line;
error->error_msg = error_msg;

if (description != NULL)
error->description = strdup(description);
if (description != NULL &&
(error->description = strdup(description)) == NULL)
clar_abort("Failed to allocate description.\n");

_clar.total_errors++;
_clar.last_report->status = CL_TEST_FAILURE;
Expand Down

0 comments on commit 453aa90

Please sign in to comment.