libsystem: strv: avoid memory leak by realloc
authorWaLyong Cho <walyong.cho@samsung.com>
Tue, 8 Nov 2016 10:27:01 +0000 (19:27 +0900)
committerWaLyong Cho <walyong.cho@samsung.com>
Tue, 8 Nov 2016 10:29:57 +0000 (19:29 +0900)
In str_to_strv(), during interation the strings if allocation is failed
after second chance, the allocated origin value v is leaked.
And recent gcc is safe for null free. So destroy value without check.

Change-Id: I7fda4d76484201b2ccb66f5617ca75d0e4ecb1aa
Signed-off-by: WaLyong Cho <walyong.cho@samsung.com>
src/libsystem/libsystem.c

index 0159c03..f7aa2e3 100644 (file)
@@ -731,14 +731,14 @@ int str_to_strv(const char *str, char ***strv, const char *separator) {
         FOREACH_WORD_SEPARATOR(w, l, str, separator, state) {
                 p = strndup(w, l);
                 if (!p) {
-                        if (v)
-                                free(v);
+                        free(v);
                         return -ENOMEM;
                 }
 
                 new = (char **)realloc(v, sizeof(char *) * (i + 2));
                 if (!new) {
                         free(p);
+                        free(v);
                         p = NULL;
                         return -ENOMEM;
                 }