From 05cd0bca812be7c275896d852c99df9a3a33096b Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 22 Jun 2013 19:36:42 +0200 Subject: Don't mutate input buffer for ass_read_memory() Fixes google code issue #88. process_text() garbles the memory as the text is parsed. This also fixes that the user provided buffer implicitly had to be null terminated. This wasn't obvious, because the caller passes in the buffer length. libass ignored the buffer length (unless a codepage was provided and iconv was enabled), and happily read past the end of the buffer. It would be much nicer if the parsing code would be fixed, instead of just copying the input buffer. Maybe one day. --- libass/ass.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libass/ass.c b/libass/ass.c index 6505892..ad37606 100644 --- a/libass/ass.c +++ b/libass/ass.c @@ -1117,7 +1117,7 @@ ASS_Track *ass_read_memory(ASS_Library *library, char *buf, size_t bufsize, char *codepage) { ASS_Track *track; - int need_free = 0; + int copied = 0; if (!buf) return 0; @@ -1128,12 +1128,19 @@ ASS_Track *ass_read_memory(ASS_Library *library, char *buf, if (!buf) return 0; else - need_free = 1; + copied = 1; } #endif + if (!copied) { + char *newbuf = malloc(bufsize + 1); + if (!newbuf) + return 0; + memcpy(newbuf, buf, bufsize); + newbuf[bufsize] = '\0'; + buf = newbuf; + } track = parse_memory(library, buf); - if (need_free) - free(buf); + free(buf); if (!track) return 0; -- cgit v1.2.3