[vlc-commits] STL: simplify and fix memory leak

Rémi Denis-Courmont git at videolan.org
Wed Apr 23 22:35:07 CEST 2014


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Apr 23 23:34:45 2014 +0300| [28132698012fab266d81de5b091d8b16e7906e3a] | committer: Rémi Denis-Courmont

STL: simplify and fix memory leak

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=28132698012fab266d81de5b091d8b16e7906e3a
---

 modules/codec/stl.c |   51 ++++++++++++++++-----------------------------------
 1 file changed, 16 insertions(+), 35 deletions(-)

diff --git a/modules/codec/stl.c b/modules/codec/stl.c
index 46b2b6d..dbf2ae9 100644
--- a/modules/codec/stl.c
+++ b/modules/codec/stl.c
@@ -78,49 +78,30 @@ static cct_number_t cct_nums[] = { {CCT_ISO_6937_2, "ISO_6937-2"},
                                    {CCT_ISO_8859_8, "ISO_8859-8"} };
 
 
-static char *ParseText(uint8_t *data, int size, const char *charset)
+static char *ParseText(const uint8_t *data, size_t size, const char *charset)
 {
-    char *text = strdup("");
-    int  text_size = 0;
+    char *text = malloc(size);
+    if (text == NULL)
+        return NULL;
+
+    size_t text_size = 0;
 
-    for (int i = 0; i < size; i++) {
+    for (size_t i = 0; i < size; i++) {
         uint8_t code = data[i];
 
         if (code == 0x8f)
             break;
-
-        char tmp[16] = "";
-        char *t = tmp;
-        if ((code >= 0x20 && code <= 0x7e) ||
-            (code >= 0xa0) )
-            snprintf(tmp, sizeof(tmp), "%c", code);
-#if 0
-        else if (code == 0x80)
-            snprintf(tmp, sizeof(tmp), "<i>");
-        else if (code == 0x81)
-            snprintf(tmp, sizeof(tmp), "</i>");
-        else if (code == 0x82)
-            snprintf(tmp, sizeof(tmp), "<u>");
-        else if (code == 0x83)
-            snprintf(tmp, sizeof(tmp), "</u>");
-#endif
-        else if (code == 0x8a)
-            snprintf(tmp, sizeof(tmp), "\n");
-        else {
-            t = NULL;
-        }
-
-        if (!t)
-            continue;
-        size_t t_size = strlen(t);
-        text = realloc_or_free(text, t_size + text_size + 1);
-        if (!text)
+        if (code == 0x7f)
             continue;
-        memcpy(&text[text_size], t, t_size);
-        text_size += t_size;
-        text[text_size]   = '\0';
+        if (code & 0x60)
+            text[text_size++] = code;
+        if (code == 0x8a)
+            text[text_size++] = '\n';
     }
-    return FromCharset(charset, text, text_size);
+
+    char *u8 = FromCharset(charset, text, text_size);
+    free(text);
+    return u8;
 }
 
 static subpicture_t *Decode(decoder_t *dec, block_t **block)



More information about the vlc-commits mailing list