Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS: don't use assignments in conditions #411

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ protected static function parse_response($headers, $url, $req_headers, $req_data
$return->url = $url;

if (!$options['filename']) {
if (($pos = strpos($headers, "\r\n\r\n")) === false) {
$pos = strpos($headers, "\r\n\r\n");
if ($pos === false) {
// Crap!
throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
}
Expand Down Expand Up @@ -826,17 +827,30 @@ public static function decompress($data) {
return $data;
}

if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) {
return $decoded;
if (function_exists('gzdecode')) {
$decoded = @gzdecode($data);
if ($decoded !== false) {
Comment on lines +830 to +832
Copy link
Member

Choose a reason for hiding this comment

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

This code not exactly the same. In the original code, the && operator will cause the result of $decoded = @gzdecode($data) to be cast as a bool before being compared to false.

In the changed code, this casting doesn't happen, and considering we do a strict comparison, it would for example have a different result when $decoded = @gzdecode($data) would return null.

Now, according to the docs, gzdecode() would only ever return false on failure. But I'm unsure what it would return if you successfully decode a document of zero length. Would it return null then?

Copy link
Member

Choose a reason for hiding this comment

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

I think the new version is actually more correct, but it would be a BC.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now, according to the docs, gzdecode() would only ever return false on failure. But I'm unsure what it would return if you successfully decode a document of zero length. Would it return null then?

Basing myself also on the documentation, I would expect it to return an empty string in that case as the standard return type is a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

This code not exactly the same. In the original code, the && operator will cause the result of $decoded = @gzdecode($data) to be cast as a bool before being compared to false.

Not sure if that would ever make a difference, so I'm not convinced this is a BC-break.
To proof my point: https://3v4l.org/OAoLN

return $decoded;
}
}
elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) {
return $decoded;

if (function_exists('gzinflate')) {
$decoded = @gzinflate($data);
if ($decoded !== false) {
Comment on lines +837 to +839
Copy link
Member

Choose a reason for hiding this comment

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

This code is changing as well because of the stripped cast to bool.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same thing.

return $decoded;
}
}
elseif (($decoded = self::compatible_gzinflate($data)) !== false) {

$decoded = self::compatible_gzinflate($data);
if ($decoded !== false) {
return $decoded;
}
elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) {
return $decoded;

if (function_exists('gzuncompress')) {
$decoded = @gzuncompress($data);
if ($decoded !== false) {
Comment on lines +849 to +851
Copy link
Member

Choose a reason for hiding this comment

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

This code is changing as well because of the stripped cast to bool.

return $decoded;
}
}

return $data;
Expand Down Expand Up @@ -910,7 +924,8 @@ public static function compatible_gzinflate($gzData) {
}

if ($huffman_encoded) {
if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
$decompressed = @gzinflate(substr($gzData, 2));
if (false !== $decompressed) {
return $decompressed;
}
}
Expand All @@ -937,20 +952,23 @@ public static function compatible_gzinflate($gzData) {
// Determine the first byte of data, based on the above ZIP header
// offsets:
$first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4)));
if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) {
$decompressed = @gzinflate(substr($gzData, 30 + $first_file_start));
if (false !== $decompressed) {
return $decompressed;
}
return false;
}

// Finally fall back to straight gzinflate
if (false !== ($decompressed = @gzinflate($gzData))) {
$decompressed = @gzinflate($gzData);
if (false !== $decompressed) {
return $decompressed;
}

// Fallback for all above failing, not expected, but included for
// debugging and preventing regressions and to track stats
if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
$decompressed = @gzinflate(substr($gzData, 2));
if (false !== $decompressed) {
return $decompressed;
}

Expand Down