Skip to content

Commit

Permalink
Fixed an integral type promotion problem by adding a JAS_CAST.
Browse files Browse the repository at this point in the history
Modified the jpc_tsfb_synthesize function so that it will be a noop for
an empty sequence (in order to avoid dereferencing a null pointer).
  • Loading branch information
mdadams committed Oct 20, 2016
1 parent 95e510c commit 2e82fa0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/libjasper/include/jasper/jas_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ extern "C" {
#define JAS_ONES(n) \
((1 << (n)) - 1)

/******************************************************************************\
*
\******************************************************************************/

__attribute__((no_sanitize("undefined")))
inline static jas_int_asr(int x, int n)
{
assert(n >= 0);
return x >> n;
}

__attribute__((no_sanitize("undefined")))
inline static jas_int_asl(int x, int n)
{
assert(n >= 0);
return x << n;
}

/******************************************************************************\
* Safe integer arithmetic (i.e., with overflow checking).
\******************************************************************************/
Expand Down
5 changes: 5 additions & 0 deletions src/libjasper/include/jasper/jas_seq.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ typedef jas_matrix_t jas_seq_t;
#define jas_matrix_numcols(matrix) \
((matrix)->numcols_)

#define jas_matrix_size(matrix) \
(jas_matrix_width(matrix) * jas_matrix_height(matrix))

/* Get a matrix element. */
#define jas_matrix_get(matrix, i, j) \
((matrix)->rows_[i][j])
Expand Down Expand Up @@ -269,6 +272,8 @@ jas_matrix_t *jas_seq2d_create(int xstart, int ystart, int xend, int yend);
((s)->xstart_ = (x), (s)->ystart_ = (y), \
(s)->xend_ = (s)->xstart_ + (s)->numcols_, \
(s)->yend_ = (s)->ystart_ + (s)->numrows_)
#define jas_seq2d_size(s) \
(jas_seq2d_width(s) * jas_seq2d_height(s))

void jas_seq2d_bindsub(jas_matrix_t *s, jas_matrix_t *s1, int xstart,
int ystart, int xend, int yend);
Expand Down
9 changes: 8 additions & 1 deletion src/libjasper/jpc/jpc_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,13 @@ static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps)
bool warn;
uint_fast32_t mask;

if (roishift < 0) {
/* We could instead return an error here. */
/* I do not think it matters much. */
jas_eprintf("warning: forcing negative ROI shift to zero "
"(bitstream is probably corrupt)\n");
roishift = 0;
}
if (roishift == 0 && bgshift == 0) {
return;
}
Expand All @@ -1856,7 +1863,7 @@ static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps)
} else {
/* We are dealing with non-ROI (i.e., background) data. */
mag <<= bgshift;
mask = (1 << numbps) - 1;
mask = (JAS_CAST(uint_fast32_t, 1) << numbps) - 1;
/* Perform a basic sanity check on the sample value. */
/* Some implementations write garbage in the unused
most-significant bit planes introduced by ROI shifting.
Expand Down
3 changes: 2 additions & 1 deletion src/libjasper/jpc/jpc_tsfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart,

int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a)
{
return (tsfb->numlvls > 0) ? jpc_tsfb_synthesize2(tsfb,
return (tsfb->numlvls > 0 && jas_seq2d_size(a)) ?
jpc_tsfb_synthesize2(tsfb,
jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)),
jas_seq2d_xstart(a), jas_seq2d_ystart(a), jas_seq2d_width(a),
jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0;
Expand Down

0 comments on commit 2e82fa0

Please sign in to comment.