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

Sean McGovern gseanmcg at gmail.com
Tue Dec 10 01:24:49 CET 2013


CC: libav-stable at libav.org
---
 libavformat/oggdec.c |   30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index ae9da3a..2c96b57 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,17 @@ 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);
+            av_freep(ost);
+            break;
+        }
         memcpy(os->buf, ost->streams[i].buf, os->bufpos);
     }
 
     ogg->state = ost;
 
-    return 0;
+    return ret;
 }
 
 static int ogg_restore(AVFormatContext *s, int discard)
@@ -156,6 +165,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 +183,12 @@ 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);
+        av_freep(os);
+        return ret;
+    }
+
     if (new_avstream) {
         st = avformat_new_stream(s, NULL);
         if (!st)
@@ -191,6 +207,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 +295,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