protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

[PHP]: GPBUtil::checkFloat modifies the passed-in variable

Open bshaffer opened this issue 1 year ago • 5 comments

From @SVIllette in https://github.com/googleapis/google-cloud-php/issues/7045. I am moving it here because it describes an issue in Google\Protobuf\Internal\GPBUtil. I was able to verify this is happening in the native PHP library and when the protobuf C extension is enabled.

To me this seems like a bug, as I don't know why a method called checkFloat should pass by reference / modify the behavior of the variable being checked. However, it looks like it was done quite intentionally

https://github.com/protocolbuffers/protobuf/blob/06e65a29de31efc65d4ab4e07b19c1636fc45d9c/php/src/Google/Protobuf/Internal/GPBUtil.php#L146-L153

This requires further investigation.

**** copied from https://github.com/googleapis/google-cloud-php/issues/7045 ****

Description

Hi guys, I just installed this package and noticed a strange behaviour when an object of class Google\Cloud\RecaptchaEnterprise\V1\RiskAnalysis is hydrated from the json response when creating an assessment (especially the score property). I discovered this bug when I tested recaptcha behaviour with a score threshold of 0.9 and it failed because the score was 0.8999999761581421.

Environment details

  • OS: MacOS
  • PHP version: 8.2
  • Package name and version: googleapis/google-cloud-php-recaptcha-enterprise v1.8.1

Code example

$var = 0.9;
GPBUtil::checkFloat($var);
var_dump($var);

$var = 0.7;
GPBUtil::checkFloat($var);
var_dump($var);

$var = 0.3;
GPBUtil::checkFloat($var);
var_dump($var);

$var = 0.1;
GPBUtil::checkFloat($var);
var_dump($var);

The output is the following : (from https://3v4l.org/CgGYZ)

8.0 - 8.3
float(0.8999999761581421) 
float(0.699999988079071) 
float(0.30000001192092896) 
float(0.10000000149011612)
5.4 - 8.0
float(0.89999997615814)
float(0.69999998807907)
float(0.30000001192093)
float(0.10000000149012)

bshaffer avatar Jul 15 '24 14:07 bshaffer

@bshaffer - past you appears to be the author of said intentional line of code. See https://github.com/protocolbuffers/protobuf/commit/468bc193ec155756bccf2b2d9eeb8e003d95ce31. Introduced in https://github.com/protocolbuffers/protobuf/pull/8187

JasonLunn avatar Oct 03 '24 22:10 JasonLunn

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Jan 02 '25 10:01 github-actions[bot]

Please keep this issue opened

SVillette avatar Jan 07 '25 09:01 SVillette

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Apr 08 '25 10:04 github-actions[bot]

Please keep this issue opened

SVillette avatar Apr 08 '25 12:04 SVillette

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Sep 20 '25 10:09 github-actions[bot]

This issue is active

bshaffer avatar Sep 20 '25 19:09 bshaffer