shithub: jbig2

Download patch

ref: 40357b4f892a85d407f098c3b855a860db48c62e
parent: f80c59f558ed476a6e6c37c5ca7b95a135b64bcf
author: Sebastian Rasmussen <sebras@gmail.com>
date: Sun Sep 29 08:09:40 EDT 2019

jbig2dec: Address all signedness comparison issues.

--- a/jbig2_arith.c
+++ b/jbig2_arith.c
@@ -31,7 +31,7 @@
 
 struct _Jbig2ArithState {
     uint32_t C;
-    int A;
+    uint32_t A;
 
     int CT;
 
@@ -228,7 +228,7 @@
 
 /* could put bit fields in to minimize memory usage */
 typedef struct {
-    unsigned short Qe;
+    uint16_t Qe;
     byte mps_xor;               /* mps_xor = index ^ NMPS */
     byte lps_xor;               /* lps_xor = index ^ NLPS ^ (SWITCH << 7) */
 } Jbig2ArithQe;
--- a/jbig2_arith_iaid.c
+++ b/jbig2_arith_iaid.c
@@ -37,12 +37,12 @@
 #include "jbig2_arith_iaid.h"
 
 struct _Jbig2ArithIaidCtx {
-    int SBSYMCODELEN;
+    uint8_t SBSYMCODELEN;
     Jbig2ArithCx *IAIDx;
 };
 
 Jbig2ArithIaidCtx *
-jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN)
+jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, uint8_t SBSYMCODELEN)
 {
     Jbig2ArithIaidCtx *result;
     size_t ctx_size;
@@ -80,7 +80,7 @@
 jbig2_arith_iaid_decode(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *actx, Jbig2ArithState *as, int32_t *p_result)
 {
     Jbig2ArithCx *IAIDx = actx->IAIDx;
-    int SBSYMCODELEN = actx->SBSYMCODELEN;
+    uint8_t SBSYMCODELEN = actx->SBSYMCODELEN;
     /* A.3 (1) */
     int PREV = 1;
     int D;
--- a/jbig2_arith_iaid.h
+++ b/jbig2_arith_iaid.h
@@ -22,7 +22,7 @@
 
 typedef struct _Jbig2ArithIaidCtx Jbig2ArithIaidCtx;
 
-Jbig2ArithIaidCtx *jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN);
+Jbig2ArithIaidCtx *jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, uint8_t SBSYMCODELEN);
 
 int jbig2_arith_iaid_decode(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *actx, Jbig2ArithState *as, int32_t *p_result);
 
--- a/jbig2_halftone.c
+++ b/jbig2_halftone.c
@@ -47,8 +47,7 @@
     const uint32_t HPW = params->HDPW;
     const uint32_t HPH = params->HDPH;
     int code;
-    uint32_t i;
-    int j;
+    uint32_t i, j;
 
     if (N == 0) {
         /* We've wrapped. */
@@ -464,8 +463,7 @@
     Jbig2Image *HSKIP = NULL;
     Jbig2PatternDict *HPATS;
     uint32_t i;
-    int32_t mg, ng;
-    int32_t x, y;
+    uint32_t mg, ng;
     uint16_t gray_val;
     int code = 0;
 
@@ -487,10 +485,10 @@
 
         for (mg = 0; mg < params->HGH; ++mg) {
             for (ng = 0; ng < params->HGW; ++ng) {
-                x = (params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
-                y = (params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
+                int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
+                int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
 
-                if (x + HPATS->HPW <= 0 || x >= (int32_t) image->width || y + HPATS->HPH <= 0 || y >= (int32_t) image->height) {
+                if (x + HPATS->HPW <= 0 || x >= image->width || y + HPATS->HPH <= 0 || y >= image->height) {
                     jbig2_image_set_pixel(HSKIP, ng, mg, 1);
                 } else {
                     jbig2_image_set_pixel(HSKIP, ng, mg, 0);
@@ -519,8 +517,8 @@
     /* 6.6.5 point 5. place patterns with procedure mentioned in 6.6.5.2 */
     for (mg = 0; mg < params->HGH; ++mg) {
         for (ng = 0; ng < params->HGW; ++ng) {
-            x = (params->HGX + mg * (int32_t) params->HRY + ng * (int32_t) params->HRX) >> 8;
-            y = (params->HGY + mg * (int32_t) params->HRX - ng * (int32_t) params->HRY) >> 8;
+            int64_t x = ((int64_t) params->HGX + mg * params->HRY + ng * params->HRX) >> 8;
+            int64_t y = ((int64_t) params->HGY + mg * params->HRX - ng * params->HRY) >> 8;
 
             /* prevent pattern index >= HNUMPATS */
             gray_val = GI[ng][mg];
--- a/jbig2_halftone.h
+++ b/jbig2_halftone.h
@@ -23,7 +23,7 @@
 typedef struct {
     int n_patterns;
     Jbig2Image **patterns;
-    int HPW, HPH;
+    uint32_t HPW, HPH;
 } Jbig2PatternDict;
 
 /* Table 24 */
--- a/jbig2_huffman.c
+++ b/jbig2_huffman.c
@@ -2072,13 +2072,13 @@
         return 0;
     }
 
-    for (i = 0; i < countof(tests); i++) {
+    for (i = 0; i < (int) countof(tests); i++) {
         Jbig2HuffmanTable *table;
         Jbig2HuffmanState *hs;
         test_stream_t st;
         int32_t code;
         bool oob;
-        int j;
+        size_t j;
 
         st.ws.get_next_word = test_get_word2;
         st.h = &tests[i];
--- a/jbig2_symbol_dict.c
+++ b/jbig2_symbol_dict.c
@@ -236,7 +236,7 @@
     uint32_t SYMWIDTH, TOTWIDTH;
     uint32_t HCFIRSTSYM;
     uint32_t *SDNEWSYMWIDTHS = NULL;
-    int SBSYMCODELEN = 0;
+    uint8_t SBSYMCODELEN = 0;
     Jbig2WordStream *ws = NULL;
     Jbig2HuffmanState *hs = NULL;
     Jbig2ArithState *as = NULL;
@@ -271,6 +271,8 @@
         return NULL;
     }
 
+    for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < ((uint64_t) params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
+
     if (params->SDHUFF) {
         jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded symbol dictionary");
         hs = jbig2_huffman_new(ctx, ws);
@@ -311,7 +313,6 @@
             jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate symbol bitmap");
             goto cleanup;
         }
-        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < ((uint64_t) params->SDNUMINSYMS + params->SDNUMNEWSYMS); SBSYMCODELEN++);
         tparams.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
         tparams.IARDX = jbig2_arith_int_ctx_new(ctx);
         tparams.IARDY = jbig2_arith_int_ctx_new(ctx);
@@ -432,7 +433,7 @@
                 code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "DW value (%d) would make SYMWIDTH (%u) negative at symbol %u", DW, SYMWIDTH, NSYMSDECODED + 1);
                 goto cleanup;
             }
-            if (DW > 0 && DW > UINT32_MAX - SYMWIDTH) {
+            if (DW > 0 && (uint32_t) DW > UINT32_MAX - SYMWIDTH) {
                 code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "DW value (%d) would make SYMWIDTH (%u) too large at symbol %u", DW, SYMWIDTH, NSYMSDECODED + 1);
                 goto cleanup;
             }
--- a/jbig2_text.c
+++ b/jbig2_text.c
@@ -937,7 +937,8 @@
     }
 
     if (!params.SBHUFF) {
-        uint32_t SBSYMCODELEN, index;
+        uint8_t SBSYMCODELEN;
+        uint32_t index;
         uint32_t SBNUMSYMS = 0;
 
         for (index = 0; index < n_dicts; index++) {
@@ -954,8 +955,8 @@
         }
 
         /* Table 31 */
-        for (SBSYMCODELEN = 0; (1U << SBSYMCODELEN) < SBNUMSYMS; SBSYMCODELEN++) {
-        }
+        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < (uint64_t) SBNUMSYMS; SBSYMCODELEN++);
+
         params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
         params.IARI = jbig2_arith_int_ctx_new(ctx);
         params.IARDW = jbig2_arith_int_ctx_new(ctx);