diff --git a/libavformat/avio.c b/libavformat/avio.c
index dc40384fc504588116593e693e5b6b57ea7726ee..44bc3afeaf1b993ebd5b8ac924ed9142c95a07eb 100644
--- a/libavformat/avio.c
+++ b/libavformat/avio.c
@@ -70,11 +70,14 @@ int url_open(URLContext **puc, const char *filename, int flags)
     err = -ENOENT;
     goto fail;
  found:
-    uc = av_malloc(sizeof(URLContext) + strlen(filename));
+    uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
     if (!uc) {
         err = -ENOMEM;
         goto fail;
     }
+#if LIBAVFORMAT_VERSION_INT >= (52<<16)
+    uc->filename = (char *) &uc[1];
+#endif
     strcpy(uc->filename, filename);
     uc->prot = up;
     uc->flags = flags;
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 3f733f0a71334540af111ec99bffbf7b19c5cea5..f44d52694fa3b21e5f4331e2a784620d4f7ae4d6 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -33,7 +33,11 @@ struct URLContext {
     int is_streamed;  /* true if streamed (no seek possible), default = false */
     int max_packet_size;  /* if non zero, the stream is packetized with this max packet size */
     void *priv_data;
+#if LIBAVFORMAT_VERSION_INT >= (52<<16)
+    char *filename; /* specified filename */
+#else
     char filename[1]; /* specified filename */
+#endif
 };
 
 typedef struct URLContext URLContext;