|
| 1 | +From 61763661b030bfb15bad9de67e8e7b5baf617bf3 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Michael Adams <mdadams@ece.uvic.ca> |
| 3 | +Date: Tue, 29 Jul 2025 20:16:35 -0700 |
| 4 | +Subject: [PATCH] Fixes #400. |
| 5 | + |
| 6 | +Added a check for a missing color component in the jas_image_chclrspc |
| 7 | +function. |
| 8 | + |
| 9 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 10 | +Upstream-reference: https://github.com/jasper-software/jasper/commit/bb7d62bd0a2a8e0e1fdb4d603f3305f955158c52.patch |
| 11 | +--- |
| 12 | + src/libjasper/base/jas_image.c | 72 ++++++++++++++++++++++++++++----- |
| 13 | + 1 files changed, 61 insertions(+), 11 deletions(-) |
| 14 | + |
| 15 | +diff --git a/src/libjasper/base/jas_image.c b/src/libjasper/base/jas_image.c |
| 16 | +index 1ed0905..c8aa42b 100644 |
| 17 | +--- a/src/libjasper/base/jas_image.c |
| 18 | ++++ b/src/libjasper/base/jas_image.c |
| 19 | +@@ -118,6 +118,8 @@ static void jas_image_calcbbox2(const jas_image_t *image, |
| 20 | + jas_image_coord_t *bry); |
| 21 | + static void jas_image_fmtinfo_init(jas_image_fmtinfo_t *fmtinfo); |
| 22 | + static void jas_image_fmtinfo_cleanup(jas_image_fmtinfo_t *fmtinfo); |
| 23 | ++static jas_cmcmptfmt_t* jas_cmcmptfmt_array_create(int n); |
| 24 | ++static void jas_cmcmptfmt_array_destroy(jas_cmcmptfmt_t* cmptfmts, int n); |
| 25 | + |
| 26 | + /******************************************************************************\ |
| 27 | + * Create and destroy operations. |
| 28 | +@@ -413,6 +415,36 @@ static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt) |
| 29 | + jas_free(cmpt); |
| 30 | + } |
| 31 | + |
| 32 | ++static jas_cmcmptfmt_t* jas_cmcmptfmt_array_create(int n) |
| 33 | ++{ |
| 34 | ++ jas_cmcmptfmt_t* cmptfmts; |
| 35 | ++ JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_create(%d)\n", n); |
| 36 | ++ if (!(cmptfmts = jas_alloc2(n, sizeof(jas_cmcmptfmt_t)))) { |
| 37 | ++ return 0; |
| 38 | ++ } |
| 39 | ++ for (int i = 0; i < n; ++i) { |
| 40 | ++ cmptfmts[i].buf = 0; |
| 41 | ++ } |
| 42 | ++ JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_create(%d) returning %p\n", n, |
| 43 | ++ JAS_CAST(void *, cmptfmts)); |
| 44 | ++ return cmptfmts; |
| 45 | ++} |
| 46 | ++ |
| 47 | ++static void jas_cmcmptfmt_array_destroy(jas_cmcmptfmt_t* cmptfmts, int n) |
| 48 | ++{ |
| 49 | ++ assert(cmptfmts); |
| 50 | ++ assert(n > 0); |
| 51 | ++ JAS_LOGDEBUGF(10, "jas_cmcmptfmt_array_destroy(%p, %d)\n", |
| 52 | ++ JAS_CAST(void *, cmptfmts), n); |
| 53 | ++ for (int i = 0; i < n; ++i) { |
| 54 | ++ if (cmptfmts[i].buf) { |
| 55 | ++ jas_free(cmptfmts[i].buf); |
| 56 | ++ } |
| 57 | ++ cmptfmts[i].buf = 0; |
| 58 | ++ } |
| 59 | ++ jas_free(cmptfmts); |
| 60 | ++} |
| 61 | ++ |
| 62 | + /******************************************************************************\ |
| 63 | + * Load and save operations. |
| 64 | + \******************************************************************************/ |
| 65 | +@@ -1588,12 +1620,15 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 66 | + jas_cmcmptfmt_t *incmptfmts; |
| 67 | + jas_cmcmptfmt_t *outcmptfmts; |
| 68 | + |
| 69 | ++ assert(image); |
| 70 | ++ assert(outprof); |
| 71 | ++ |
| 72 | + #if 0 |
| 73 | + jas_eprintf("IMAGE\n"); |
| 74 | + jas_image_dump(image, stderr); |
| 75 | + #endif |
| 76 | + |
| 77 | +- if (image->numcmpts_ == 0) { |
| 78 | ++ if (!jas_image_numcmpts(image)) { |
| 79 | + /* |
| 80 | + can't work with a file with no components; |
| 81 | + continuing would crash because we'd attempt to |
| 82 | +@@ -1604,6 +1639,8 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 83 | + |
| 84 | + outimage = 0; |
| 85 | + xform = 0; |
| 86 | ++ incmptfmts = 0; |
| 87 | ++ outcmptfmts = 0; |
| 88 | + if (!(inimage = jas_image_copy(image))) { |
| 89 | + goto error; |
| 90 | + } |
| 91 | +@@ -1694,16 +1731,22 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 92 | + } |
| 93 | + |
| 94 | + inpixmap.numcmpts = numinclrchans; |
| 95 | +- if (!(incmptfmts = jas_alloc2(numinclrchans, sizeof(jas_cmcmptfmt_t)))) { |
| 96 | ++ assert(numinclrchans != 0); |
| 97 | ++ if (!(incmptfmts = jas_cmcmptfmt_array_create(numinclrchans))) { |
| 98 | + // formerly call to abort() |
| 99 | + goto error; |
| 100 | + } |
| 101 | + inpixmap.cmptfmts = incmptfmts; |
| 102 | + for (unsigned i = 0; i < numinclrchans; ++i) { |
| 103 | + const int j = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(i)); |
| 104 | ++ if (j < 0) { |
| 105 | ++ jas_logerrorf("missing color component %d\n", i); |
| 106 | ++ goto error; |
| 107 | ++ } |
| 108 | + if (!(incmptfmts[i].buf = jas_alloc2(width, sizeof(long)))) { |
| 109 | + goto error; |
| 110 | + } |
| 111 | ++ assert(j >= 0 && j < jas_image_numcmpts(inimage)); |
| 112 | + incmptfmts[i].prec = jas_image_cmptprec(inimage, j); |
| 113 | + incmptfmts[i].sgnd = jas_image_cmptsgnd(inimage, j); |
| 114 | + incmptfmts[i].width = width; |
| 115 | +@@ -1711,7 +1754,7 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 116 | + } |
| 117 | + |
| 118 | + outpixmap.numcmpts = numoutclrchans; |
| 119 | +- if (!(outcmptfmts = jas_alloc2(numoutclrchans, sizeof(jas_cmcmptfmt_t)))) { |
| 120 | ++ if (!(outcmptfmts = jas_cmcmptfmt_array_create(numoutclrchans))) { |
| 121 | + // formerly call to abort() |
| 122 | + goto error; |
| 123 | + } |
| 124 | +@@ -1719,9 +1762,14 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 125 | + |
| 126 | + for (unsigned i = 0; i < numoutclrchans; ++i) { |
| 127 | + const int j = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(i)); |
| 128 | ++ if (j < 0) { |
| 129 | ++ jas_logerrorf("missing color component %d\n", i); |
| 130 | ++ goto error; |
| 131 | ++ } |
| 132 | + if (!(outcmptfmts[i].buf = jas_alloc2(width, sizeof(long)))) { |
| 133 | + goto error; |
| 134 | + } |
| 135 | ++ assert(j >= 0 && j < jas_image_numcmpts(outimage)); |
| 136 | + outcmptfmts[i].prec = jas_image_cmptprec(outimage, j); |
| 137 | + outcmptfmts[i].sgnd = jas_image_cmptsgnd(outimage, j); |
| 138 | + outcmptfmts[i].width = width; |
| 139 | +@@ -1746,14 +1794,8 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | +- for (unsigned i = 0; i < numoutclrchans; ++i) { |
| 144 | +- jas_free(outcmptfmts[i].buf); |
| 145 | +- } |
| 146 | +- jas_free(outcmptfmts); |
| 147 | +- for (unsigned i = 0; i < numinclrchans; ++i) { |
| 148 | +- jas_free(incmptfmts[i].buf); |
| 149 | +- } |
| 150 | +- jas_free(incmptfmts); |
| 151 | ++ jas_cmcmptfmt_array_destroy(outcmptfmts, numoutclrchans); |
| 152 | ++ jas_cmcmptfmt_array_destroy(incmptfmts, numinclrchans); |
| 153 | + jas_cmxform_destroy(xform); |
| 154 | + jas_image_destroy(inimage); |
| 155 | + |
| 156 | +@@ -1765,6 +1807,14 @@ jas_image_t *jas_image_chclrspc(jas_image_t *image, |
| 157 | + #endif |
| 158 | + return outimage; |
| 159 | + error: |
| 160 | ++ if (incmptfmts) { |
| 161 | ++ assert(numinclrchans); |
| 162 | ++ jas_cmcmptfmt_array_destroy(incmptfmts, numinclrchans); |
| 163 | ++ } |
| 164 | ++ if (outcmptfmts) { |
| 165 | ++ assert(numoutclrchans); |
| 166 | ++ jas_cmcmptfmt_array_destroy(outcmptfmts, numoutclrchans); |
| 167 | ++ } |
| 168 | + if (xform) { |
| 169 | + jas_cmxform_destroy(xform); |
| 170 | + } |
| 171 | +-- |
| 172 | +2.45.4 |
| 173 | + |
0 commit comments