Improve curl_init() return type analysis #3346
Merged
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
I got annoyed by the failing e2e/php8 test (https://github.com/phpstan/phpstan/blob/1.12.x/e2e/php8/test.php#L11, https://phpstan.org/r/32a5c25f-c266-4c04-9bf1-2ec0562397d8), so I wanted to resolve it by restoring the expected issue there. (But only now do I notice that this doesn't help that at all - I completely misunderstood the issue there :P)
Following, phpstan/phpstan#1274, phpstan/phpstan#1465, phpstan/phpstan#1484 I looked into
curl_init()
a bit more.Here's my analysis: (with verified behavior across all latest PHP minor versions and libcurl 7.10.5 + 8.9.1)
One important point to start with, if we presume that
curl_init()
without arguments never returnsfalse
, we generally ignore initialization errors or the case where curl fails to allocate more memory.curl_init()
with a string argument will only return false if it fails to setCURLOPT_URL
with ithttps://github.com/php/php-src/blob/php-8.4.0beta3/ext/curl/interface.c#L1154-L1159
CURLOPT_URL
will fail with a value error on the PHP side if the URL contains a null bytehttps://github.com/php/php-src/blob/php-8.4.0beta3/ext/curl/interface.c#L139
https://github.com/php/php-src/blob/php-8.0.30/ext/curl/interface.c#L104-L107
CURLOPT_URL
will fail with a warning (false
return) on the PHP side if the URL contains a null bytehttps://github.com/php/php-src/blob/php-7.4.33/ext/curl/interface.c#L112-L115
But, if open_basedir is enabled, it will also fail when the URL is invalid or uses the
file
schemehttps://github.com/php/php-src/blob/php-7.4.33/ext/curl/interface.c#L146-L155
https://github.com/curl/curl/blob/curl-8_9_1/lib/setopt.c#L3256
https://github.com/curl/curl/blob/curl-8_9_1/lib/setopt.c#L1470
https://github.com/curl/curl/blob/curl-8_9_1/lib/setopt.c#L70-L71