[JPWL] imagetotga(): fix read heap buffer overflow if numcomps < 3 (#987)
authorYoung_X <YangX92@hotmail.com>
Fri, 23 Nov 2018 07:58:23 +0000 (15:58 +0800)
committerYoung_X <YangX92@hotmail.com>
Fri, 23 Nov 2018 09:08:57 +0000 (17:08 +0800)
Signed-off-by: Young_X <YangX92@hotmail.com>
src/bin/jpwl/convert.c

index 04ca64ca33818ab5364086ff8a16319c3c5b6032..4f636c175fce408fa30b85cd32181195734e6a1c 100644 (file)
@@ -445,7 +445,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
 {
     int width, height, bpp, x, y;
     opj_bool write_alpha;
-    int i, adjustR, adjustG, adjustB;
+    int i, adjustR, adjustG = 0, adjustB = 0;
     unsigned int alpha_channel;
     float r, g, b, a;
     unsigned char value;
@@ -486,8 +486,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 = y * width;