about summary refs log tree commit diff
path: root/patches/jasper-sanitized-bmp.patch
blob: d02ee91e34cb051601d5aad19ea44df00e4684be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
commit 8f62b4761711d036fd8964df256b938c809b7fca
Author: Michael Adams <mdadams@ece.uvic.ca>
Date:   2016-10-15 20:32:23 -0700

    Fixed a sanitizer failure in the BMP codec.
    Also, added a --debug-level command line option to the imginfo command
    for debugging purposes.

diff --git a/src/appl/imginfo.c b/src/appl/imginfo.c
index 8af19e98aed7..f458a9770e55 100644
--- a/src/appl/imginfo.c
+++ b/src/appl/imginfo.c
@@ -85,7 +85,8 @@ typedef enum {
 	OPT_HELP,
 	OPT_VERSION,
 	OPT_VERBOSE,
-	OPT_INFILE
+	OPT_INFILE,
+	OPT_DEBUG
 } optid_t;
 
 /******************************************************************************\
@@ -104,6 +105,7 @@ static jas_opt_t opts[] = {
 	{OPT_VERSION, "version", 0},
 	{OPT_VERBOSE, "verbose", 0},
 	{OPT_INFILE, "f", JAS_OPT_HASARG},
+	{OPT_DEBUG, "debug-level", JAS_OPT_HASARG},
 	{-1, 0, 0}
 };
 
@@ -126,6 +128,7 @@ int main(int argc, char **argv)
 	int numcmpts;
 	int verbose;
 	char *fmtname;
+	int debug;
 
 	if (jas_init()) {
 		abort();
@@ -135,6 +138,7 @@ int main(int argc, char **argv)
 
 	infile = 0;
 	verbose = 0;
+	debug = 0;
 
 	/* Parse the command line options. */
 	while ((id = jas_getopt(argc, argv, opts)) >= 0) {
@@ -146,6 +150,9 @@ int main(int argc, char **argv)
 			printf("%s\n", JAS_VERSION);
 			exit(EXIT_SUCCESS);
 			break;
+		case OPT_DEBUG:
+			debug = atoi(jas_optarg);
+			break;
 		case OPT_INFILE:
 			infile = jas_optarg;
 			break;
@@ -156,6 +163,8 @@ int main(int argc, char **argv)
 		}
 	}
 
+	jas_setdbglevel(debug);
+
 	/* Open the image file. */
 	if (infile) {
 		/* The image is to be read from a file. */
@@ -177,6 +186,7 @@ int main(int argc, char **argv)
 
 	/* Decode the image. */
 	if (!(image = jas_image_decode(instream, fmtid, 0))) {
+		jas_stream_close(instream);
 		fprintf(stderr, "cannot load image\n");
 		return EXIT_FAILURE;
 	}
diff --git a/src/libjasper/bmp/bmp_dec.c b/src/libjasper/bmp/bmp_dec.c
index 7a6dcb157483..6e7d8802cc7b 100644
--- a/src/libjasper/bmp/bmp_dec.c
+++ b/src/libjasper/bmp/bmp_dec.c
@@ -77,6 +77,7 @@
 #include "jasper/jas_stream.h"
 #include "jasper/jas_image.h"
 #include "jasper/jas_malloc.h"
+#include "jasper/jas_debug.h"
 
 #include "bmp_cod.h"
 
@@ -122,12 +123,22 @@ jas_image_t *bmp_decode(jas_stream_t *in, char *optstr)
 		jas_eprintf("cannot get header\n");
 		return 0;
 	}
+	JAS_DBGLOG(1, (
+	  "BMP header: magic 0x%x; siz %d; res1 %d; res2 %d; off %d\n",
+	  hdr.magic, hdr.siz, hdr.reserved1, hdr.reserved2, hdr.off
+	  ));
 
 	/* Read the bitmap information. */
 	if (!(info = bmp_getinfo(in))) {
 		jas_eprintf("cannot get info\n");
 		return 0;
 	}
+	JAS_DBGLOG(1,
+	  ("BMP information: len %d; width %d; height %d; numplanes %d; "
+	  "depth %d; enctype %d; siz %d; hres %d; vres %d; numcolors %d; "
+	  "mincolors %d\n", info->len, info->width, info->height, info->numplanes,
+	  info->depth, info->enctype, info->siz, info->hres, info->vres,
+	  info->numcolors, info->mincolors));
 
 	/* Ensure that we support this type of BMP file. */
 	if (!bmp_issupported(&hdr, info)) {
@@ -440,7 +451,7 @@ static int bmp_getint32(jas_stream_t *in, int_fast32_t *val)
 		if ((c = jas_stream_getc(in)) == EOF) {
 			return -1;
 		}
-		v |= (c << 24);
+		v |= (JAS_CAST(uint_fast32_t, c) << 24);
 		if (--n <= 0) {
 			break;
 		}