libaacs | branch: master | npzacs <npz...@gmail.com> | Tue Apr 28 13:05:06 2015 
+0300| [28af032c8b85461c611f959f5ea1a18af4022ccb] | committer: npzacs

str_printf(): return NULL if memory allocation fails

> http://git.videolan.org/gitweb.cgi/libaacs.git/?a=commit;h=28af032c8b85461c611f959f5ea1a18af4022ccb
---

 src/util/strutl.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/util/strutl.c b/src/util/strutl.c
index 88055af..ae55b59 100644
--- a/src/util/strutl.c
+++ b/src/util/strutl.c
@@ -164,10 +164,17 @@ char *str_printf(const char *fmt, ...)
     va_list ap;
     int     len;
     int     size = 100;
-    char   *tmp, *str;
+    char   *tmp, *str = NULL;
 
-    str = malloc(size);
     while (1) {
+
+        tmp = realloc(str, size);
+        if (tmp == NULL) {
+            X_FREE(str);
+            return NULL;
+        }
+        str = tmp;
+
         /* Try to print in the allocated space. */
         va_start(ap, fmt);
         len = vsnprintf(str, size, fmt, ap);
@@ -183,12 +190,6 @@ char *str_printf(const char *fmt, ...)
             size = len+1; /* precisely what is needed */
         else           /* glibc 2.0 */
             size *= 2;  /* twice the old size */
-
-        tmp = realloc(str, size);
-        if (tmp == NULL) {
-            return str;
-        }
-        str = tmp;
     }
 }
 

_______________________________________________
libaacs-devel mailing list
libaacs-devel@videolan.org
https://mailman.videolan.org/listinfo/libaacs-devel

Reply via email to