]> granicus.if.org Git - php/commitdiff
Make s(tr)pprintf infallible
authorNikita Popov <nikic@php.net>
Thu, 16 Jul 2015 21:17:29 +0000 (23:17 +0200)
committerNikita Popov <nikic@php.net>
Fri, 17 Jul 2015 14:53:06 +0000 (16:53 +0200)
spprintf now always creates a buffer and strpprintf always returns
a zend_string. Previously, if the result of the format happened to
be empty, the spprintf buffer would be set to NULL and strpprintf
would return NULL.

main/spprintf.c

index 759372b37053f0fd15ef334eac1fd8008cb6d3f0..4594fb7605ebd5fcf2c0d375fcbc2ddb6dc899e8 100644 (file)
@@ -838,7 +838,6 @@ skip_output:
 PHPAPI size_t vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
 {
        smart_string buf = {0};
-       size_t result;
 
        /* since there are places where (v)spprintf called without checking for null,
           a bit of defensive coding here */
@@ -855,13 +854,11 @@ PHPAPI size_t vspprintf(char **pbuf, size_t max_len, const char *format, va_list
 
        if (buf.c) {
                *pbuf = buf.c;
-               result = buf.len;
+               return buf.len;
        } else {
-               *pbuf = NULL;
-               result = 0;
+               *pbuf = estrndup("", 0);
+               return 0;
        }
-
-       return result;
 }
 /* }}} */
 
@@ -883,11 +880,15 @@ PHPAPI zend_string *vstrpprintf(size_t max_len, const char *format, va_list ap)
 
        xbuf_format_converter(&buf, 0, format, ap);
 
-       if (max_len && buf.s && ZSTR_LEN(buf.s) > max_len) {
+       if (!buf.s) {
+               return ZSTR_EMPTY_ALLOC();
+       }
+
+       if (max_len && ZSTR_LEN(buf.s) > max_len) {
                ZSTR_LEN(buf.s) = max_len;
        }
-       smart_str_0(&buf);
 
+       smart_str_0(&buf);
        return buf.s;
 }
 /* }}} */