summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Hart <bathterror@gmail.com>2021-10-09 16:40:45 +0100
committerGitHub <noreply@github.com>2021-10-09 11:40:45 -0400
commitba14dec6efe9d87fe80fa1d7bd3d5b0583e1320e (patch)
treebd84e4da42ba0815a19111d01b9b5118a5b17124
parentdceb29a6f5e8b0fb5e21d14c39013efcbb9203ee (diff)
downloadlibgd-ba14dec6efe9d87fe80fa1d7bd3d5b0583e1320e.tar.gz
Fix out of bounds write im->alpha[im->transparent] (#785)
Since #737 gdImageColorTransparent does not correctly handle the case that im->transparent = -1 (which is the initial value and used to indicate no transparent colour has been set). This leads to undefined behaviour via an out-of-bound write: im->alpha[im->transparent] = gdAlphaOpaque; (in practice I assume this merely overwrites an earlier struct member) This can be triggered via loading a gif through gdImageCreateFromGifPtr third_party/gd/source/gd.c:922:2: runtime error: index -1 out of bounds for type 'int [256]' #0 0x5629c034a839 in gdImageColorTransparent third_party/gd/source/gd.c:922:29 #1 0x5629c034ebf0 in gdImageCreateFromGifCtx third_party/gd/source/gd_gif_in.c:328:4 #2 0x5629c034f14f in gdImageCreateFromGifPtr third_party/gd/source/gd_gif_in.c:186:7 Fixes #784.
-rw-r--r--src/gd.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/src/gd.c b/src/gd.c
index 574368c..0be8aad 100644
--- a/src/gd.c
+++ b/src/gd.c
@@ -919,7 +919,9 @@ BGD_DECLARE(void) gdImageColorTransparent (gdImagePtr im, int color)
if (color >= gdMaxColors) {
return;
}
- im->alpha[im->transparent] = gdAlphaOpaque;
+ if (im->transparent != -1) {
+ im->alpha[im->transparent] = gdAlphaOpaque;
+ }
im->alpha[color] = gdAlphaTransparent;
im->transparent = color;
}