[libbluray-devel] str_printf(): Simplify. Return NULL if memory allocation fails.

hpi1 git at videolan.org
Tue Apr 28 12:41:48 CEST 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Tue Apr 28 13:09:21 2015 +0300| [92b346b5315b8b017b68b79c1f015f652cd1059d] | committer: hpi1

str_printf(): Simplify. Return NULL if memory allocation fails.

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=92b346b5315b8b017b68b79c1f015f652cd1059d
---

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

diff --git a/src/util/strutl.c b/src/util/strutl.c
index 39c772d..b9f02bb 100644
--- a/src/util/strutl.c
+++ b/src/util/strutl.c
@@ -40,8 +40,15 @@ char *str_printf(const char *fmt, ...)
     int     size = 100;
     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);
@@ -57,12 +64,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;
     }
 }
 



More information about the libbluray-devel mailing list