diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index 70c7817fa8205258cbef4e57b561b1542c2263c3..8f8a08f1bdee6b7a3aa6321a6e5d93026a730231 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -444,6 +444,8 @@ static int ogg_write_header(AVFormatContext *s)
             return -1;
         }
         oggstream = av_mallocz(sizeof(*oggstream));
+        if (!oggstream)
+            return AVERROR(ENOMEM);
         oggstream->page.stream_index = i;
 
         if (!(s->flags & AVFMT_FLAG_BITEXACT))