Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #9872
For humans
Problem
Google\Cloud\Datastore\Key::determineIdentifierType()previously relied onis_numeric()to decide whether an identifier should be inferred asKey::TYPE_ID(int64) orKey::TYPE_NAME(string).is_numeric()returnstruefor values that cannot be represented as a signed 64-bit integer in Protobuf (PathElement.id), including:'13975337727204308302793171870910')md5('240610708') === '0e462097431906509019562988736854')'12.34')Passing such values without an explicit
identifierTypecaused a runtime crash during gRPC serialization (Google\Protobuf\Internal\Message::__construct(): Cannot convert '...' to integer).Solution
is_numeric()check inKey::determineIdentifierType()with a strict signed 64-bit integer check (Key::isInt64()), matching onlyintvalues or base-10 integer strings within[-9223372036854775808, 9223372036854775807].Key::TYPE_NAME.Datastore/tests/Unit/KeyTest.phpcoveringint64boundaries, overflowing numeric strings, scientific notation, float strings, and standard integer/string IDs.Why not
filter_var($identifier, FILTER_VALIDATE_INT)?FILTER_VALIDATE_INTvalidates againstPHP_INT_MAX/PHP_INT_MINfor the current platform. On 32-bit PHP (PHP_INT_SIZE === 4), standard 16-digit Datastore auto-allocated IDs (e.g.'5629499534213120') exceed 32-bitPHP_INT_MAX(2147483647) and would be misclassified asKey::TYPE_NAME.FILTER_VALIDATE_INTalso trims whitespace and acceptsbool/+prefixes unless extra guards are added. A small string-basedisInt64()check avoids all of these edge cases and works identically on both 32-bit and 64-bit PHP with no extra extensions.Verification
Datastoreunit test suite (vendor/bin/phpunit tests/Unit):OK (362 tests, 918 assertions).For agents
Datastore/src/Key.php: replacedis_numeric($identifier)inKey::determineIdentifierType()withKey::isInt64($identifier)(works identically on 32-bit and 64-bit PHP without extra extensions).Datastore/tests/Unit/KeyTest.php: addedtestInferredIdentifierType()withinferredIdentifierTypeProvider()data provider.filter_var(..., FILTER_VALIDATE_INT)was intentionally avoided because it bounds-checks against platformPHP_INT_MAX(breaking 16-digit Datastoreint64IDs on 32-bit PHP) and accepts whitespace /bool/+prefixes without additional guards.intand base-10stringwithin[-9223372036854775808, 9223372036854775807]) continue to inferKey::TYPE_ID.identifierTypeoverrides (Key::TYPE_ID/Key::TYPE_NAME) are unaffected.int64conversion now inferKey::TYPE_NAME.