diff --git a/ffmpeg.c b/ffmpeg.c
index 6098422375..faeb2f5c3a 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3366,7 +3366,8 @@ static int transcode_init(void)
             if ((ist = get_input_stream(ost)))
                 dec = ist->st->codec;
             if (dec && dec->subtitle_header) {
-                ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
+                /* ASS code assumes this buffer is null terminated so add extra byte. */
+                ost->st->codec->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
                 if (!ost->st->codec->subtitle_header) {
                     ret = AVERROR(ENOMEM);
                     goto dump_format;
diff --git a/libavcodec/ass_split.c b/libavcodec/ass_split.c
index a0b72542c7..81d501eefe 100644
--- a/libavcodec/ass_split.c
+++ b/libavcodec/ass_split.c
@@ -352,8 +352,10 @@ void ff_ass_split_free(ASSSplitContext *ctx)
 {
     if (ctx) {
         int i;
-        for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++)
+        for (i=0; i<FF_ARRAY_ELEMS(ass_sections); i++) {
             free_section(ctx, &ass_sections[i]);
+            av_freep(&(ctx->field_order[i]));
+        }
         av_free(ctx);
     }
 }