diff --git a/libavformat/avio.c b/libavformat/avio.c
index 44bc3afeaf1b993ebd5b8ac924ed9142c95a07eb..0dd7e63f2a341c7a38dd563b31b29c17947ddf1f 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -154,7 +154,9 @@ offset_t url_filesize(URLContext *h)
     size= url_seek(h, 0, AVSEEK_SIZE);
     if(size<0){
         pos = url_seek(h, 0, SEEK_CUR);
-        size = url_seek(h, -1, SEEK_END)+1;
+        if ((size = url_seek(h, -1, SEEK_END)) < 0)
+            return size;
+        size++;
         url_seek(h, pos, SEEK_SET);
     }
     return size;