---
As the commented case were cleared in separate patches, this last version
should address all comments received.
When memory is not immediately free'd it means it is cleared by .close.
The internal capability FF_CODEC_CAP_INIT_CLEANUP is employed to avoid leaks.
Vittorio

 libavcodec/aacpsy.c        |  6 ++++++
 libavcodec/asvenc.c        |  2 ++
 libavcodec/dct.c           |  2 ++
 libavcodec/eatgv.c         |  2 ++
 libavcodec/huffyuvenc.c    |  2 +-
 libavcodec/jpeglsdec.c     |  6 ++++++
 libavcodec/lclenc.c        |  2 ++
 libavcodec/libtheoraenc.c  |  6 ++++++
 libavcodec/libx264.c       |  4 ++++
 libavcodec/libxvid.c       |  6 ++++++
 libavcodec/psymodel.c      | 12 ++++++++++++
 libavcodec/pthread_frame.c |  7 +++++++
 libavcodec/ratecontrol.c   |  7 +++++++
 libavcodec/svq1enc.c       |  8 ++++++++
 libavcodec/truemotion2.c   | 10 ++++++++++
 libavcodec/wmaenc.c        |  4 ++++
 libavcodec/xsubdec.c       | 13 +++++++++++++
 17 files changed, 98 insertions(+), 1 deletion(-)

diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c
index 6c6e573..15f872e 100644
--- a/libavcodec/aacpsy.c
+++ b/libavcodec/aacpsy.c
@@ -298,6 +298,8 @@ static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
     const float num_bark   = calc_bark((float)bandwidth);
 
     ctx->model_priv_data = av_mallocz(sizeof(AacPsyContext));
+    if (!ctx->model_priv_data)
+        return AVERROR(ENOMEM);
     pctx = (AacPsyContext*) ctx->model_priv_data;
 
     pctx->chan_bitrate = chan_bitrate;
@@ -349,6 +351,10 @@ static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
     }
 
     pctx->ch = av_mallocz(sizeof(AacPsyChannel) * ctx->avctx->channels);
+    if (!pctx->ch) {
+        av_freep(&pctx);
+        return AVERROR(ENOMEM);
+    }
 
     lame_window_init(pctx, ctx->avctx);
 
diff --git a/libavcodec/asvenc.c b/libavcodec/asvenc.c
index f8c52af..0879615 100644
--- a/libavcodec/asvenc.c
+++ b/libavcodec/asvenc.c
@@ -299,6 +299,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
                      avctx->global_quality / 2) / avctx->global_quality;
 
     avctx->extradata                   = av_mallocz(8);
+    if (!avctx->extradata)
+        return AVERROR(ENOMEM);
     avctx->extradata_size              = 8;
     ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
     ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
diff --git a/libavcodec/dct.c b/libavcodec/dct.c
index 4dbbff8..180477e 100644
--- a/libavcodec/dct.c
+++ b/libavcodec/dct.c
@@ -191,6 +191,8 @@ av_cold int ff_dct_init(DCTContext *s, int nbits, enum 
DCTTransformType inverse)
 
         s->costab = ff_cos_tabs[nbits + 2];
         s->csc2   = av_malloc(n / 2 * sizeof(FFTSample));
+        if (!s->csc2)
+            return AVERROR(ENOMEM);
 
         if (ff_rdft_init(&s->rdft, nbits, inverse == DCT_III) < 0) {
             av_free(s->csc2);
diff --git a/libavcodec/eatgv.c b/libavcodec/eatgv.c
index c400b56..5df4301 100644
--- a/libavcodec/eatgv.c
+++ b/libavcodec/eatgv.c
@@ -174,6 +174,8 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
     /* allocate codebook buffers as necessary */
     if (num_mvs > s->num_mvs) {
         s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
+        if (!s->mv_codebook)
+            return AVERROR(ENOMEM);
         s->num_mvs = num_mvs;
     }
 
diff --git a/libavcodec/huffyuvenc.c b/libavcodec/huffyuvenc.c
index 6b3ff76..175b256 100644
--- a/libavcodec/huffyuvenc.c
+++ b/libavcodec/huffyuvenc.c
@@ -155,7 +155,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->version = 2;
 
     avctx->coded_frame = av_frame_alloc();
-    if (!avctx->coded_frame)
+    if (!avctx->extradata || !avctx->stats_out || !avctx->coded_frame)
         return AVERROR(ENOMEM);
 
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index d9b08fb..3b93799 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -278,10 +278,16 @@ int ff_jpegls_decode_picture(MJpegDecodeContext *s, int 
near,
     int off = 0, stride = 1, width, shift, ret = 0;
 
     zero = av_mallocz(s->picture_ptr->linesize[0]);
+    if (!zero)
+        return AVERROR(ENOMEM);
     last = zero;
     cur  = s->picture_ptr->data[0];
 
     state = av_mallocz(sizeof(JLSState));
+    if (!state) {
+        av_free(zero);
+        return AVERROR(ENOMEM);
+    }
     /* initialize JPEG-LS state from JPEG parameters */
     state->near   = near;
     state->bpp    = (s->bits < 2) ? 2 : s->bits;
diff --git a/libavcodec/lclenc.c b/libavcodec/lclenc.c
index 20841bc..2448b9e 100644
--- a/libavcodec/lclenc.c
+++ b/libavcodec/lclenc.c
@@ -135,6 +135,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
     assert(avctx->width && avctx->height);
 
     avctx->extradata= av_mallocz(8);
+    if (!avctx->extradata)
+        return AVERROR(ENOMEM);
 
     avctx->coded_frame = av_frame_alloc();
     if (!avctx->coded_frame)
diff --git a/libavcodec/libtheoraenc.c b/libavcodec/libtheoraenc.c
index 462bc94..097336b 100644
--- a/libavcodec/libtheoraenc.c
+++ b/libavcodec/libtheoraenc.c
@@ -101,6 +101,8 @@ static int get_stats(AVCodecContext *avctx, int eos)
     if (!eos) {
         h->stats = av_fast_realloc(h->stats, &h->stats_size,
                                    h->stats_offset + bytes);
+        if (!h->stats)
+            return AVERROR(ENOMEM);
         memcpy(h->stats + h->stats_offset, buf, bytes);
         h->stats_offset += bytes;
     } else {
@@ -108,6 +110,8 @@ static int get_stats(AVCodecContext *avctx, int eos)
         // libtheora generates a summary header at the end
         memcpy(h->stats, buf, bytes);
         avctx->stats_out = av_malloc(b64_size);
+        if (!avctx->stats_out)
+            return AVERROR(ENOMEM);
         av_base64_encode(avctx->stats_out, b64_size, h->stats, 
h->stats_offset);
     }
     return 0;
@@ -131,6 +135,8 @@ static int submit_stats(AVCodecContext *avctx)
         }
         h->stats_size = strlen(avctx->stats_in) * 3/4;
         h->stats      = av_malloc(h->stats_size);
+        if (!h->stats)
+            return AVERROR(ENOMEM);
         h->stats_size = av_base64_decode(h->stats, avctx->stats_in, 
h->stats_size);
     }
     while (h->stats_size - h->stats_offset > 0) {
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 841b824..d7d1df3 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -542,6 +542,8 @@ static av_cold int X264_init(AVCodecContext *avctx)
 
         s = x264_encoder_headers(x4->enc, &nal, &nnal);
         avctx->extradata = p = av_malloc(s);
+        if (!p)
+            return AVERROR(ENOMEM);
 
         for (i = 0; i < nnal; i++) {
             /* Don't put the SEI in extradata. */
@@ -549,6 +551,8 @@ static av_cold int X264_init(AVCodecContext *avctx)
                 av_log(avctx, AV_LOG_INFO, "%s\n", nal[i].p_payload+25);
                 x4->sei_size = nal[i].i_payload;
                 x4->sei      = av_malloc(x4->sei_size);
+                if (!x4->sei)
+                    return AVERROR(ENOMEM);
                 memcpy(x4->sei, nal[i].p_payload, nal[i].i_payload);
                 continue;
             }
diff --git a/libavcodec/libxvid.c b/libavcodec/libxvid.c
index 46da773..97ff95b 100644
--- a/libavcodec/libxvid.c
+++ b/libavcodec/libxvid.c
@@ -276,6 +276,8 @@ static int xvid_strip_vol_header(AVCodecContext *avctx, 
AVPacket *pkt,
         /* We need to store the header, so extract it */
         if (!avctx->extradata) {
             avctx->extradata = av_malloc(vo_len);
+            if (!avctx->extradata)
+                return AVERROR(ENOMEM);
             memcpy(avctx->extradata, pkt->data, vo_len);
             avctx->extradata_size = vo_len;
         }
@@ -594,11 +596,15 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx)
         if (avctx->intra_matrix) {
             intra           = avctx->intra_matrix;
             x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
+            if (!x->intra_matrix)
+                return AVERROR(ENOMEM);
         } else
             intra = NULL;
         if (avctx->inter_matrix) {
             inter           = avctx->inter_matrix;
             x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
+            if (!x->inter_matrix)
+                return AVERROR(ENOMEM);
         } else
             inter = NULL;
 
diff --git a/libavcodec/psymodel.c b/libavcodec/psymodel.c
index a2af611..5179ede 100644
--- a/libavcodec/psymodel.c
+++ b/libavcodec/psymodel.c
@@ -39,6 +39,12 @@ av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext 
*avctx, int num_lens,
     ctx->group     = av_mallocz(sizeof(ctx->group[0]) * num_groups);
     ctx->bands     = av_malloc (sizeof(ctx->bands[0])     * num_lens);
     ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
+
+    if (!ctx->ch || !ctx->group || !ctx->bands || !ctx->num_bands) {
+        ff_psy_end(ctx);
+        return AVERROR(ENOMEM);
+    }
+
     memcpy(ctx->bands,     bands,     sizeof(ctx->bands[0])     *  num_lens);
     memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) *  num_lens);
 
@@ -98,6 +104,8 @@ av_cold struct FFPsyPreprocessContext* 
ff_psy_preprocess_init(AVCodecContext *av
     int i;
     float cutoff_coeff = 0;
     ctx        = av_mallocz(sizeof(FFPsyPreprocessContext));
+    if (!ctx)
+        return NULL;
     ctx->avctx = avctx;
 
     if (avctx->cutoff > 0)
@@ -109,6 +117,10 @@ av_cold struct FFPsyPreprocessContext* 
ff_psy_preprocess_init(AVCodecContext *av
                                              cutoff_coeff, 0.0, 0.0);
     if (ctx->fcoeffs) {
         ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
+        if (!ctx->fstate) {
+            av_free(ctx);
+            return NULL;
+        }
         for (i = 0; i < avctx->channels; i++)
             ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
     }
diff --git a/libavcodec/pthread_frame.c b/libavcodec/pthread_frame.c
index effc9a5..d7f742b 100644
--- a/libavcodec/pthread_frame.c
+++ b/libavcodec/pthread_frame.c
@@ -584,8 +584,15 @@ int ff_frame_thread_init(AVCodecContext *avctx)
     }
 
     avctx->internal->thread_ctx = fctx = 
av_mallocz(sizeof(FrameThreadContext));
+    if (!fctx)
+        return AVERROR(ENOMEM);
 
     fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
+    if (!fctx->threads) {
+        av_freep(&avctx->internal->thread_ctx);
+        return AVERROR(ENOMEM);
+    }
+
     pthread_mutex_init(&fctx->buffer_mutex, NULL);
     fctx->delaying = 1;
 
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index aae7570..6fe174a 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -163,6 +163,8 @@ av_cold int ff_rate_control_init(MpegEncContext *s)
             return -1;
         rcc->entry       = av_mallocz(i * sizeof(RateControlEntry));
         rcc->num_entries = i;
+        if (!rcc->entry)
+            return AVERROR(ENOMEM);
 
         /* init all to skipped p frames
          * (with b frames we might have a not encoded frame at the end FIXME) 
*/
@@ -932,6 +934,11 @@ static int init_pass2(MpegEncContext *s)
 
     qscale         = av_malloc(sizeof(double) * rcc->num_entries);
     blurred_qscale = av_malloc(sizeof(double) * rcc->num_entries);
+    if (!qscale || !blurred_qscale) {
+        av_freep(&qscale);
+        av_freep(&blurred_qscale);
+        return AVERROR(ENOMEM);
+    }
     toobig = 0;
 
     for (step = 256 * 256; step > 0.0000001; step *= 0.5) {
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index f49f487..7620332 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -293,6 +293,8 @@ static int svq1_encode_plane(SVQ1EncContext *s, int plane,
             s->motion_val16[plane] = av_mallocz((s->m.mb_stride *
                                                  (block_height + 2) + 1) *
                                                 2 * sizeof(int16_t));
+            if (!s->motion_val8[plane] || !s->motion_val16[plane])
+                return AVERROR(ENOMEM);
         }
 
         s->m.mb_type = s->mb_type;
@@ -550,6 +552,12 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx)
                                         s->y_block_height * sizeof(int32_t));
     s->ssd_int8_vs_int16   = ssd_int8_vs_int16_c;
 
+    if (!s->m.me.temp || !s->m.me.scratchpad || !s->m.me.map ||
+        !s->m.me.score_map || !s->mb_type || !s->dummy) {
+        svq1_encode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
+
     if (ARCH_PPC)
         ff_svq1enc_init_ppc(s);
     if (ARCH_X86)
diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c
index 122643d..094096e 100644
--- a/libavcodec/truemotion2.c
+++ b/libavcodec/truemotion2.c
@@ -171,6 +171,10 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes 
*code)
     huff.nums    = av_mallocz(huff.max_num * sizeof(int));
     huff.bits    = av_mallocz(huff.max_num * sizeof(uint32_t));
     huff.lens    = av_mallocz(huff.max_num * sizeof(int));
+    if (!huff.nums || !huff.bits || !huff.lens) {
+        res = AVERROR(ENOMEM);
+        goto out;
+    }
 
     res = tm2_read_tree(ctx, 0, 0, &huff);
 
@@ -193,10 +197,16 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes 
*code)
             code->bits = huff.max_bits;
             code->length = huff.max_num;
             code->recode = av_malloc(code->length * sizeof(int));
+            if (!code->recode) {
+                res = AVERROR(ENOMEM);
+                goto out;
+            }
             for (i = 0; i < code->length; i++)
                 code->recode[i] = huff.nums[i];
         }
     }
+
+out:
     /* free allocated memory */
     av_free(huff.nums);
     av_free(huff.bits);
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index e801663..c176daa 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -62,11 +62,15 @@ static av_cold int encode_init(AVCodecContext *avctx)
     flags2 = 1;
     if (avctx->codec->id == AV_CODEC_ID_WMAV1) {
         extradata             = av_malloc(4);
+        if (!extradata)
+            return AVERROR(ENOMEM);
         avctx->extradata_size = 4;
         AV_WL16(extradata, flags1);
         AV_WL16(extradata + 2, flags2);
     } else if (avctx->codec->id == AV_CODEC_ID_WMAV2) {
         extradata             = av_mallocz(10);
+        if (!extradata)
+            return AVERROR(ENOMEM);
         avctx->extradata_size = 10;
         AV_WL32(extradata, flags1);
         AV_WL16(extradata + 4, flags2);
diff --git a/libavcodec/xsubdec.c b/libavcodec/xsubdec.c
index d01b410..a7dd7ee 100644
--- a/libavcodec/xsubdec.c
+++ b/libavcodec/xsubdec.c
@@ -95,7 +95,13 @@ static int decode_frame(AVCodecContext *avctx, void *data, 
int *data_size,
 
     // allocate sub and set values
     sub->rects =  av_mallocz(sizeof(*sub->rects));
+    if (!sub->rects)
+        return AVERROR(ENOMEM);
     sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
+    if (!sub->rects[0]) {
+        av_freep(&sub->rects);
+        return AVERROR(ENOMEM);
+    }
     sub->num_rects = 1;
     sub->rects[0]->x = x; sub->rects[0]->y = y;
     sub->rects[0]->w = w; sub->rects[0]->h = h;
@@ -104,6 +110,13 @@ static int decode_frame(AVCodecContext *avctx, void *data, 
int *data_size,
     sub->rects[0]->pict.data[0] = av_malloc(w * h);
     sub->rects[0]->nb_colors = 4;
     sub->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
+    if (!sub->rects[0]->pict.data[0] || !sub->rects[0]->pict.data[1]) {
+        av_freep(&sub->rects[0]->pict.data[1]);
+        av_freep(&sub->rects[0]->pict.data[0]);
+        av_freep(&sub->rects[0]);
+        av_freep(&sub->rects);
+        return AVERROR(ENOMEM);
+    }
 
     // read palette
     for (i = 0; i < sub->rects[0]->nb_colors; i++)
-- 
1.9.5 (Apple Git-50.3)

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to