Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failures more consistently via clar_abort() #101

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clar/summary: convert to use clar_abort()
When initializing a summary fails we first call `perror()` followed by
`clar_abort()`. This is somewhat broken because the second function call
will always claim that opening the file has failed, even though the
failure may actually be that we are out of memory.

Fix this by using `clar_abort()` in `clar_summary_init()` directly,
which gives direct access to the failures' root cause. Callers do not
need to check for errors anymore.
pks-t committed Sep 20, 2024

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit 3a9a6c6a9281e5d04e030d4de23691c1d589eb82
6 changes: 2 additions & 4 deletions clar.c
Original file line number Diff line number Diff line change
@@ -567,10 +567,8 @@ clar_test_init(int argc, char **argv)
if (_clar.write_summary && !_clar.summary_filename)
_clar.summary_filename = strdup("summary.xml");

if (_clar.write_summary &&
!(_clar.summary = clar_summary_init(_clar.summary_filename)))
clar_abort("Failed to open the summary file '%s': %s.\n",
_clar.summary_filename, strerror(errno));
if (_clar.write_summary)
_clar.summary = clar_summary_init(_clar.summary_filename);

if (clar_sandbox() < 0)
clar_abort("Failed to sandbox the test runner.\n");
14 changes: 5 additions & 9 deletions clar/summary.h
Original file line number Diff line number Diff line change
@@ -66,16 +66,12 @@ struct clar_summary *clar_summary_init(const char *filename)
struct clar_summary *summary;
FILE *fp;

if ((fp = fopen(filename, "w")) == NULL) {
perror("fopen");
return NULL;
}
if ((fp = fopen(filename, "w")) == NULL)
clar_abort("Failed to open the summary file '%s': %s.\n",
filename, strerror(errno));

if ((summary = malloc(sizeof(struct clar_summary))) == NULL) {
perror("malloc");
fclose(fp);
return NULL;
}
if ((summary = malloc(sizeof(struct clar_summary))) == NULL)
clar_abort("Failed to allocate summary.\n");

summary->filename = filename;
summary->fp = fp;