Skip to content

Commit 9cae438

Browse files
mdouchametan-ucw
authored andcommitted
Unify error handling in lib/safe_stdio.c
- Properly format caller file:line location - Pedantically check invalid function return values Signed-off-by: Martin Doucha <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent b93568d commit 9cae438

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

lib/safe_stdio.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void),
2929
FILE *f = fopen(path, mode);
3030

3131
if (f == NULL) {
32-
tst_brkm(TBROK | TERRNO, cleanup_fn,
33-
"%s:%d: fopen(%s,%s) failed",
34-
file, lineno, path, mode);
32+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
33+
"fopen(%s,%s) failed", path, mode);
3534
}
3635

3736
return f;
@@ -44,9 +43,12 @@ int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void),
4443

4544
ret = fclose(f);
4645

47-
if (ret) {
48-
tst_brkm(TBROK | TERRNO, cleanup_fn,
49-
"%s:%d: fclose(%p) failed", file, lineno, f);
46+
if (ret == EOF) {
47+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
48+
"fclose(%p) failed", f);
49+
} else if (ret) {
50+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
51+
"Invalid fclose(%p) return value %d", f, ret);
5052
}
5153

5254
return ret;
@@ -62,9 +64,12 @@ int safe_asprintf(const char *file, const int lineno, void (cleanup_fn)(void),
6264
ret = vasprintf(strp, fmt, va);
6365
va_end(va);
6466

65-
if (ret < 0) {
66-
tst_brkm(TBROK | TERRNO, cleanup_fn,
67-
"%s:%d: asprintf(%s,...) failed", file, lineno, fmt);
67+
if (ret == -1) {
68+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
69+
"asprintf(%s,...) failed", fmt);
70+
} else if (ret < 0) {
71+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
72+
"Invalid asprintf(%s,...) return value %d", fmt, ret);
6873
}
6974

7075
return ret;
@@ -81,13 +86,12 @@ FILE *safe_popen(const char *file, const int lineno, void (cleanup_fn)(void),
8186

8287
if (stream == NULL) {
8388
if (errno != 0) {
84-
tst_brkm(TBROK | TERRNO, cleanup_fn,
85-
"%s:%d: popen(%s,%s) failed",
86-
file, lineno, command, type);
89+
tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
90+
"popen(%s,%s) failed", command, type);
8791
} else {
88-
tst_brkm(TBROK, cleanup_fn,
89-
"%s:%d: popen(%s,%s) failed: Out of memory",
90-
file, lineno, command, type);
92+
tst_brkm_(file, lineno, TBROK, cleanup_fn,
93+
"popen(%s,%s) failed: Out of memory",
94+
command, type);
9195
}
9296
}
9397

0 commit comments

Comments
 (0)