]> git.imager.perl.org - imager.git/blobdiff - palimg.c
- converted t/t022double.t to use Test::More
[imager.git] / palimg.c
index f397807c5baf7f7e117306b985f6a6d6d995cb55..886697b6d51d2a01dade7d0d9da6e82708b41d2c 100644 (file)
--- a/palimg.c
+++ b/palimg.c
@@ -84,9 +84,10 @@ Currently 0 < maxpal <= 256
 */
 i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   i_img_pal_ext *palext;
+  int bytes;
 
   i_clear_error();
-  if (maxpal < 0 || maxpal > 256) {
+  if (maxpal < 1 || maxpal > 256) {
     i_push_error(0, "Maximum of 256 palette entries");
     return NULL;
   }
@@ -98,6 +99,11 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
     i_push_errorf(0, "Channels must be positive and <= %d", MAXCHANNELS);
     return NULL;
   }
+  bytes = sizeof(i_palidx) * x * y;
+  if (bytes / y / sizeof(i_palidx) != x) {
+    i_push_errorf(0, "integer overflow calculating image allocation");
+    return NULL;
+  }
 
   memcpy(im, &IIM_base_8bit_pal, sizeof(i_img));
   palext = mymalloc(sizeof(i_img_pal_ext));
@@ -107,7 +113,7 @@ i_img *i_img_pal_new_low(i_img *im, int x, int y, int channels, int maxpal) {
   palext->last_found = -1;
   im->ext_data = palext;
   i_tags_new(&im->tags);
-  im->bytes = sizeof(i_palidx) * x * y;
+  im->bytes = bytes;
   im->idata = mymalloc(im->bytes);
   im->channels = channels;
   memset(im->idata, 0, im->bytes);
@@ -121,7 +127,12 @@ i_img *i_img_pal_new(int x, int y, int channels, int maxpal) {
   i_img *im;
   mm_log((1, "i_img_pal_new(x %d, y %d, channels %d, maxpal %d)\n", x, y, channels, maxpal));
   im = mymalloc(sizeof(i_img));
-  return i_img_pal_new_low(im, x, y, channels, maxpal);
+  if (!i_img_pal_new_low(im, x, y, channels, maxpal)) {
+    myfree(im);
+    im = NULL;
+  }
+
+  return im;
 }
 
 /*
@@ -159,8 +170,6 @@ The conversion cannot be done for virtual images.
 */
 int i_img_to_rgb_inplace(i_img *im) {
   i_img temp;
-  i_color *pal;
-  int palsize;
 
   if (im->virtual)
     return 0;
@@ -330,7 +339,7 @@ RGB.
 =cut
 */
 static int i_plin_p(i_img *im, int l, int r, int y, i_color *vals) {
-  int ch, count, i;
+  int count, i;
   i_palidx *data;
   i_palidx which;
   if (y >=0 && y < im->ysize && l < im->xsize && l >= 0) {
@@ -528,7 +537,7 @@ static int i_maxcolors_p(i_img *im) {
 =cut
 */
 static int i_setcolors_p(i_img *im, int index, i_color *colors, int count) {
-  if (index >= 0 && count >= 1 && index + count < PALEXT(im)->count) {
+  if (index >= 0 && count >= 1 && index + count <= PALEXT(im)->count) {
     while (count) {
       PALEXT(im)->pal[index++] = *colors++;
       --count;