diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index 7e32e439ea1eeeaa6ef5e16d7cabdaf6b835510b..2ad8cb3a8bd8ed10c51b20344fcaa298bcd1e342 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -532,11 +532,16 @@ static int nsv_read_header(AVFormatContext *s)
     for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) {
         if (nsv_resync(s) < 0)
             return -1;
-        if (nsv->state == NSV_FOUND_NSVF)
+        if (nsv->state == NSV_FOUND_NSVF) {
             err = nsv_parse_NSVf_header(s);
+            if (err < 0)
+                return err;
+        }
             /* we need the first NSVs also... */
         if (nsv->state == NSV_FOUND_NSVS) {
             err = nsv_parse_NSVs_header(s);
+            if (err < 0)
+                return err;
             break; /* we just want the first one */
         }
     }