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

fix for big int-ranges #934

Closed
wants to merge 2 commits into from
Closed

fix for big int-ranges #934

wants to merge 2 commits into from

Conversation

voku
Copy link
Contributor

@voku voku commented Jan 21, 2022

fix issue #6375

@staabm
Copy link
Contributor

staabm commented Jan 24, 2022

Could you add a failing unit test?

@voku
Copy link
Contributor Author

voku commented Jan 24, 2022

@staabm If I try to add a test like this ...

		$builder = ConstantArrayTypeBuilder::createFromConstantArray(
			new ConstantArrayType(
				[new ConstantIntegerType(0), new ConstantIntegerType(1)],
				[new StringType(), new StringType()],
			),
		);
		$builder->setOffsetValueType(IntegerRangeType::fromInterval(-2147483648, 2147483647), new StringType());

... I see I exception from phpunit itself:

The data provider specified for PHPStan\PhpDoc\TypeDescriptionTest::testParsingDesiredTypeDescription is invalid.
ValueError: The supplied range exceeds the maximum array size: start=-2147483648 end=2147483647

EDIT: test added and added a missing "-" before min-range self::ARRAY_COUNT_LIMIT :-P

@@ -104,14 +104,17 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
if (count($integerRanges) > 0) {
foreach ($integerRanges as $integerRange) {
if ($integerRange->getMin() === null) {
$minRange = $integerRange->getMin();
if ($minRange === null || $minRange <= -self::ARRAY_COUNT_LIMIT) {
Copy link
Contributor

@staabm staabm Jan 24, 2022

Choose a reason for hiding this comment

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

I dont think the fix is correct.

It reads like a range from a value smaller then -256 would no longer be possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't possible before anyway, see next lines of code ... if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {...

Copy link
Contributor

Choose a reason for hiding this comment

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

I think its not the same thing to have a range of at max 256 elements vs only ranges between -256 and +256

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Other ideas? The problem is, we need to check this before the range( code, otherwise php will crash, Maybe we can avoid range and use a loop here?

Copy link
Contributor

@staabm staabm Jan 24, 2022

Choose a reason for hiding this comment

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

Hmm max-min <= limit could work since stepping is always 1 in this cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was the idea. I already tried it, maybe you can take look at it, thanks.

do {
$rangeCount++;
if ($rangeCount > self::ARRAY_COUNT_LIMIT) {
$scalarTypes = $integerRangesBackup;
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the logic. The $scalarTypes array should never contain IntegerRangeType, why do you assign it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am sure that the classes that triggers the issue #6375 contains int-ranges and that the range call triggers the fatal error. So I tried to test it via unit tests.

What type triggers the fatal error from issue #6375? Does the int type somehow contains the int-range? I thought that this is a new type? 🤔

@ondrejmirtes
Copy link
Member

I'm sorry, I didn't understand your fix, so I fixed it by myself: d3f968d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants