about summary refs log tree commit diff
path: root/converter/other/bmptopnm.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/other/bmptopnm.c')
-rw-r--r--converter/other/bmptopnm.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/converter/other/bmptopnm.c b/converter/other/bmptopnm.c
index c39b4fd7..54868364 100644
--- a/converter/other/bmptopnm.c
+++ b/converter/other/bmptopnm.c
@@ -695,6 +695,13 @@ bmpReadinfoheader(FILE *                 const ifP,
         *errorP = NULL;
         *bytesReadP = cInfoHeaderSize;
     }
+    /* Part of our anti-arithmetic overflow strategy is to make sure height
+       and width always fit in 16 bits, so they can be multiplied together.
+       This shouldn't be a problem, since they come from 16 bit fields in
+       the BMP info header.
+    */
+    assert(headerP->cols < (1<<16));
+    assert(headerP->rows < (1<<16));
 }
 
 
@@ -1204,6 +1211,9 @@ bmpReadraster(FILE *            const ifP,
         */
     unsigned char ** bmpRaster;
 
+    assert(cols < (1<<16));
+    assert(bytesPerRow < (1<<16));
+
     bmpRaster = allocBmpRaster(rows, bytesPerRow);
 
     *bytesReadP = 0;
@@ -1456,8 +1466,8 @@ readBmp(FILE *               const ifP,
 
 static void
 writeRasterGen(unsigned char **   const bmpRaster,
-               int                const cols, 
-               int                const rows, 
+               unsigned int       const cols, 
+               unsigned int       const rows, 
                int                const format,
                unsigned int       const cBitCount, 
                struct pixelformat const pixelformat,
@@ -1492,8 +1502,8 @@ writeRasterGen(unsigned char **   const bmpRaster,
 
 static void
 writeRasterPbm(unsigned char ** const bmpRaster,
-               int              const cols, 
-               int              const rows, 
+               unsigned int     const cols, 
+               unsigned int     const rows, 
                xel              const colormap[]) {
 /*----------------------------------------------------------------------------
   Write the PBM raster to Standard Output corresponding to the raw BMP
@@ -1510,9 +1520,9 @@ writeRasterPbm(unsigned char ** const bmpRaster,
   
   We destroy *bmpRaster as a side effect.
 -----------------------------------------------------------------------------*/
-    unsigned int const colChars = pbm_packed_bytes(cols);
+    unsigned int const colCharCt = pbm_packed_bytes(cols);
     
-    int row;
+    unsigned int row;
     enum colorFormat {BlackWhite, WhiteBlack};
     enum colorFormat colorformat;
                   
@@ -1521,12 +1531,12 @@ writeRasterPbm(unsigned char ** const bmpRaster,
     else                  
         colorformat = BlackWhite;
         
-    for (row=0; row < rows; ++row){
+    for (row = 0; row < rows; ++row){
         unsigned char * const bitrow = bmpRaster[row]; 
 
         if (colorformat == BlackWhite) {
             unsigned int i;
-            for (i = 0; i < colChars; ++i) 
+            for (i = 0; i < colCharCt; ++i) 
                 bitrow[i] = ~bitrow[i]; /* flip all pixels */ 
         }   
 
@@ -1550,7 +1560,7 @@ main(int argc, const char ** argv) {
            and gray.
         */
     int cols, rows;
-    unsigned char **bmpRaster;
+    unsigned char ** bmpRaster;
         /* The raster part of the BMP image, as a row x column array, with
            each element being a raw byte from the BMP raster.  Note that
            bmpRaster[0] is really Row 0 -- the top row of the image, even