Skip to content

error when curl read func returns unexpected long#22757

Open
Sjord wants to merge 1 commit into
php:masterfrom
Sjord:curl-error-read-long
Open

error when curl read func returns unexpected long#22757
Sjord wants to merge 1 commit into
php:masterfrom
Sjord:curl-error-read-long

Conversation

@Sjord

@Sjord Sjord commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Raise a value error when the callback registered with CURLOPT_READFUNCTION returns an unexpected long.

The function registered with CURLOPT_READFUNCTION should return a string. PHP then writes that string to a buffer and returns the length, so that curl can read that many bytes from the buffer.

The function can also return CURL_READFUNC_ABORT and CURL_READFUNC_PAUSE, so it also supports returning longs. However, when it returns a long other than these two constants, it is interpreted as a length. PHP does not update the buffer, but does instruct curl it can read that many bytes from the buffer. It reads whatever uninitialized data that is in the buffer and sends it over the line to the server.

This seems bad, so validate the return value of the read function and raise an error.

Returning 0 is a bit of an edge case. It is not documented but does results in correct behavior (i.e. end-of-file). So we accept that, but don't advertise it as valid in the error message.

I am not worried about BC-break, because sending an uninitialized buffer does not result in a valid request, so this would already not work.

I considered silently casting the int to a string. This would be very "old PHP" behavior. I think throwing an error is more strict and better and more in line with "new PHP" behavior, if that makes sense.

Related to #10270

Raise a value error when the callback registered with
CURLOPT_READFUNCTION returns an unexpected long.

The function registered with CURLOPT_READFUNCTION should return a
string. PHP then writes that string to a buffer and returns the
length, so that curl can read that many bytes from the buffer.

The function can also return CURL_READFUNC_ABORT and
CURL_READFUNC_PAUSE, so it also supports returning longs. However, when
it returns a long other than these two constants, it is interpreted as a
length. PHP does not update the buffer, but does instruct curl it can
read that many bytes from the buffer. It reads whatever uninitialized
data that is in the buffer and sends it over the line to the server.

This seems bad, so validate the return value of the read function and
raise an error.

Returning 0 is a bit of an edge case. It is not documented but does
results in correct behavior (i.e. end-of-file). So we accept that, but
don't advertise it as valid in the error message.

I am not worried about BC-break, because sending an uninitialized buffer
does not result in a valid request, so this would already not work.

I considered silently casting the int to a string. This would be very
"old PHP" behavior. I think throwing an error is more strict and better
and more in line with "new PHP" behavior, if that makes sense.

Related to php#10270
@Sjord
Sjord requested a review from adoy as a code owner July 16, 2026 08:41
@Sjord

Sjord commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

@nicolas-grekas Could you review this?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant