shithub: jbig2

Download patch

ref: caeaa2c6ff5ec7b76e71960dd497ca4c82c136a5
parent: c1621123f28e88e6170d151137f2b88244b4f622
author: Sebastian Rasmussen <sebras@gmail.com>
date: Wed Mar 11 16:56:06 EDT 2020

jbig2dec: Handle errors from reading jbig2 streams.

--- a/jbig2dec.c
+++ b/jbig2dec.c
@@ -635,11 +635,23 @@
         /* pull the whole file/global stream into memory */
         for (;;) {
             int n_bytes = fread(buf, 1, sizeof(buf), f);
-
+            if (n_bytes < 0) {
+                if (f_page != NULL)
+                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to read jbig2 global stream");
+                else
+                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to read jbig2 page stream");
+            }
             if (n_bytes <= 0)
                 break;
-            if (jbig2_data_in(ctx, buf, n_bytes))
+
+            if (jbig2_data_in(ctx, buf, (size_t) n_bytes) < 0)
+            {
+                if (f_page != NULL)
+                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to process jbig2 global stream");
+                else
+                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to process jbig2 page stream");
                 break;
+            }
         }
         fclose(f);
 
@@ -654,11 +666,16 @@
 
                 for (;;) {
                     int n_bytes = fread(buf, 1, sizeof(buf), f_page);
-
+                    if (n_bytes < 0)
+                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to read jbig2 page stream");
                     if (n_bytes <= 0)
                         break;
-                    if (jbig2_data_in(ctx, buf, n_bytes))
+
+                    if (jbig2_data_in(ctx, buf, (size_t) n_bytes) < 0)
+                    {
+                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to process jbig2 page stream");
                         break;
+                    }
                 }
             }
             fclose(f_page);