Skip to content

ext/bcmath: Fixed LONG_MAX #15663

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

Merged
merged 1 commit into from
Aug 30, 2024
Merged

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Aug 30, 2024

One f is missing.

However, LONG_MAX is predefined in php.h, so this macro will probably never be used....
TBH, I think it's okay to delete it...

@cmb69
Copy link
Member

cmb69 commented Aug 30, 2024

It's doubtful to define it this way, since on LP64 architectures (which are likely used by most PHP builds) long is a 64bit type.

Given that LONG_MAX is a mandatory part of C99, I think dropping the fallback definitions is fine, but I'm a bit uneasy about doing it that late in the release cycle.

@SakiTakamachi
Copy link
Member Author

Here is the definition in php.h.

#define LONG_MAX 2147483647L

I don't know why it's defined as 32bit....

@cmb69
Copy link
Member

cmb69 commented Aug 30, 2024

I don't know why it's defined as 32bit....

Perhaps because nobody cared about it since 64bit support happened.

@SakiTakamachi
Copy link
Member Author

oh...

In fact, if want a 64-bit value, use ZEND_LONG_MAX.

It seems difficult to immediately switch LONG_MAX/LONG_MIN to 64-bit, since we need to check the intention of the places where they are used.

@nielsdos
Copy link
Member

I don't know why it's defined as 32bit....

Note that this is only when LONG_MAX is not defined in limits.h, which it should be in C99.

LONG_MAX is only used in bcmath here:

if (val > LONG_MAX / BASE) {
return 0;
}
val *= BASE;
if (val > LONG_MAX - n) {
return 0;
}

Just include php.h for the correct definition in the C file (if it's not already included indirectly), and remove the wrong definition from the header.

@cmb69
Copy link
Member

cmb69 commented Aug 30, 2024

ZEND_LONG_MAX and LONG_MAX must not be confused. For instance, on 64bit Windows, ZEND_LONG_MAX is 2**63-1, but LONG_MAX is 2**31-1.

It seems difficult to immediately switch LONG_MAX/LONG_MIN to 64-bit, […]

We cannot do this at all, because it would be wrong.

[…], since we need to check the intention of the places where they are used.

The intention of using LONG_MAX should always be the same: having the largest value that can be stored in a variable of type long.

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

I think this is good as is for now (we can revisit this for PHP 8.5 or PHP 9.0).

Thank you for the PR!

@SakiTakamachi
Copy link
Member Author

Ah, I see, I remembered incorrectly about LONG_MAX.
Thanks!

@SakiTakamachi SakiTakamachi merged commit a27878c into php:master Aug 30, 2024
9 of 10 checks passed
@SakiTakamachi SakiTakamachi deleted the fix/bcmath_long_max branch August 30, 2024 16:07
@cmb69
Copy link
Member

cmb69 commented Aug 30, 2024

Seems we had some overlap here. :)

@nielsdos, the definition in php.h is also "wrong":

#define LONG_MAX 2147483647L

@nielsdos
Copy link
Member

@cmb69 limits.h is included too, so the fallback doesn't matter and the value will be right.

@cmb69
Copy link
Member

cmb69 commented Aug 30, 2024

@nielsdos, there is no problem as it is now. bcmath.h includes zend.h which includes zend_types.h which includes zend_portability.h which includes limits.h. (Well, no problem regarding the possibly missing <limits.h> inclusion, but apparently a big mess regarding the inlcudes; maybe worse: php_bcmath.h includes bcmath.h which in turn includes php_bcmath.h.)

@nielsdos
Copy link
Member

Right, then ig we can just drop these fallbacks in the next version.

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

Successfully merging this pull request may close these issues.

3 participants