diff --git a/libavformat/utils.c b/libavformat/utils.c
index 63a8616cbfa4ac6192b604a857a423e1d1215453..75819570e9bb9e8c270e478a1bd8a0ba80faf6e8 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2843,6 +2843,7 @@ int ff_alloc_extradata(AVCodecContext *avctx, int size)
     int ret;
 
     if (size < 0 || size >= INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
+        avctx->extradata = NULL;
         avctx->extradata_size = 0;
         return AVERROR(EINVAL);
     }