Skip to content

sapi: Follow-up for post-merge comments on GH-23386 - #26006

Open
NattyNarwhal wants to merge 6 commits into
php:masterfrom
NattyNarwhal:followup-gh23386
Open

NattyNarwhal wants to merge 6 commits into
php:masterfrom
NattyNarwhal:followup-gh23386

Conversation

@NattyNarwhal

Copy link
Copy Markdown
Member

Addressing comments from @ndossche

Input filters that didn't change the length would result in an empty
string.
Note that even before 25c9b84,
lsphp leaked:

```
[Tue Dec  1 16:19:57 2026]  Script:  '-'
/Users/calvin/src/php-src/main/SAPI.c(294) :  Freeing 0x0000000108c01200 (25 bytes), script=-
[Tue Dec  1 16:19:57 2026]  Script:  '-'
/Users/calvin/src/php-src/Zend/zend_ini.c(382) :  Freeing 0x0000000108c02000 (56 bytes), script=-
[Tue Dec  1 16:19:57 2026]  Script:  '-'
/Users/calvin/src/php-src/Zend/zend_hash.c(191) :  Freeing 0x0000000108c03000 (320 bytes), script=-
[Tue Dec  1 16:19:57 2026]  Script:  '-'
/Users/calvin/src/php-src/Zend/zend_string.h(449) :  Freeing 0x0000000108c07000 (39 bytes), script=-
[Tue Dec  1 16:19:57 2026]  Script:  '-'
/Users/calvin/src/php-src/Zend/zend_llist.c(35) :  Freeing 0x0000000108c07050 (39 bytes), script=-
=== Total 5 memory leaks detected ===
```
Comment thread main/SAPI.c
/* XXX: pretty ugly because input filters aren't zend_string yet. */
char *tmp = estrdup(ZSTR_VAL(value));
size_t tmp_len = 0;
size_t tmp_len = ZSTR_LEN(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose there cannot be a NUL byte in value? In that case, you can optimize the above estrdup call by using estrndup and tmp_len

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there can be, since all the SAPIs currently call strlen for the length when making zend_strings and prior to the zend_stringification of sapi getenv, it called estrdup anyways.

return fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
int ret = fn_chain[0](&ctx, (fn_activate_user_ini_chain_t*)(fn_chain + 1));
if (ctx.doc_root) {
zend_string_release(ctx.doc_root);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what t he lifetime management/ownership rules are here, but it feels icky to release this and not set this to NULL, especially with the NULL check in this branch

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx should get deallocated end of scope here. doc_root might not be allocated from lsapi_activate_user_ini_basic_checks, but in that case, we should probably zero-init ctx...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants