[libav-stable] [PATCH] ogg: check and propagate memory allocation failures as appropriate

Sean McGovern gseanmcg at gmail.com
Mon Dec 9 06:22:20 CET 2013


CC: libav-stable at libav.org
---
 libavformat/oggdec.c |   34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index ae9da3a..243f550 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -60,9 +60,13 @@ static const struct ogg_codec * const ogg_codecs[] = {
 static int ogg_save(AVFormatContext *s)
 {
     struct ogg *ogg = s->priv_data;
+    int i, ret = 0;
     struct ogg_state *ost =
         av_malloc(sizeof(*ost) + (ogg->nstreams - 1) * sizeof(*ogg->streams));
-    int i;
+
+    if (!ost)
+        return AVERROR(ENOMEM);
+
     ost->pos      = avio_tell(s->pb);
     ost->curidx   = ogg->curidx;
     ost->next     = ogg->state;
@@ -72,12 +76,20 @@ static int ogg_save(AVFormatContext *s)
     for (i = 0; i < ogg->nstreams; i++) {
         struct ogg_stream *os = ogg->streams + i;
         os->buf = av_mallocz(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
+        if (!os->buf) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
         memcpy(os->buf, ost->streams[i].buf, os->bufpos);
     }
 
     ogg->state = ost;
 
     return 0;
+            
+fail:
+    av_freep(&ost);
+    return ret;
 }
 
 static int ogg_restore(AVFormatContext *s, int discard)
@@ -156,6 +168,7 @@ static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
     int idx         = ogg->nstreams++;
     AVStream *st;
     struct ogg_stream *os;
+    int ret = 0;
 
     os = av_realloc(ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
 
@@ -173,6 +186,11 @@ static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
     os->header        = -1;
     os->start_granule = OGG_NOGRANULE_VALUE;
 
+    if (!os->buf) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
     if (new_avstream) {
         st = avformat_new_stream(s, NULL);
         if (!st)
@@ -183,6 +201,10 @@ static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
     }
 
     return idx;
+    
+fail:
+    av_freep(&os);
+    return ret;
 }
 
 static int ogg_new_buf(struct ogg *ogg, int idx)
@@ -191,6 +213,9 @@ static int ogg_new_buf(struct ogg *ogg, int idx)
     uint8_t *nb = av_malloc(os->bufsize + FF_INPUT_BUFFER_PADDING_SIZE);
     int size = os->bufpos - os->pstart;
 
+    if (!nb)
+        return AVERROR(ENOMEM);
+
     if (os->buf) {
         memcpy(nb, os->buf + os->pstart, size);
         av_free(os->buf);
@@ -276,8 +301,11 @@ static int ogg_read_page(AVFormatContext *s, int *str)
     os = ogg->streams + idx;
     os->page_pos = avio_tell(bc) - 27;
 
-    if (os->psize > 0)
-        ogg_new_buf(ogg, idx);
+    if (os->psize > 0) {
+        ret = ogg_new_buf(ogg, idx);
+        if (ret < 0)
+            return ret;
+    }
 
     ret = avio_read(bc, os->segments, nsegs);
     if (ret < nsegs)
-- 
1.7.9.2



More information about the libav-stable mailing list