imagetotga(): fix read heap buffer overflow if numcomps < 3 (#987)
authorEven Rouault <even.rouault@spatialys.com>
Wed, 16 Aug 2017 11:11:36 +0000 (13:11 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Wed, 16 Aug 2017 11:11:36 +0000 (13:11 +0200)
src/bin/jp2/convert.c

index e2e16027786e5c8f257398dd0060c700c93d4ba4..a4eb81f6a830e03de16f405d2e375a4dbfbcf6b4 100644 (file)
@@ -941,7 +941,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
     int width, height, bpp, x, y;
     OPJ_BOOL write_alpha;
     unsigned int i;
-    int adjustR, adjustG, adjustB, fails;
+    int adjustR, adjustG = 0, adjustB = 0, fails;
     unsigned int alpha_channel;
     float r, g, b, a;
     unsigned char value;
@@ -986,8 +986,10 @@ int imagetotga(opj_image_t * image, const char *outfile)
     scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
 
     adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
-    adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
-    adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    if (image->numcomps >= 3) {
+        adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
+        adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    }
 
     for (y = 0; y < height; y++) {
         unsigned int index = (unsigned int)(y * width);