diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index ec5a46409acbed7545ddad83fdfc0e6571256a60..1924f364b6f13be4d491a239aef81849169a5ac2 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -995,6 +995,9 @@ int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
 {
     AVCodecContext *avctx = h->s.avctx;
 
+    if(!buf || size <= 0)
+        return -1;
+
     if(buf[0] == 1){
         int i, cnt, nalsize;
         const unsigned char *p = buf;