farbfeld

suckless image format with conversion tools
git clone git://git.suckless.org/farbfeld
Log | Files | Refs | README | LICENSE

commit 3fa775e1dfd5dc38648f88c2f385be1f0fe45605
parent c494437937ee01c6d3eb5b630a15e207aae76c25
Author: Alexander Krotov <ilabdsf@yandex.ru>
Date:   Thu, 19 May 2016 01:21:58 +0300

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).

Diffstat:
Mff2jpg.c | 14++++++++++----
Mff2png.c | 7+++++--
Mff2ppm.c | 14++++++++++----
3 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/ff2jpg.c b/ff2jpg.c @@ -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); @@ -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]); @@ -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 @@ -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 @@ -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); @@ -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]); @@ -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; }