Skip to content

Commit

Permalink
Fix #330: Integer overflow in gdImageScaleBilinearPalette()
Browse files Browse the repository at this point in the history
The color components are supposed to be in range 0..255, so we must not
cast them to `signed char`, what can be the default for `char`.
  • Loading branch information
cmb69 committed Oct 10, 2016
1 parent 7ce5aea commit 77c8d35
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/gd_interpolation.c
Expand Up @@ -1267,10 +1267,10 @@ static gdImagePtr gdImageScaleBilinearPalette(gdImagePtr im, const unsigned int
f_a4 = gd_itofx(gdTrueColorGetAlpha(pixel4));

{
const char red = (char) gd_fxtoi(gd_mulfx(f_w1, f_r1) + gd_mulfx(f_w2, f_r2) + gd_mulfx(f_w3, f_r3) + gd_mulfx(f_w4, f_r4));
const char green = (char) gd_fxtoi(gd_mulfx(f_w1, f_g1) + gd_mulfx(f_w2, f_g2) + gd_mulfx(f_w3, f_g3) + gd_mulfx(f_w4, f_g4));
const char blue = (char) gd_fxtoi(gd_mulfx(f_w1, f_b1) + gd_mulfx(f_w2, f_b2) + gd_mulfx(f_w3, f_b3) + gd_mulfx(f_w4, f_b4));
const char alpha = (char) gd_fxtoi(gd_mulfx(f_w1, f_a1) + gd_mulfx(f_w2, f_a2) + gd_mulfx(f_w3, f_a3) + gd_mulfx(f_w4, f_a4));
const unsigned char red = (unsigned char) gd_fxtoi(gd_mulfx(f_w1, f_r1) + gd_mulfx(f_w2, f_r2) + gd_mulfx(f_w3, f_r3) + gd_mulfx(f_w4, f_r4));
const unsigned char green = (unsigned char) gd_fxtoi(gd_mulfx(f_w1, f_g1) + gd_mulfx(f_w2, f_g2) + gd_mulfx(f_w3, f_g3) + gd_mulfx(f_w4, f_g4));
const unsigned char blue = (unsigned char) gd_fxtoi(gd_mulfx(f_w1, f_b1) + gd_mulfx(f_w2, f_b2) + gd_mulfx(f_w3, f_b3) + gd_mulfx(f_w4, f_b4));
const unsigned char alpha = (unsigned char) gd_fxtoi(gd_mulfx(f_w1, f_a1) + gd_mulfx(f_w2, f_a2) + gd_mulfx(f_w3, f_a3) + gd_mulfx(f_w4, f_a4));

new_img->tpixels[dst_offset_v][dst_offset_h] = gdTrueColorAlpha(red, green, blue, alpha);
}
Expand Down
1 change: 1 addition & 0 deletions tests/gdimagescale/.gitignore
@@ -1,2 +1,3 @@
/bug00330
/github_bug_00218
/bug_overflow_large_new_size
1 change: 1 addition & 0 deletions tests/gdimagescale/CMakeLists.txt
@@ -1,4 +1,5 @@
LIST(APPEND TESTS_FILES
bug00330
github_bug_00218
bug_overflow_large_new_size
)
Expand Down
1 change: 1 addition & 0 deletions tests/gdimagescale/Makemodule.am
@@ -1,5 +1,6 @@

libgd_test_programs += \
gdimagescale/bug00330 \
gdimagescale/github_bug_00218 \
gdimagescale/bug_overflow_large_new_size

Expand Down
32 changes: 32 additions & 0 deletions tests/gdimagescale/bug00330.c
@@ -0,0 +1,32 @@
/**
* Regression test for <https://github.com/libgd/libgd/issues/330>.
*
* We're testing that after scaling a palette image, the center pixel actually
* has the expected color value.
*/


#include "gd.h"
#include "gdtest.h"


int main()
{
gdImagePtr src, dst;
int color;

src = gdImageCreate(100, 100);
gdImageColorAllocate(src, 255, 255, 255);

gdImageSetInterpolationMethod(src, GD_BILINEAR_FIXED);
dst = gdImageScale(src, 200, 200);

color = gdImageGetPixel(dst, 99, 99);
gdTestAssertMsg(color == 0xffffff,
"expected color ffffff, but got %x\n", color);

gdImageDestroy(src);
gdImageDestroy(dst);

return 0;
}

0 comments on commit 77c8d35

Please sign in to comment.