You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
php_stream_memory_get_buffer() not zero-terminated #15628
It should be noted that while len stores the length without a trailing null byte, the actual string contents in val must always contain a trailing null byte.
The problem here is that php_stream_memory_get_buffer returns a zend_string which is not zero-terminated. Now, the question is whether that is a bug in php_stream_memory_get_buffer(), or whether zend_strings are always zero-terminated, unless they aren't.
A possible fix for php_stream_memory_get_buffer() might be:
But looking at the implementation of php_stream_memory_write() (which is called by php_stream_putc() among others), I suspect a lot of reallocation happening, so this new string initialization might not make that much of a difference. I wonder whether we should use a smart_str instead for the buffer.
I like your last patch better (didn't check whether ZSTR_LEN will go out of bounds though), but I believe it should be something along those lines.
To smart_str or not to smart_str is a bit orthogonal, it's hard to say without a benchmark.
changed the title [-]php_stream_memory_get_buffer returns a zend_string which is not zero-terminated[/-][+]php_stream_memory_get_buffer() not zero-terminated[/+]on Aug 29, 2024
Activity
ndossche commentedon Aug 29, 2024
Afaik zend_string should always be NUL terminated. It would be sad to have to init a new string though...
cmb69 commentedon Aug 29, 2024
Well, I guess we could also do:
But looking at the implementation of
php_stream_memory_write()(which is called byphp_stream_putc()among others), I suspect a lot of reallocation happening, so this new string initialization might not make that much of a difference. I wonder whether we should use asmart_strinstead for the buffer.ndossche commentedon Aug 29, 2024
I like your last patch better (didn't check whether ZSTR_LEN will go out of bounds though), but I believe it should be something along those lines.
To
smart_stror not tosmart_stris a bit orthogonal, it's hard to say without a benchmark.cmb69 commentedon Aug 29, 2024
See
php-src/Zend/zend_string.h
Lines 196 to 203 in 367f303
zend_string_alloc()is supposed to allocate at least one byte more than requested. I'll make a PR shortly.Right!
ndossche commentedon Aug 29, 2024
Well yes, I know this. But the reason why it is safe is here:
php-src/main/streams/memory.c
Line 58 in 367f303
because it uses the realloc of zend_string itself instead of something magical custom.
Great! Thanks.
Fix phpGH-15628: php_stream_memory_get_buffer() not zero-terminated
[-]php_stream_memory_get_buffer returns a zend_string which is not zero-terminated[/-][+]php_stream_memory_get_buffer() not zero-terminated[/+]Merge branch 'PHP-8.2' into PHP-8.3
Merge branch 'PHP-8.3'
Fix GH-15628: php_stream_memory_get_buffer() not zero-terminated