Skip to content

Commit edbbccc

Browse files
mdouchametan-ucw
authored andcommitted
Unify error handling in include/tst_safe_macros.h
- Pedantically check invalid syscall return values Signed-off-by: Martin Doucha <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent b967dcf commit edbbccc

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

include/tst_safe_macros.h

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,14 @@ static inline int safe_ftruncate(const char *file, const int lineno,
256256
int rval;
257257

258258
rval = ftruncate(fd, length);
259+
259260
if (rval == -1) {
260261
tst_brk_(file, lineno, TBROK | TERRNO,
261-
"ftruncate(%d,%ld) failed",
262-
fd, (long)length);
262+
"ftruncate(%d,%ld) failed", fd, (long)length);
263+
} else if (rval) {
264+
tst_brk_(file, lineno, TBROK | TERRNO,
265+
"Invalid ftruncate(%d,%ld) return value %d", fd,
266+
(long)length, rval);
263267
}
264268

265269
return rval;
@@ -273,10 +277,14 @@ static inline int safe_truncate(const char *file, const int lineno,
273277
int rval;
274278

275279
rval = truncate(path, length);
280+
276281
if (rval == -1) {
277282
tst_brk_(file, lineno, TBROK | TERRNO,
278-
"truncate(%s,%ld) failed",
279-
path, (long)length);
283+
"truncate(%s,%ld) failed", path, (long)length);
284+
} else if (rval) {
285+
tst_brk_(file, lineno, TBROK | TERRNO,
286+
"Invalid truncate(%s,%ld) return value %d", path,
287+
(long)length, rval);
280288
}
281289

282290
return rval;
@@ -293,7 +301,11 @@ static inline int safe_stat(const char *file, const int lineno,
293301

294302
if (rval == -1) {
295303
tst_brk_(file, lineno, TBROK | TERRNO,
296-
"stat(%s,%p) failed", path, buf);
304+
"stat(%s,%p) failed", path, buf);
305+
} else if (rval) {
306+
tst_brk_(file, lineno, TBROK | TERRNO,
307+
"Invalid stat(%s,%p) return value %d", path, buf,
308+
rval);
297309
}
298310

299311
return rval;
@@ -311,6 +323,9 @@ static inline int safe_fstat(const char *file, const int lineno,
311323
if (rval == -1) {
312324
tst_brk_(file, lineno, TBROK | TERRNO,
313325
"fstat(%d,%p) failed", fd, buf);
326+
} else if (rval) {
327+
tst_brk_(file, lineno, TBROK | TERRNO,
328+
"Invalid fstat(%d,%p) return value %d", fd, buf, rval);
314329
}
315330

316331
return rval;
@@ -328,6 +343,10 @@ static inline int safe_lstat(const char *file, const int lineno,
328343
if (rval == -1) {
329344
tst_brk_(file, lineno, TBROK | TERRNO,
330345
"lstat(%s,%p) failed", path, buf);
346+
} else if (rval) {
347+
tst_brk_(file, lineno, TBROK | TERRNO,
348+
"Invalid lstat(%s,%p) return value %d", path, buf,
349+
rval);
331350
}
332351

333352
return rval;
@@ -344,7 +363,11 @@ static inline int safe_statfs(const char *file, const int lineno,
344363

345364
if (rval == -1) {
346365
tst_brk_(file, lineno, TBROK | TERRNO,
347-
"statfs(%s,%p) failed", path, buf);
366+
"statfs(%s,%p) failed", path, buf);
367+
} else if (rval) {
368+
tst_brk_(file, lineno, TBROK | TERRNO,
369+
"Invalid statfs(%s,%p) return value %d", path, buf,
370+
rval);
348371
}
349372

350373
return rval;
@@ -361,8 +384,11 @@ static inline off_t safe_lseek(const char *file, const int lineno,
361384

362385
if (rval == (off_t) -1) {
363386
tst_brk_(file, lineno, TBROK | TERRNO,
364-
"lseek(%d,%ld,%d) failed",
365-
fd, (long)offset, whence);
387+
"lseek(%d,%ld,%d) failed", fd, (long)offset, whence);
388+
} else if (rval < 0) {
389+
tst_brk_(file, lineno, TBROK | TERRNO,
390+
"Invalid lseek(%d,%ld,%d) return value %ld", fd,
391+
(long)offset, whence, (long)rval);
366392
}
367393

368394
return rval;
@@ -379,8 +405,11 @@ static inline int safe_getrlimit(const char *file, const int lineno,
379405

380406
if (rval == -1) {
381407
tst_brk_(file, lineno, TBROK | TERRNO,
382-
"getrlimit(%d,%p) failed",
383-
resource, rlim);
408+
"getrlimit(%d,%p) failed", resource, rlim);
409+
} else if (rval) {
410+
tst_brk_(file, lineno, TBROK | TERRNO,
411+
"Invalid getrlimit(%d,%p) return value %d", resource,
412+
rlim, rval);
384413
}
385414

386415
return rval;
@@ -397,8 +426,11 @@ static inline int safe_setrlimit(const char *file, const int lineno,
397426

398427
if (rval == -1) {
399428
tst_brk_(file, lineno, TBROK | TERRNO,
400-
"setrlimit(%d,%p) failed",
401-
resource, rlim);
429+
"setrlimit(%d,%p) failed", resource, rlim);
430+
} else if (rval) {
431+
tst_brk_(file, lineno, TBROK | TERRNO,
432+
"Invalid setrlimit(%d,%p) return value %d", resource,
433+
rlim, rval);
402434
}
403435

404436
return rval;

0 commit comments

Comments
 (0)