Skip to content

Fix GH-16258 overflow on jddayofweek argument. #16263

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

Closed
wants to merge 3 commits into from

Conversation

devnexen
Copy link
Member

@devnexen devnexen commented Oct 6, 2024

No description provided.

@devnexen devnexen linked an issue Oct 6, 2024 that may be closed by this pull request
@@ -35,6 +35,10 @@ int DayOfWeek(
{
int dow;

if ((sdn + 1) > INT_MAX) {
Copy link
Member

Choose a reason for hiding this comment

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

zend_long can be the same as int; in which case this check is removed by the compiler because it'll be UB.
Note that you can just change the formula and the if-checks a bit because we're working in mod 7 to avoid the overflow; and avoid needing to check the overflow in the first place.

@devnexen devnexen marked this pull request as draft October 6, 2024 11:42
@devnexen devnexen force-pushed the gh16258 branch 2 times, most recently from ac8cd42 to 735f7fa Compare October 6, 2024 11:54
@devnexen devnexen marked this pull request as ready for review October 6, 2024 12:40
Comment on lines 36 to 43
int dow;

dow = (sdn + 1) % 7;
dow = (int)(sdn % 7 + 1);
if (dow >= 0) {
return (dow);
return (dow % 7);
} else {
return (dow + 7);
return (dow + 7) % 7;
}
Copy link
Member

Choose a reason for hiding this comment

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

Can't we just do:

return (sdn % 7 + 8) % 7;

instead of what we have now?

Copy link
Member Author

Choose a reason for hiding this comment

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

giving it a try

Copy link
Member

Choose a reason for hiding this comment

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

I mean, it is somewhat obscure, and likely not what @nielsdos had in mind. But I think it should work.

Copy link
Member Author

@devnexen devnexen Oct 6, 2024

Choose a reason for hiding this comment

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

it does work. I think the changed old test, the values make more sense to me.

Copy link
Member

Choose a reason for hiding this comment

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

Without that (int) cast, bug_53574_2.phpt succeeds for me; either remove the cast, or cast the whole return value to (int) (that shouldn't be necessary since the % 7 ensures that values fit into int, but if some tool complains, we may need to cast).

Copy link
Member

Choose a reason for hiding this comment

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

Double modulus works and is intuitive as well. It should be possible to avoid the double modulus too but it likely doesn't matter for performance.

@devnexen devnexen closed this in a5e8ac6 Oct 6, 2024
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.

Signed integer overflow in ext/calendar/dow.c:38
3 participants