[hackers] [farbfeld] [PATCH] Improve fread error handling in ff2*

From: Alexander Krotov <ilabdsf_AT_yandex.ru>
Date: Thu, 19 May 2016 01:21:58 +0300

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).
---
 ff2jpg.c | 14 ++++++++++----
 ff2png.c |  7 +++++--
 ff2ppm.c | 14 ++++++++++----
 3 files changed, 25 insertions(+), 10 deletions(-)
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;
 }
-- 
2.8.1
Received on Thu May 19 2016 - 00:21:58 CEST

This archive was generated by hypermail 2.3.0 : Thu May 19 2016 - 00:24:15 CEST