diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 5ea9d216a4aa5c98abc183151b2a46da68f19fac..b571772f606b22bdf8ec06436a968b9d72e2981e 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -1455,7 +1455,7 @@ static int hls_write_header(AVFormatContext *s)
             if (basename_size > 0) {
                 hls->base_output_dirname = av_malloc(basename_size);
             } else {
-                hls->base_output_dirname = av_malloc(strlen(hls->fmp4_init_filename));
+                hls->base_output_dirname = av_malloc(strlen(hls->fmp4_init_filename) + 1);
             }
             if (!hls->base_output_dirname) {
                 ret = AVERROR(ENOMEM);