Commit 48214956 authored by Michael Niedermayer's avatar Michael Niedermayer

avcodec/jpeglsenc: Check for memory allocation failures

Fixes CID1271044
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent f9d24ee1
...@@ -253,8 +253,9 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -253,8 +253,9 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
const int near = avctx->prediction_method; const int near = avctx->prediction_method;
PutBitContext pb, pb2; PutBitContext pb, pb2;
GetBitContext gb; GetBitContext gb;
uint8_t *buf2, *zero, *cur, *last; uint8_t *buf2 = NULL;
JLSState *state; uint8_t *zero, *cur, *last;
JLSState *state = NULL;
int i, size, ret; int i, size, ret;
int comps; int comps;
...@@ -269,6 +270,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -269,6 +270,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
return ret; return ret;
buf2 = av_malloc(pkt->size); buf2 = av_malloc(pkt->size);
if (!buf2)
goto fail;
init_put_bits(&pb, pkt->data, pkt->size); init_put_bits(&pb, pkt->data, pkt->size);
init_put_bits(&pb2, buf2, pkt->size); init_put_bits(&pb2, buf2, pkt->size);
...@@ -299,6 +302,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -299,6 +302,8 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
put_bits(&pb, 8, 0); // point transform: none put_bits(&pb, 8, 0); // point transform: none
state = av_mallocz(sizeof(JLSState)); state = av_mallocz(sizeof(JLSState));
if (!state)
goto fail;
/* initialize JPEG-LS state from JPEG parameters */ /* initialize JPEG-LS state from JPEG parameters */
state->near = near; state->near = near;
state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8; state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
...@@ -308,10 +313,9 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -308,10 +313,9 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
ls_store_lse(state, &pb); ls_store_lse(state, &pb);
zero = av_mallocz(FFABS(p->linesize[0])); zero = av_mallocz(FFABS(p->linesize[0]));
if (!zero) { if (!zero)
av_free(state); goto fail;
return AVERROR(ENOMEM);
}
last = zero; last = zero;
cur = p->data[0]; cur = p->data[0];
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) { if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
...@@ -384,7 +388,7 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -384,7 +388,7 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
} }
} }
avpriv_align_put_bits(&pb); avpriv_align_put_bits(&pb);
av_free(buf2); av_freep(&buf2);
/* End of image */ /* End of image */
put_marker(&pb, EOI); put_marker(&pb, EOI);
...@@ -396,6 +400,11 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, ...@@ -396,6 +400,11 @@ static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
pkt->flags |= AV_PKT_FLAG_KEY; pkt->flags |= AV_PKT_FLAG_KEY;
*got_packet = 1; *got_packet = 1;
return 0; return 0;
fail:
av_freep(&buf2);
av_freep(&state);
return AVERROR(ENOMEM);
} }
static av_cold int encode_close(AVCodecContext *avctx) static av_cold int encode_close(AVCodecContext *avctx)
......
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