Skip to content

Commit 411a406

Browse files
committed
Fixed a few bugs in the RAS encoder and decoder where errors were tested
with assertions instead of being gracefully handled.
1 parent c62014d commit 411a406

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

Diff for: src/libjasper/ras/ras_dec.c

+24-6
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,16 @@ static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
260260
/* Avoid compiler warnings about unused parameters. */
261261
cmap = 0;
262262

263+
assert(jas_image_numcmpts(image) <= 3);
264+
265+
for (i = 0; i < 3; ++i) {
266+
data[i] = 0;
267+
}
268+
263269
for (i = 0; i < jas_image_numcmpts(image); ++i) {
264-
data[i] = jas_matrix_create(1, jas_image_width(image));
265-
assert(data[i]);
270+
if (!(data[i] = jas_matrix_create(1, jas_image_width(image)))) {
271+
goto error;
272+
}
266273
}
267274

268275
pad = RAS_ROWSIZE(hdr) - (hdr->width * hdr->depth + 7) / 8;
@@ -273,7 +280,7 @@ static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
273280
for (x = 0; x < hdr->width; x++) {
274281
while (nz < hdr->depth) {
275282
if ((c = jas_stream_getc(in)) == EOF) {
276-
return -1;
283+
goto error;
277284
}
278285
z = (z << 8) | c;
279286
nz += 8;
@@ -293,22 +300,31 @@ static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
293300
}
294301
if (pad) {
295302
if ((c = jas_stream_getc(in)) == EOF) {
296-
return -1;
303+
goto error;
297304
}
298305
}
299306
for (i = 0; i < jas_image_numcmpts(image); ++i) {
300307
if (jas_image_writecmpt(image, i, 0, y, hdr->width, 1,
301308
data[i])) {
302-
return -1;
309+
goto error;
303310
}
304311
}
305312
}
306313

307314
for (i = 0; i < jas_image_numcmpts(image); ++i) {
308315
jas_matrix_destroy(data[i]);
316+
data[i] = 0;
309317
}
310318

311319
return 0;
320+
321+
error:
322+
for (i = 0; i < 3; ++i) {
323+
if (data[i]) {
324+
jas_matrix_destroy(data[i]);
325+
}
326+
}
327+
return -1;
312328
}
313329

314330
static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap)
@@ -327,7 +343,9 @@ static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap)
327343
{
328344
jas_eprintf("warning: palettized images not fully supported\n");
329345
numcolors = 1 << hdr->depth;
330-
assert(numcolors <= RAS_CMAP_MAXSIZ);
346+
if (numcolors > RAS_CMAP_MAXSIZ) {
347+
return -1;
348+
}
331349
actualnumcolors = hdr->maplength / 3;
332350
for (i = 0; i < numcolors; i++) {
333351
cmap->data[i] = 0;

Diff for: src/libjasper/ras/ras_enc.c

+23-6
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,17 @@ static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image,
230230
jas_matrix_t *data[3];
231231
int i;
232232

233+
assert(numcmpts <= 3);
234+
235+
for (i = 0; i < 3; ++i) {
236+
data[i] = 0;
237+
}
238+
233239
for (i = 0; i < numcmpts; ++i) {
234-
data[i] = jas_matrix_create(jas_image_height(image), jas_image_width(image));
235-
assert(data[i]);
240+
if (!(data[i] = jas_matrix_create(jas_image_height(image),
241+
jas_image_width(image)))) {
242+
goto error;
243+
}
236244
}
237245

238246
rowsize = RAS_ROWSIZE(hdr);
@@ -244,7 +252,7 @@ static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image,
244252
for (i = 0; i < numcmpts; ++i) {
245253
if (jas_image_readcmpt(image, cmpts[i], 0, y,
246254
jas_image_width(image), 1, data[i])) {
247-
return -1;
255+
goto error;
248256
}
249257
}
250258
z = 0;
@@ -263,7 +271,7 @@ static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image,
263271
while (nz >= 8) {
264272
c = (z >> (nz - 8)) & 0xff;
265273
if (jas_stream_putc(out, c) == EOF) {
266-
return -1;
274+
goto error;
267275
}
268276
nz -= 8;
269277
z &= RAS_ONES(nz);
@@ -272,21 +280,30 @@ static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image,
272280
if (nz > 0) {
273281
c = (z >> (8 - nz)) & RAS_ONES(nz);
274282
if (jas_stream_putc(out, c) == EOF) {
275-
return -1;
283+
goto error;
276284
}
277285
}
278286
if (pad % 2) {
279287
if (jas_stream_putc(out, 0) == EOF) {
280-
return -1;
288+
goto error;
281289
}
282290
}
283291
}
284292

285293
for (i = 0; i < numcmpts; ++i) {
286294
jas_matrix_destroy(data[i]);
295+
data[i] = 0;
287296
}
288297

289298
return 0;
299+
300+
error:
301+
for (i = 0; i < numcmpts; ++i) {
302+
if (data[i]) {
303+
jas_matrix_destroy(data[i]);
304+
}
305+
}
306+
return -1;
290307
}
291308

292309
static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr)

0 commit comments

Comments
 (0)