[hackers] [farbfeld] Improve fread error handling in ff2* || Alexander Krotov
commit 3fa775e1dfd5dc38648f88c2f385be1f0fe45605
Author: Alexander Krotov <ilabdsf_AT_yandex.ru>
AuthorDate: Thu May 19 01:21:58 2016 +0300
Commit: FRIGN <dev_AT_frign.de>
CommitDate: Thu May 19 12:55:45 2016 +0200
Improve fread error handling in ff2*
In case of unexpected end of file errno is not set, and strerror(errno)
returns "Success". Caller should distinguish between error and EOF by
calling ferror(3) as described in fread(3).
diff --git a/ff2jpg.c b/ff2jpg.c
index 3da35e2..6f88a23 100644
--- a/ff2jpg.c
+++ b/ff2jpg.c
_AT_@ -80,8 +80,7 @@ main(int argc, char *argv[])
/* header */
if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- return 1;
+ goto readerr;
}
if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
fprintf(stderr, "%s: invalid magic value\n", argv0);
_AT_@ -124,8 +123,7 @@ main(int argc, char *argv[])
/* write rows */
for (i = 0; i < height; ++i) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- return 1;
+ goto readerr;
}
for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
a = ntohs(row[j + 3]);
_AT_@ -142,4 +140,12 @@ main(int argc, char *argv[])
jpeg_destroy_compress(&cinfo);
return 0;
+readerr:
+ if (ferror(stdin)) {
+ fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
+ } else {
+ fprintf(stderr, "%s: unexpected end of file\n", argv0);
+ }
+
+ return 1;
}
diff --git a/ff2png.c b/ff2png.c
index 4304bbb..5731082 100644
--- a/ff2png.c
+++ b/ff2png.c
_AT_@ -81,8 +81,11 @@ main(int argc, char *argv[])
return 0;
readerr:
- fprintf(stderr, "%s: fread: ", argv0);
- perror(NULL);
+ if (ferror(stdin)) {
+ fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
+ } else {
+ fprintf(stderr, "%s: unexpected end of file\n", argv0);
+ }
return 1;
}
diff --git a/ff2ppm.c b/ff2ppm.c
index 880fa5b..2a67eb2 100644
--- a/ff2ppm.c
+++ b/ff2ppm.c
_AT_@ -60,8 +60,7 @@ main(int argc, char *argv[])
/* header */
if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- return 1;
+ goto readerr;
}
if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
fprintf(stderr, "%s: invalid magic value\n", argv0);
_AT_@ -90,8 +89,7 @@ main(int argc, char *argv[])
/* write rows */
for (i = 0; i < height; ++i) {
if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
- return 1;
+ goto readerr;
}
for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
a = ntohs(row[j + 3]);
_AT_@ -108,4 +106,12 @@ main(int argc, char *argv[])
}
return 0;
+readerr:
+ if (ferror(stdin)) {
+ fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
+ } else {
+ fprintf(stderr, "%s: unexpected end of file\n", argv0);
+ }
+
+ return 1;
}
Received on Thu May 19 2016 - 12:56:00 CEST
This archive was generated by hypermail 2.3.0
: Thu May 19 2016 - 13:00:18 CEST