diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
index 788562aa81db53dbbbf4f722c875f1f8cae79377..92843df2581b2698f160e2f2a58773aed1383692 100644
--- a/libavformat/hlsproto.c
+++ b/libavformat/hlsproto.c
@@ -121,8 +121,10 @@ static int parse_playlist(URLContext *h, const char *url)
         return ret;
 
     read_chomp_line(in, line, sizeof(line));
-    if (strcmp(line, "#EXTM3U"))
-        return AVERROR_INVALIDDATA;
+    if (strcmp(line, "#EXTM3U")) {
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
+    }
 
     free_segment_list(s);
     s->finished = 0;