On Thu, 28 Nov 2013, Alexandra Khirnova wrote:

---
libavcodec/binkaudio.c            |   10 +++++-----
libavcodec/bitstream.c            |   10 +++++++---
libavcodec/eatgv.c                |    4 +++-
libavcodec/flashsv.c              |   29 +++++++++++++----------------
libavcodec/g2meet.c               |   17 ++++++-----------
libavcodec/h264_mp4toannexb_bsf.c |   19 +++++++------------
libavcodec/libmp3lame.c           |   10 +++-------
libavcodec/libschroedingerenc.c   |    9 +++++++--
libavcodec/libtheoraenc.c         |   10 +++++-----
libavcodec/libvpxenc.c            |   10 ++++++----
libavcodec/shorten.c              |   27 ++++++++++-----------------
libavcodec/zmbv.c                 |   16 +++++++---------
12 files changed, 79 insertions(+), 92 deletions(-)

diff --git a/libavcodec/binkaudio.c b/libavcodec/binkaudio.c
index ddaa613..d0d58d5 100644
--- a/libavcodec/binkaudio.c
+++ b/libavcodec/binkaudio.c
@@ -293,7 +293,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
    int ret, consumed = 0;

    if (!get_bits_left(gb)) {
-        uint8_t *buf;
+        int err;
        /* handle end-of-stream */
        if (!avpkt->size) {
            *got_frame_ptr = 0;
@@ -303,10 +303,10 @@ static int decode_frame(AVCodecContext *avctx, void *data,
            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
            return AVERROR_INVALIDDATA;
        }
-        buf = av_realloc(s->packet_buffer, avpkt->size + 
FF_INPUT_BUFFER_PADDING_SIZE);
-        if (!buf)
-            return AVERROR(ENOMEM);
-        s->packet_buffer = buf;
+        if ((err = av_reallocp(&s->packet_buffer,
+                               avpkt->size +
+                               FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
+            return err;
        memcpy(s->packet_buffer, avpkt->data, avpkt->size);
        init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
        consumed = avpkt->size;

Why? Just to make it consistent? To be clear, the existing code seems to work perfectly correctly here, so this doesn't fix a bug as far as I can see.

diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c
index e7c476b..22521e1 100644
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -106,12 +106,16 @@ static int alloc_table(VLC *vlc, int size, int use_static)

    vlc->table_size += size;
    if (vlc->table_size > vlc->table_allocated) {
+        int err;
        if (use_static)
            return AVERROR_BUG;
        vlc->table_allocated += (1 << vlc->bits);
-        vlc->table = av_realloc(vlc->table, sizeof(VLC_TYPE) * 2 * 
vlc->table_allocated);
-        if (!vlc->table)
-            return AVERROR(ENOMEM);
+        if ((err = av_reallocp(&vlc->table,
+                               sizeof(VLC_TYPE) * 2 *
+                               vlc->table_allocated)) < 0) {
+            vlc->table_allocated = 0;
+            return err;

It would probably be good to reset vlc->table_size to 0 here as well, since the array is in fact removed. (In most of these cases with *_size and *_allocated, most of the code will read the *_size variable and not touch the *_allocated, so you'll have to reset both.)

+        }
    }
    return index;
}
diff --git a/libavcodec/eatgv.c b/libavcodec/eatgv.c
index 7ab18c5..0129636 100644
--- a/libavcodec/eatgv.c
+++ b/libavcodec/eatgv.c
@@ -178,7 +178,9 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
    }

    if (num_blocks_packed > s->num_blocks_packed) {
-        s->block_codebook = av_realloc(s->block_codebook, 
num_blocks_packed*16);
+        int err;
+        if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 
0)
+            return err;

You forgot to reset s->num_blocks_packed here (it functions as an *_allocated variable).

        s->num_blocks_packed = num_blocks_packed;
    }

diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c
index a6e8dae..92b32f7 100644
--- a/libavcodec/flashsv.c
+++ b/libavcodec/flashsv.c
@@ -296,13 +296,11 @@ static int flashsv_decode_frame(AVCodecContext *avctx, 
void *data,
    /* the block size could change between frames, make sure the buffer
     * is large enough, if not, get a larger one */
    if (s->block_size < s->block_width * s->block_height) {
-        int tmpblock_size = 3 * s->block_width * s->block_height;
+        int tmpblock_size = 3 * s->block_width * s->block_height, err;

-        s->tmpblock = av_realloc(s->tmpblock, tmpblock_size);
-        if (!s->tmpblock) {
-            av_log(avctx, AV_LOG_ERROR,
-                   "Cannot allocate decompression buffer.\n");
-            return AVERROR(ENOMEM);
+        if ((err = av_reallocp(&s->tmpblock, tmpblock_size)) < 0) {
+            av_log(avctx, AV_LOG_ERROR, "Cannot allocate decompression 
buffer.\n");
+            return err;
        }
        if (s->ver == 2) {
            s->deflate_block_size = calc_deflate_block_size(tmpblock_size);
@@ -311,12 +309,9 @@ static int flashsv_decode_frame(AVCodecContext *avctx, 
void *data,
                       "Cannot determine deflate buffer size.\n");
                return -1;
            }
-            s->deflate_block = av_realloc(s->deflate_block,
-                                          s->deflate_block_size);
-            if (!s->deflate_block) {
-                av_log(avctx, AV_LOG_ERROR,
-                       "Cannot allocate deflate buffer.\n");
-                return AVERROR(ENOMEM);
+            if ((err = av_reallocp(&s->deflate_block, s->deflate_block_size)) 
< 0) {
+                av_log(avctx, AV_LOG_ERROR, "Cannot allocate deflate 
buffer.\n");
+                return err;
            }
        }
    }
@@ -340,11 +335,13 @@ static int flashsv_decode_frame(AVCodecContext *avctx, 
void *data,
    /* we care for keyframes only in Screen Video v2 */
    s->is_keyframe = (avpkt->flags & AV_PKT_FLAG_KEY) && (s->ver == 2);
    if (s->is_keyframe) {
-        s->keyframedata = av_realloc(s->keyframedata, avpkt->size);
+        int err;
+        if ((err = av_reallocp(&s->keyframedata, avpkt->size)) < 0)
+            return err;
        memcpy(s->keyframedata, avpkt->data, avpkt->size);
-        s->blocks = av_realloc(s->blocks,
-                               (v_blocks + !!v_part) * (h_blocks + !!h_part) *
-                               sizeof(s->blocks[0]));
+        if ((err = av_reallocp(&s->blocks, (v_blocks + !!v_part) *
+                               (h_blocks + !!h_part) * sizeof(s->blocks[0]))) 
< 0)
+            return err;
    }

    av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
diff --git a/libavcodec/g2meet.c b/libavcodec/g2meet.c
index 0b4a8b7..1ae87b5 100644
--- a/libavcodec/g2meet.c
+++ b/libavcodec/g2meet.c
@@ -236,16 +236,14 @@ static int jpg_decode_data(JPGContext *c, int width, int 
height,
                           int swapuv)
{
    GetBitContext gb;
-    uint8_t *tmp;
    int mb_w, mb_h, mb_x, mb_y, i, j;
    int bx, by;
    int unesc_size;
    int ret;

-    tmp = av_realloc(c->buf, src_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!tmp)
-        return AVERROR(ENOMEM);
-    c->buf = tmp;
+    if ((ret = av_reallocp(&c->buf,
+                           src_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
+        return ret;
    jpg_unescape(src, src_size, c->buf, &unesc_size);
    memset(c->buf + unesc_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
    init_get_bits(&gb, c->buf, unesc_size * 8);
@@ -478,8 +476,7 @@ static int g2m_load_cursor(AVCodecContext *avctx, 
G2MContext *c,
    uint32_t bits;
    uint32_t cur_size, cursor_w, cursor_h, cursor_stride;
    uint32_t cursor_hot_x, cursor_hot_y;
-    int cursor_fmt;
-    uint8_t *tmp;
+    int cursor_fmt, err;

    cur_size      = bytestream2_get_be32(gb);
    cursor_w      = bytestream2_get_byte(gb);
@@ -514,13 +511,11 @@ static int g2m_load_cursor(AVCodecContext *avctx, 
G2MContext *c,
        return AVERROR_PATCHWELCOME;
    }

-    tmp = av_realloc(c->cursor, cursor_stride * cursor_h);
-    if (!tmp) {
+    if ((err = av_reallocp(&c->cursor, cursor_stride * cursor_h)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "Cannot allocate cursor buffer\n");
-        return AVERROR(ENOMEM);
+        return err;
    }

-    c->cursor        = tmp;
    c->cursor_w      = cursor_w;
    c->cursor_h      = cursor_h;
    c->cursor_hot_x  = cursor_hot_x;
diff --git a/libavcodec/h264_mp4toannexb_bsf.c 
b/libavcodec/h264_mp4toannexb_bsf.c
index e86c3e1..9e8115d 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -37,13 +37,12 @@ static int alloc_and_copy(uint8_t **poutbuf, int 
*poutbuf_size,
{
    uint32_t offset         = *poutbuf_size;
    uint8_t nal_header_size = offset ? 3 : 4;
-    void *tmp;
+    int err;

    *poutbuf_size += sps_pps_size + in_size + nal_header_size;
-    tmp = av_realloc(*poutbuf, *poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!tmp)
-        return AVERROR(ENOMEM);
-    *poutbuf = tmp;
+    if ((err = av_reallocp(poutbuf,
+                           *poutbuf_size + FF_INPUT_BUFFER_PADDING_SIZE)) < 0)
+        return err;

You'll need to reset *outbuf_size on errors here.

    if (sps_pps)
        memcpy(*poutbuf + offset, sps_pps, sps_pps_size);
    memcpy(*poutbuf + sps_pps_size + nal_header_size + offset, in, in_size);
@@ -84,7 +83,7 @@ static int h264_extradata_to_annexb(AVCodecContext *avctx, 
const int padding)
    }

    while (unit_nb--) {
-        void *tmp;
+        int err;

        unit_size   = AV_RB16(extradata);
        total_size += unit_size + 4;
@@ -94,12 +93,8 @@ static int h264_extradata_to_annexb(AVCodecContext *avctx, 
const int padding)
            av_free(out);
            return AVERROR(EINVAL);
        }
-        tmp = av_realloc(out, total_size + padding);
-        if (!tmp) {
-            av_free(out);
-            return AVERROR(ENOMEM);
-        }
-        out = tmp;
+        if ((err = av_reallocp(&out, total_size + padding)) < 0)
+            return err;
        memcpy(out + total_size - unit_size - 4, nalu_header, 4);
        memcpy(out + total_size - unit_size, extradata + 2, unit_size);
        extradata += 2 + unit_size;
diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index ce0e9c8..ee76ff8 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -57,18 +57,14 @@ typedef struct LAMEContext {
static int realloc_buffer(LAMEContext *s)
{
    if (!s->buffer || s->buffer_size - s->buffer_index < BUFFER_SIZE) {
-        uint8_t *tmp;
-        int new_size = s->buffer_index + 2 * BUFFER_SIZE;
+        int new_size = s->buffer_index + 2 * BUFFER_SIZE, err;

        av_dlog(s->avctx, "resizing output buffer: %d -> %d\n", s->buffer_size,
                new_size);
-        tmp = av_realloc(s->buffer, new_size);
-        if (!tmp) {
-            av_freep(&s->buffer);
+        if ((err = av_reallocp(&s->buffer, new_size)) < 0) {
            s->buffer_size = s->buffer_index = 0;
-            return AVERROR(ENOMEM);
+            return err;
        }
-        s->buffer      = tmp;
        s->buffer_size = new_size;
    }
    return 0;
diff --git a/libavcodec/libschroedingerenc.c b/libavcodec/libschroedingerenc.c
index 11cc0eb..c2aac53 100644
--- a/libavcodec/libschroedingerenc.c
+++ b/libavcodec/libschroedingerenc.c
@@ -295,6 +295,7 @@ static int libschroedinger_encode_frame(AVCodecContext 
*avctx, AVPacket *pkt,

    /* Now check to see if we have any output from the encoder. */
    while (go) {
+        int err;
        SchroStateEnum state;
        state = schro_encoder_wait(encoder);
        switch (state) {
@@ -309,8 +310,12 @@ static int libschroedinger_encode_frame(AVCodecContext 
*avctx, AVPacket *pkt,
             * be able to set the pts correctly. So we don't write data
             * to the frame output queue until we actually have a frame
             */
-            p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf,
-                                                 p_schro_params->enc_buf_size + 
enc_buf->length);
+            if ((err = av_reallocp(&p_schro_params->enc_buf,
+                                   p_schro_params->enc_buf_size +
+                                   enc_buf->length)) < 0) {
+                p_schro_params->enc_buf_size = 0;
+                return err;
+            }

            memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size,
                   enc_buf->data, enc_buf->length);
diff --git a/libavcodec/libtheoraenc.c b/libavcodec/libtheoraenc.c
index 9041a27..a7c1722 100644
--- a/libavcodec/libtheoraenc.c
+++ b/libavcodec/libtheoraenc.c
@@ -58,8 +58,8 @@ static int concatenate_packet(unsigned int* offset,
                              const ogg_packet* packet)
{
    const char* message = NULL;
-    uint8_t* newdata    = NULL;
    int newsize = avc_context->extradata_size + 2 + packet->bytes;
+    int err = AVERROR_INVALIDDATA;

    if (packet->bytes < 0) {
        message = "ogg_packet has negative size";
@@ -68,16 +68,16 @@ static int concatenate_packet(unsigned int* offset,
    } else if (newsize < avc_context->extradata_size) {
        message = "extradata_size would overflow";
    } else {
-        newdata = av_realloc(avc_context->extradata, newsize);
-        if (!newdata)
+        if (av_reallocp(&avc_context->extradata, newsize) < 0) {
+            avc_context->extradata_size = 0;
            message = "av_realloc failed";

Why not store the return value in err here?

+        }
    }
    if (message) {
        av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", 
message);
-        return -1;
+        return err;
    }

-    avc_context->extradata      = newdata;
    avc_context->extradata_size = newsize;
    AV_WB16(avc_context->extradata + (*offset), packet->bytes);
    *offset += 2;
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 9996afc..1945a35 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -467,11 +467,13 @@ static int queue_frames(AVCodecContext *avctx, AVPacket 
*pkt_out,
            break;
        case VPX_CODEC_STATS_PKT: {
            struct vpx_fixed_buf *stats = &ctx->twopass_stats;
-            stats->buf = av_realloc(stats->buf,
-                                    stats->sz + pkt->data.twopass_stats.sz);
-            if (!stats->buf) {
+            int err;
+            if ((err = av_reallocp(&stats->buf,
+                                   stats->sz +
+                                   pkt->data.twopass_stats.sz)) < 0) {
+                stats->sz = 0;
                av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
-                return AVERROR(ENOMEM);
+                return err;
            }
            memcpy((uint8_t*)stats->buf + stats->sz,
                   pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c
index 31251ac..1aed2ed 100644
--- a/libavcodec/shorten.c
+++ b/libavcodec/shorten.c
@@ -119,9 +119,7 @@ static av_cold int shorten_decode_init(AVCodecContext 
*avctx)

static int allocate_buffers(ShortenContext *s)
{
-    int i, chan;
-    int *coeffs;
-    void *tmp_ptr;
+    int i, chan, err;

    for (chan = 0; chan < s->channels; chan++) {
        if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
@@ -135,26 +133,21 @@ static int allocate_buffers(ShortenContext *s)
            return AVERROR_INVALIDDATA;
        }

-        tmp_ptr =
-            av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
-        if (!tmp_ptr)
-            return AVERROR(ENOMEM);
-        s->offset[chan] = tmp_ptr;
+        if ((err = av_reallocp(&s->offset[chan],
+                               sizeof(s->offset[chan]) *

You changed the sizeof. Why? The new sizeof is not only different from the previous, it's also wrong.

+                               FFMAX(1, s->nmean))) < 0)
+            return err;

-        tmp_ptr = av_realloc(s->decoded_base[chan], (s->blocksize + s->nwrap) *
-                             sizeof(s->decoded_base[0][0]));
-        if (!tmp_ptr)
-            return AVERROR(ENOMEM);
-        s->decoded_base[chan] = tmp_ptr;
+        if ((err = av_reallocp(&s->decoded_base[chan], (s->blocksize + 
s->nwrap) *
+                               sizeof(s->decoded_base[0][0]))) < 0)
+            return err;
        for (i = 0; i < s->nwrap; i++)
            s->decoded_base[chan][i] = 0;
        s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
    }

-    coeffs = av_realloc(s->coeffs, s->nwrap * sizeof(*s->coeffs));
-    if (!coeffs)
-        return AVERROR(ENOMEM);
-    s->coeffs = coeffs;
+    if ((err = av_reallocp(&s->coeffs, s->nwrap * sizeof(*s->coeffs))) < 0)
+        return err;

    return 0;
}
diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index d17f37a..9a927da 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -406,7 +406,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, 
int *got_frame, AVPac
    int zret = Z_OK; // Zlib return code
    int len = buf_size;
    int hi_ver, lo_ver, ret;
-    uint8_t *tmp;

    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
@@ -417,6 +416,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, 
int *got_frame, AVPac
    c->flags = buf[0];
    buf++; len--;
    if (c->flags & ZMBV_KEYFRAME) {
+        int err;
        hi_ver = buf[0];
        lo_ver = buf[1];
        c->comp = buf[2];
@@ -481,14 +481,12 @@ static int decode_frame(AVCodecContext *avctx, void 
*data, int *got_frame, AVPac
            return AVERROR_UNKNOWN;
        }

-        tmp = av_realloc(c->cur,  avctx->width * avctx->height * (c->bpp / 8));
-        if (!tmp)
-            return AVERROR(ENOMEM);
-        c->cur = tmp;
-        tmp = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
-        if (!tmp)
-            return AVERROR(ENOMEM);
-        c->prev = tmp;
+        if ((err = av_reallocp(&c->cur, avctx->width * avctx->height *
+                               (c->bpp / 8))) < 0)
+            return err;
+        if ((err = av_reallocp(&c->prev, avctx->width * avctx->height *
+                               (c->bpp / 8))) < 0)
+            return err;

There's not much gain from these ones, it's not much shorter or nicer than the existing code, and above all - the existing code is totally correct.

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

Reply via email to