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

False positive: Comparison operation ">" between 0 and 0 is always false. #8938

Closed
phil-davis opened this issue Feb 22, 2023 · 10 comments
Closed

Comments

@phil-davis
Copy link

Bug report

The type of "while strlen" construction in the example was passing phpstan. But now it fails on PHP7 (but looks like it passes on PHP8).

Code snippet that reproduces the problem

https://phpstan.org/r/0694abfa-be7c-47e9-ab0e-d2d6ca7dec80

Expected output

The code should pass phpstan, and was passing with 1.9

Did PHPStan help you today? Did it make you happy in any way?

@phil-davis
Copy link
Author

We noticed this in overnight CI that started failing on code here:
https://github.com/owncloud/encryption/blob/master/lib/Crypto/Encryption.php#L321

@phil-davis
Copy link
Author

Note: with PHP 7.4 this passes on phpstan 1.9.18 and fails on 1.10.0

@phil-davis
Copy link
Author

Is this commit somehow related ?
phpstan/phpstan-src@649eb70

But I don't see an obvious problem with that code, or how it would execute differently on PHP 7.4

@VincentLanglet
Copy link
Contributor

Is this commit somehow related ? phpstan/phpstan-src@649eb70

If you want, you can find the commit who introduce the bug by writing a test case for php 7.4 on phpstan-src and running git bisect.
(or maybe by requiring directly the commit hash from https://github.com/phpstan/phpstan/commits/1.10.x in your composer.json to see if your code works/fail)

@phil-davis
Copy link
Author

good idea about using the commit hash, I will try that.

@eigan
Copy link

eigan commented Feb 24, 2023

Got similar issue with this snippet

https://phpstan.org/r/b44378ba-632d-4b47-bb04-f8ac420be7a3

Comparison operation ">" between 0 and 31 is always false.

@phil-davis
Copy link
Author

Somehow phpstan is thinking that strlen() is going to always return 0 in code like ours.
And it reports:

Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your [configuration file](https://phpstan.org/config-reference).

But I don't see any PHPDoc at all in our code examples, So that message seems to be emitted in error.

@phil-davis
Copy link
Author

In my example, I can refactor to:

		$x = strlen($data);
		while ($x > 0) {

and that passes. But having the strlen() call embedded in the while fails on PHP 7.

So there is a silly workaround for my example.

But doing a similar refactoring of @eigan example, it still fails. What is going on?

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@f882eff

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants