shithub: jbig2

Download patch

ref: 83c67854e5e0fd06045bd5e8fc53f98e1ddb9b76
parent: 9b525fc5d325e559dae1f9fb7d3956b8407df3d5
author: Ralph Giles <giles@ghostscript.com>
date: Wed Apr 15 14:41:10 EDT 2009

Move an eof check out of a while loop where it was never called.

This section was flagged by coverity. Not because the eof check was
never executed, but because of casting away the possible EOF return
value of fgetc() inside the while condition. The isdigit() call would
still terminate the while on eof, and the worst that would happen
is that the allocated image would be smaller than intended and the
eof would be caught after trying to read the data, so this was not
a serious issue.

This commit removes the meaningless eof check inside the digit-copying
while loop and adds a specific check before. It also avoids copying the
first non-digit character to the parse buffer, which should resolve the
coverity warning.

--- a/jbig2_image_pbm.c
+++ b/jbig2_image_pbm.c
@@ -1,7 +1,7 @@
 /*
     jbig2dec
 
-    Copyright (C) 2002 Artifex Software, Inc.
+    Copyright (C) 2009 Artifex Software, Inc.
 
     This software is distributed under license and may not
     be copied, modified or distributed except as expressly
@@ -108,13 +108,19 @@
             while ((c = fgetc(in)) != '\n');
             continue;
         }
+        /* report unexpected eof */
+        if (c == EOF) {
+           fprintf(stderr, "end-of-file parsing pbm header\n");
+           return NULL;
+        }
         if (isdigit(c)) {
             buf[i++] = c;
-            while (isdigit(buf[i++] = fgetc(in))) {
-                if (feof(in) || i >= 32) {
+            while (isdigit(c = fgetc(in))) {
+                if (i >= 32) {
                     fprintf(stderr, "pbm parsing error\n");
                     return NULL;
                 }
+                buf[i++] = c;
             }
             buf[i] = '\0';
             sscanf(buf, "%d", &dim[done]);