Skip to content

Commit 7808a06

Browse files
committed
gif loader: check LZW code size (Issue #75)
1 parent 2df6437 commit 7808a06

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/fromgif.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ typedef struct
5858
unsigned char suffix;
5959
} gif_lzw;
6060

61+
enum {
62+
gif_lzw_max_code_size = 12
63+
};
64+
6165
typedef struct
6266
{
6367
int w, h;
6468
unsigned char *out; /* output buffer (always 4 components) */
6569
int flags, bgindex, ratio, transparent, eflags;
6670
unsigned char pal[256][3];
6771
unsigned char lpal[256][3];
68-
gif_lzw codes[4096];
72+
gif_lzw codes[1 << gif_lzw_max_code_size];
6973
unsigned char *color_table;
7074
int parse, step;
7175
int lflags;
@@ -299,7 +303,15 @@ gif_process_raster(
299303
signed int codesize, codemask, avail, oldcode, bits, valid_bits, clear;
300304
gif_lzw *p;
301305

306+
/* LZW Minimum Code Size */
302307
lzw_cs = gif_get8(s);
308+
if (lzw_cs > gif_lzw_max_code_size) {
309+
sixel_helper_set_additional_message(
310+
"Unsupported GIF (LZW code size)");
311+
status = SIXEL_RUNTIME_ERROR;
312+
goto end;
313+
}
314+
303315
clear = 1 << lzw_cs;
304316
first = 1;
305317
codesize = lzw_cs + 1;
@@ -353,7 +365,7 @@ gif_process_raster(
353365
goto end;
354366
}
355367
if (oldcode >= 0) {
356-
if (avail < 4096) {
368+
if (avail < (1 << gif_lzw_max_code_size)) {
357369
p = &g->codes[avail++];
358370
p->prefix = (signed short) oldcode;
359371
p->first = g->codes[oldcode].first;

0 commit comments

Comments
 (0)