improve program flow - relates to #353

Tue, 16 Jan 2024 23:43:27 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 16 Jan 2024 23:43:27 +0100
changeset 813
aba6d37b78bd
parent 812
fda0f1910d7e
child 814
5f9e07d3dd6c

improve program flow - relates to #353

src/printf.c file | annotate | diff | comparison | revisions
--- a/src/printf.c	Tue Jan 16 23:16:15 2024 +0100
+++ b/src/printf.c	Tue Jan 16 23:43:27 2024 +0100
@@ -144,27 +144,21 @@
     va_list ap2;
     va_copy(ap2, ap);
     int ret = vsnprintf(*str, len, fmt, ap);
-    if (ret < 0 || ((unsigned)ret) < len) {
-        va_end(ap2);
-        return ret;
-    } else {
+    if ((unsigned) ret >= len) {
         unsigned newlen = ret + 1;
         char *ptr = cxRealloc(alloc, *str, newlen);
         if (ptr) {
             int newret = vsnprintf(ptr, newlen, fmt, ap2);
-            va_end(ap2);
             if (newret < 0) {
                 cxFree(alloc, ptr);
-                return ret;
             } else {
                 *str = ptr;
-                return newret;
+                ret = newret;
             }
-        } else {
-            va_end(ap2);
-            return ret;
         }
     }
+    va_end(ap2);
+    return ret;
 }
 
 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) {
@@ -180,25 +174,19 @@
     va_copy(ap2, ap);
     int ret = vsnprintf(buf, len, fmt, ap);
     *str = buf;
-    if (ret < 0 || ((unsigned)ret) < len) {
-        va_end(ap2);
-        return ret;
-    } else {
+    if ((unsigned) ret >= len) {
         unsigned newlen = ret + 1;
         char *ptr = cxMalloc(alloc, newlen);
         if (ptr) {
             int newret = vsnprintf(ptr, newlen, fmt, ap2);
-            va_end(ap2);
             if (newret < 0) {
                 cxFree(alloc, ptr);
-                return ret;
             } else {
                 *str = ptr;
-                return newret;
+                ret = newret;
             }
-        } else {
-            va_end(ap2);
-            return ret;
         }
     }
+    va_end(ap2);
+    return ret;
 }

mercurial