]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: tools: vsnprintf() is not always reliable on Solaris
authorWilly Tarreau <w@1wt.eu>
Mon, 1 Apr 2013 20:48:54 +0000 (22:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 1 Apr 2013 20:58:28 +0000 (22:58 +0200)
Seen on Solaris 8, calling vsnprintf() with a null-size results
in the output size not being computed. This causes some random
behaviour including crashes when trying to display error messages
when loading an invalid configuration.

src/standard.c

index 37b3c436e3fb0f21ced5d4158c98dda1cbcf8291..adeb335fff89eb49e88cc43ec92e951c524dabee 100644 (file)
@@ -1900,13 +1900,21 @@ char *memprintf(char **out, const char *format, ...)
                 * intermediate evaluations get wrong.
                 */
                va_start(args, format);
-               needed = vsnprintf(ret, allocated, format, args) + 1;
+               needed = vsnprintf(ret, allocated, format, args);
                va_end(args);
 
-               if (needed <= allocated)
-                       break;
+               if (needed < allocated) {
+                       /* Note: on Solaris 8, the first iteration always
+                        * returns -1 if allocated is zero, so we force a
+                        * retry.
+                        */
+                       if (!allocated)
+                               needed = 0;
+                       else
+                               break;
+               }
 
-               allocated = needed;
+               allocated = needed + 1;
                ret = realloc(ret, allocated);
        } while (ret);