Skip to content

Fix GH-16234 jewishtojd overflow on year argument. #16243

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 2 commits into from

Conversation

devnexen
Copy link
Member

@devnexen devnexen commented Oct 5, 2024

No description provided.


/* Start with the time of the first molad after creation. */
r1 = NEW_MOON_OF_CREATION;
chk = (zend_long)metonicCycle;
Copy link
Member

Choose a reason for hiding this comment

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

Note that if zend_long is 32-bit longs, you still have an issue.
Furthermore, I think the failure should be propagated instead of silently set to 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

Furthermore, I think the failure should be propagated instead of silently set to 0.

That sounds more a change for master ?

chk = (zend_long)metonicCycle;

if (chk == ZEND_LONG_MAX ||
chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF) == ZEND_LONG_MAX ||
Copy link
Member

Choose a reason for hiding this comment

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

This multiplication can still overflow, no?

@devnexen devnexen force-pushed the gh16234 branch 2 times, most recently from d6658c5 to d426d7f Compare October 5, 2024 21:31

/* Calculate metonicCycle * HALAKIM_PER_METONIC_CYCLE. The upper 32
* bits of the result will be in r2 and the lower 16 bits will be
* in r1. */
r1 += metonicCycle * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF);
r1 += chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF);
Copy link
Member

Choose a reason for hiding this comment

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

This can still overflow, because r1 starts with NEW_MOON_OF_CREATION.
Analogous for r2.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

It still isn't right...
You essentially need to check if r1 > ZEND_LONG_MAX which isn't possible to write of course, but if we look at it mathematically:
r1 = NEW_MOON_OF_CREATION + chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF)
and so
NEW_MOON_OF_CREATION + chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF) > ZEND_LONG_MAX
<=> chk * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF) > ZEND_LONG_MAX - NEW_MOON_OF_CREATION
<=> chk > (ZEND_LONG_MAX - NEW_MOON_OF_CREATION)/(HALAKIM_PER_METONIC_CYCLE & 0xFFFF)

This isn't what you wrote and I don't think it's equivalent. Now you should do the same for r2.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

You misunderstood the notation. In mathematics <=> means "equivalent". So I meant that the last expression was the one that should be tested and the steps before that just show how I arrived there.

You need something like this: https://gist.github.com/nielsdos/527062ecc725d34c5641b48d3450e1f5

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

OK if CI green

@devnexen devnexen closed this in e3015de 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/jewish.c:439
2 participants