Skip to content

Commit bb7d62b

Browse files
committed
Fixes #400.
Added a check for a missing color component in the jas_image_chclrspc function.
1 parent 6cb475b commit bb7d62b

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

data/test/other/poc_400.pnm

16 Bytes
Binary file not shown.

src/libjasper/base/jas_image.c

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ static void jas_image_calcbbox2(const jas_image_t *image,
118118
jas_image_coord_t *bry);
119119
static void jas_image_fmtinfo_init(jas_image_fmtinfo_t *fmtinfo);
120120
static void jas_image_fmtinfo_cleanup(jas_image_fmtinfo_t *fmtinfo);
121+
static jas_cmcmptfmt_t* jas_cmcmptfmt_array_create(int n);
122+
static void jas_cmcmptfmt_array_destroy(jas_cmcmptfmt_t* cmptfmts, int n);
121123

122124
/******************************************************************************\
123125
* Create and destroy operations.
@@ -413,6 +415,36 @@ static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt)
413415
jas_free(cmpt);
414416
}
415417

418+
static jas_cmcmptfmt_t* jas_cmcmptfmt_array_create(int n)
419+
{
420+
jas_cmcmptfmt_t* cmptfmts;
421+
JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_create(%d)\n", n);
422+
if (!(cmptfmts = jas_alloc2(n, sizeof(jas_cmcmptfmt_t)))) {
423+
return 0;
424+
}
425+
for (int i = 0; i < n; ++i) {
426+
cmptfmts[i].buf = 0;
427+
}
428+
JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_create(%d) returning %p\n", n,
429+
JAS_CAST(void *, cmptfmts));
430+
return cmptfmts;
431+
}
432+
433+
static void jas_cmcmptfmt_array_destroy(jas_cmcmptfmt_t* cmptfmts, int n)
434+
{
435+
assert(cmptfmts);
436+
assert(n > 0);
437+
JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_destroy(%p, %d)\n",
438+
JAS_CAST(void *, cmptfmts), n);
439+
for (int i = 0; i < n; ++i) {
440+
if (cmptfmts[i].buf) {
441+
jas_free(cmptfmts[i].buf);
442+
}
443+
cmptfmts[i].buf = 0;
444+
}
445+
jas_free(cmptfmts);
446+
}
447+
416448
/******************************************************************************\
417449
* Load and save operations.
418450
\******************************************************************************/
@@ -1588,12 +1620,15 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image,
15881620
jas_cmcmptfmt_t *incmptfmts;
15891621
jas_cmcmptfmt_t *outcmptfmts;
15901622

1623+
assert(image);
1624+
assert(outprof);
1625+
15911626
#if 0
15921627
jas_eprintf("IMAGE\n");
15931628
jas_image_dump(image, stderr);
15941629
#endif
15951630

1596-
if (image->numcmpts_ == 0) {
1631+
if (!jas_image_numcmpts(image)) {
15971632
/*
15981633
can't work with a file with no components;
15991634
continuing would crash because we'd attempt to
@@ -1604,6 +1639,8 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image,
16041639

16051640
outimage = 0;
16061641
xform = 0;
1642+
incmptfmts = 0;
1643+
outcmptfmts = 0;
16071644
if (!(inimage = jas_image_copy(image))) {
16081645
goto error;
16091646
}
@@ -1694,34 +1731,45 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image,
16941731
}
16951732

16961733
inpixmap.numcmpts = numinclrchans;
1697-
if (!(incmptfmts = jas_alloc2(numinclrchans, sizeof(jas_cmcmptfmt_t)))) {
1734+
assert(numinclrchans != 0);
1735+
if (!(incmptfmts = jas_cmcmptfmt_array_create(numinclrchans))) {
16981736
// formerly call to abort()
16991737
goto error;
17001738
}
17011739
inpixmap.cmptfmts = incmptfmts;
17021740
for (unsigned i = 0; i < numinclrchans; ++i) {
17031741
const int j = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(i));
1742+
if (j < 0) {
1743+
jas_logerrorf("missing color component %d\n", i);
1744+
goto error;
1745+
}
17041746
if (!(incmptfmts[i].buf = jas_alloc2(width, sizeof(long)))) {
17051747
goto error;
17061748
}
1749+
assert(j >= 0 && j < jas_image_numcmpts(inimage));
17071750
incmptfmts[i].prec = jas_image_cmptprec(inimage, j);
17081751
incmptfmts[i].sgnd = jas_image_cmptsgnd(inimage, j);
17091752
incmptfmts[i].width = width;
17101753
incmptfmts[i].height = 1;
17111754
}
17121755

17131756
outpixmap.numcmpts = numoutclrchans;
1714-
if (!(outcmptfmts = jas_alloc2(numoutclrchans, sizeof(jas_cmcmptfmt_t)))) {
1757+
if (!(outcmptfmts = jas_cmcmptfmt_array_create(numoutclrchans))) {
17151758
// formerly call to abort()
17161759
goto error;
17171760
}
17181761
outpixmap.cmptfmts = outcmptfmts;
17191762

17201763
for (unsigned i = 0; i < numoutclrchans; ++i) {
17211764
const int j = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(i));
1765+
if (j < 0) {
1766+
jas_logerrorf("missing color component %d\n", i);
1767+
goto error;
1768+
}
17221769
if (!(outcmptfmts[i].buf = jas_alloc2(width, sizeof(long)))) {
17231770
goto error;
17241771
}
1772+
assert(j >= 0 && j < jas_image_numcmpts(outimage));
17251773
outcmptfmts[i].prec = jas_image_cmptprec(outimage, j);
17261774
outcmptfmts[i].sgnd = jas_image_cmptsgnd(outimage, j);
17271775
outcmptfmts[i].width = width;
@@ -1746,14 +1794,8 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image,
17461794
}
17471795
}
17481796

1749-
for (unsigned i = 0; i < numoutclrchans; ++i) {
1750-
jas_free(outcmptfmts[i].buf);
1751-
}
1752-
jas_free(outcmptfmts);
1753-
for (unsigned i = 0; i < numinclrchans; ++i) {
1754-
jas_free(incmptfmts[i].buf);
1755-
}
1756-
jas_free(incmptfmts);
1797+
jas_cmcmptfmt_array_destroy(outcmptfmts, numoutclrchans);
1798+
jas_cmcmptfmt_array_destroy(incmptfmts, numinclrchans);
17571799
jas_cmxform_destroy(xform);
17581800
jas_image_destroy(inimage);
17591801

@@ -1765,6 +1807,14 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image,
17651807
#endif
17661808
return outimage;
17671809
error:
1810+
if (incmptfmts) {
1811+
assert(numinclrchans);
1812+
jas_cmcmptfmt_array_destroy(incmptfmts, numinclrchans);
1813+
}
1814+
if (outcmptfmts) {
1815+
assert(numoutclrchans);
1816+
jas_cmcmptfmt_array_destroy(outcmptfmts, numoutclrchans);
1817+
}
17681818
if (xform) {
17691819
jas_cmxform_destroy(xform);
17701820
}

0 commit comments

Comments
 (0)