Debian Codegraph @

tests/issue/GHSA-wpx3-h5g8-qr3w/decode-raw-null-palette.c

raw
1 /*
2 * Reproducer for GHSA-wpx3-h5g8-qr3w.
3 *
4 * sixel_decode_raw() used to test the address of the output parameter
5 * after allocating the palette. This allocator fails exactly at the
6 * palette allocation so the vulnerable code continues and writes through
7 * the NULL value stored in palette.
8 */
9
10 #include "config.h"
11
12 #include <stdlib.h>
13
14 #include <sixel.h>
15
16 static int malloc_count;
17
18 static void *
19 fail_palette_malloc(size_t size)
20 {
21 ++malloc_count;
22 if (malloc_count == 3) {
23 return NULL;
24 }
25
26 return malloc(size);
27 }
28
29 int
30 main(void)
31 {
32 static unsigned char const sixel[] = "";
33 sixel_allocator_t *allocator;
34 unsigned char *pixels;
35 unsigned char *palette;
36 SIXELSTATUS status;
37 int width;
38 int height;
39 int ncolors;
40
41 allocator = NULL;
42 pixels = NULL;
43 palette = NULL;
44 width = 0;
45 height = 0;
46 ncolors = 0;
47
48 status = sixel_allocator_new(&allocator,
49 fail_palette_malloc,
50 NULL,
51 NULL,
52 NULL);
53 if (SIXEL_FAILED(status)) {
54 return 2;
55 }
56
57 status = sixel_decode_raw((unsigned char *)sixel,
58 0,
59 &pixels,
60 &width,
61 &height,
62 &palette,
63 &ncolors,
64 allocator);
65
66 sixel_allocator_unref(allocator);
67 free(pixels);
68 free(palette);
69
70 return status == SIXEL_BAD_ALLOCATION ? 0: 1;
71 }