Skip to content

Commit 75dc448

Browse files
committed
Make s(tr)pprintf infallible
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.
1 parent daba578 commit 75dc448

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

main/spprintf.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,6 @@ static void xbuf_format_converter(void *xbuf, zend_bool is_char, const char *fmt
838838
PHPAPI size_t vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
839839
{
840840
smart_string buf = {0};
841-
size_t result;
842841

843842
/* since there are places where (v)spprintf called without checking for null,
844843
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
855854

856855
if (buf.c) {
857856
*pbuf = buf.c;
858-
result = buf.len;
857+
return buf.len;
859858
} else {
860-
*pbuf = NULL;
861-
result = 0;
859+
*pbuf = estrndup("", 0);
860+
return 0;
862861
}
863-
864-
return result;
865862
}
866863
/* }}} */
867864

@@ -883,11 +880,15 @@ PHPAPI zend_string *vstrpprintf(size_t max_len, const char *format, va_list ap)
883880

884881
xbuf_format_converter(&buf, 0, format, ap);
885882

886-
if (max_len && buf.s && ZSTR_LEN(buf.s) > max_len) {
883+
if (!buf.s) {
884+
return ZSTR_EMPTY_ALLOC();
885+
}
886+
887+
if (max_len && ZSTR_LEN(buf.s) > max_len) {
887888
ZSTR_LEN(buf.s) = max_len;
888889
}
889-
smart_str_0(&buf);
890890

891+
smart_str_0(&buf);
891892
return buf.s;
892893
}
893894
/* }}} */

0 commit comments

Comments
 (0)