diff mbox

[FFmpeg-devel,1/4] avcodec/htmlsubtitles: Check for string truncation and return error

Message ID 20170506001050.23234-1-michael@niedermayer.cc
State Accepted
Commit f4ae3cce64bd46b1d539bdeac39753f83015f114
Headers show

Commit Message

Michael Niedermayer May 6, 2017, 12:10 a.m. UTC
Fixes out of array access
Fixes: 1354/clusterfuzz-testcase-minimized-5520132195483648

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/targets/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/htmlsubtitles.c | 12 +++++++++---
 libavcodec/htmlsubtitles.h |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

Comments

Michael Niedermayer May 8, 2017, 2:44 p.m. UTC | #1
On Sat, May 06, 2017 at 02:10:47AM +0200, Michael Niedermayer wrote:
> Fixes out of array access
> Fixes: 1354/clusterfuzz-testcase-minimized-5520132195483648
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/targets/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/htmlsubtitles.c | 12 +++++++++---
>  libavcodec/htmlsubtitles.h |  2 +-
>  2 files changed, 10 insertions(+), 4 deletions(-)

patchset applied

[...]
diff mbox

Patch

diff --git a/libavcodec/htmlsubtitles.c b/libavcodec/htmlsubtitles.c
index 8b57febd26..16295daa0c 100644
--- a/libavcodec/htmlsubtitles.c
+++ b/libavcodec/htmlsubtitles.c
@@ -46,11 +46,12 @@  typedef struct SrtStack {
 
 static void rstrip_spaces_buf(AVBPrint *buf)
 {
-    while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
-        buf->str[--buf->len] = 0;
+    if (av_bprint_is_complete(buf))
+        while (buf->len > 0 && buf->str[buf->len - 1] == ' ')
+            buf->str[--buf->len] = 0;
 }
 
-void ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
+int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
 {
     char *param, buffer[128], tmp[128];
     int len, tag_close, sptr = 1, line_start = 1, an = 0, end = 0;
@@ -171,8 +172,13 @@  void ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in)
             line_start = 0;
     }
 
+    if (!av_bprint_is_complete(dst))
+        return AVERROR(ENOMEM);
+
     while (dst->len >= 2 && !strncmp(&dst->str[dst->len - 2], "\\N", 2))
         dst->len -= 2;
     dst->str[dst->len] = 0;
     rstrip_spaces_buf(dst);
+
+    return 0;
 }
diff --git a/libavcodec/htmlsubtitles.h b/libavcodec/htmlsubtitles.h
index e10cdda241..f3a8ef5d8b 100644
--- a/libavcodec/htmlsubtitles.h
+++ b/libavcodec/htmlsubtitles.h
@@ -23,6 +23,6 @@ 
 
 #include "libavutil/bprint.h"
 
-void ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in);
+int ff_htmlmarkup_to_ass(void *log_ctx, AVBPrint *dst, const char *in);
 
 #endif /* AVCODEC_HTMLSUBTITLES_H */