1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avcodec/jpeglsenc: Check for memory allocation failures

Fixes CID1271044

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2015-02-22 13:43:52 +01:00
parent f9d24ee1dc
commit 48214956b2

View File

@ -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,
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,
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,
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,
} }
} }
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,
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)