Fix PHP non-conformance when parsing JSON booleans into float/double fields. In https://github.com/protocolbuffers/protobuf/commit/cb440834fde94f4fa2433fdb71b6366d7b1713a9 we discovered an issue where boolean inputs can be parsed into float fields in PHP due to a missing is_numeric check. (Note that this is_numeric check is present for int fields). This is very unintended, and this is a behavioral fix. PiperOrigin-RevId: 952923362
diff --git a/conformance/failure_list_php.txt b/conformance/failure_list_php.txt index 6e54a83..c9a32d8 100644 --- a/conformance/failure_list_php.txt +++ b/conformance/failure_list_php.txt
@@ -47,12 +47,8 @@ Required.*.DurationProtoNanosWrongSignNegativeSecs.JsonOutput # Should have failed to serialize, but didn't. Required.*.TimestampProtoNanoTooLarge.JsonOutput # Should have failed to serialize, but didn't. Required.*.TimestampProtoNegativeNanos.JsonOutput # Should have failed to serialize, but didn't. -Required.*.JsonInput.SingleValueForRepeatedFieldInt32 # Should have failed to parse, but didn't. +Required.*.JsonInput.SingleValueForRepeatedFieldInt32 # Should have failed to parse, but didn't. Required.*.ProtobufInput.BadTag_FieldNumberTooHigh # Should have failed to parse, but didn't. Required.*.ProtobufInput.BadTag_FieldNumberSlightlyTooHigh # Should have failed to parse, but didn't. Required.*.ProtobufInput.BadTag_OverlongVarint # Should have failed to parse, but didn't. Required.*.ProtobufInput.BadTag_VarintMoreThanTenBytes # Should have failed to parse, but didn't. -Required.*.JsonInput.DoubleFieldFalseValue # Should have failed to parse, but didn't. -Required.*.JsonInput.DoubleFieldTrueValue # Should have failed to parse, but didn't. -Required.*.JsonInput.FloatFieldFalseValue # Should have failed to parse, but didn't. -Required.*.JsonInput.FloatFieldTrueValue # Should have failed to parse, but didn't.
diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 892456d..b4ec5ea 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php
@@ -932,6 +932,10 @@ if ($value === "NaN") { return NAN; } + if (!is_numeric($value)) { + throw new GPBDecodeException( + "Invalid data type for float field"); + } return $value; case GPBType::INT32: case GPBType::SINT32: