Commit c9220d5b authored by Michael Niedermayer's avatar Michael Niedermayer

avcodec/mjpegdec: Reorder operations to avoid undefined behavior

Fixes: asan_heap-oob_1dd60fd_267_cov_2954683513_5baad44ca4702949724234e35c5bb341.jpg

Found-by: Mateusz "j00ru" Jurczyk and Gynvael Coldwind
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 9dc0bac9
...@@ -719,7 +719,7 @@ static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block, ...@@ -719,7 +719,7 @@ static int decode_dc_progressive(MJpegDecodeContext *s, int16_t *block,
av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
val = (val * quant_matrix[0] << Al) + s->last_dc[component]; val = (val * (quant_matrix[0] << Al)) + s->last_dc[component];
s->last_dc[component] = val; s->last_dc[component] = val;
block[0] = val; block[0] = val;
return 0; return 0;
...@@ -762,14 +762,14 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block, ...@@ -762,14 +762,14 @@ static int decode_block_progressive(MJpegDecodeContext *s, int16_t *block,
if (i >= se) { if (i >= se) {
if (i == se) { if (i == se) {
j = s->scantable.permutated[se]; j = s->scantable.permutated[se];
block[j] = level * quant_matrix[j] << Al; block[j] = level * (quant_matrix[j] << Al);
break; break;
} }
av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
j = s->scantable.permutated[i]; j = s->scantable.permutated[i];
block[j] = level * quant_matrix[j] << Al; block[j] = level * (quant_matrix[j] << Al);
} else { } else {
if (run == 0xF) {// ZRL - skip 15 coefficients if (run == 0xF) {// ZRL - skip 15 coefficients
i += 15; i += 15;
...@@ -848,7 +848,7 @@ static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block, ...@@ -848,7 +848,7 @@ static int decode_block_refinement(MJpegDecodeContext *s, int16_t *block,
ZERO_RUN; ZERO_RUN;
j = s->scantable.permutated[i]; j = s->scantable.permutated[i];
val--; val--;
block[j] = ((quant_matrix[j]^val) - val) << Al; block[j] = ((quant_matrix[j] << Al) ^ val) - val;
if (i == se) { if (i == se) {
if (i > *last_nnz) if (i > *last_nnz)
*last_nnz = i; *last_nnz = i;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment