From 800e0d9b35536d190cf2054d485ad3c76989e4a1 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Wed, 3 Jun 2020 21:16:48 +0000 Subject: Fix bugs from previous commit git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3829 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/other/cameratopam/camera.c | 4 +-- converter/other/cameratopam/stdio_nofail.c | 41 +++++++++++++++++++++++++----- converter/other/cameratopam/stdio_nofail.h | 6 +++++ 3 files changed, 42 insertions(+), 9 deletions(-) (limited to 'converter') diff --git a/converter/other/cameratopam/camera.c b/converter/other/cameratopam/camera.c index 3b29d1ac..4610462c 100644 --- a/converter/other/cameratopam/camera.c +++ b/converter/other/cameratopam/camera.c @@ -450,7 +450,7 @@ rollei_load_raw(Image const image) { unsigned iten=0, isix, i, buffer=0, row, col, todo[16]; isix = raw_width * raw_height * 5 / 8; - while (fread_nofail (pixel, 1, 10, ifp) == 10) { + while (fread_or_eof_nofail (pixel, 1, 10, ifp) == 10) { for (i=0; i < 10; i+=2) { todo[i] = iten++; todo[i+1] = pixel[i] << 8 | pixel[i+1]; @@ -784,7 +784,7 @@ fill_input_buffer (j_decompress_ptr cinfo) static char jpeg_buffer[4096]; size_t nbytes; - nbytes = fread_nofail (jpeg_buffer, 1, 4096, ifp); + nbytes = fread_or_eof_nofail (jpeg_buffer, 1, 4096, ifp); swab (jpeg_buffer, jpeg_buffer, nbytes); cinfo->src->next_input_byte = jpeg_buffer; cinfo->src->bytes_in_buffer = nbytes; diff --git a/converter/other/cameratopam/stdio_nofail.c b/converter/other/cameratopam/stdio_nofail.c index 683d068a..97a8796d 100644 --- a/converter/other/cameratopam/stdio_nofail.c +++ b/converter/other/cameratopam/stdio_nofail.c @@ -9,6 +9,26 @@ size_t +fread_or_eof_nofail(void * const ptr, + size_t const size, + size_t const nmemb, + FILE * const streamP) { + + size_t rc; + + rc = fread(ptr, size, nmemb, streamP); + + if (rc < nmemb) { + if (!feof(streamP)) + pm_error("File read failed. Errno=%d (%s)", + errno, strerror(errno)); + } + return rc; +} + + + +void fread_nofail(void * const ptr, size_t const size, size_t const nmemb, @@ -18,10 +38,13 @@ fread_nofail(void * const ptr, rc = fread(ptr, size, nmemb, streamP); - if (rc < 0) - pm_error("File read failed. Errno=%d (%s)", errno, strerror(errno)); - - return rc; + if (rc < nmemb) { + if (feof(streamP)) + pm_error("File read failed. Premature end of file"); + else + pm_error("File read failed. Errno=%d (%s)", + errno, strerror(errno)); + } } @@ -33,9 +56,13 @@ fgetc_nofail(FILE * streamP) { rc = fgetc(streamP); - if (rc < 0) - pm_error("File read failed. Errno=%d (%s)", errno, strerror(errno)); - + if (rc == EOF) { + if (feof(streamP)) + pm_error("File read failed. Premature end of file"); + else + pm_error("File read failed. Errno=%d (%s)", + errno, strerror(errno)); + } return rc; } diff --git a/converter/other/cameratopam/stdio_nofail.h b/converter/other/cameratopam/stdio_nofail.h index f220b4ac..8f45b537 100644 --- a/converter/other/cameratopam/stdio_nofail.h +++ b/converter/other/cameratopam/stdio_nofail.h @@ -1,6 +1,12 @@ #include size_t +fread_or_eof_nofail(void * const ptr, + size_t const size, + size_t const nmemb, + FILE * const streamP); + +void fread_nofail(void * const ptr, size_t const size, size_t const nmemb, -- cgit 1.4.1