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

Avoid UB in timelib_diff_days() #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

cmb69
Copy link
Contributor

@cmb69 cmb69 commented Jan 19, 2025

We clamp the calculated days to properly fit in an int.

See php/php-src#17504.

We clamp the calculated days to properly fit in an `int`.

See <php/php-src#17504>.
@@ -201,7 +201,8 @@ int timelib_diff_days(timelib_time *one, timelib_time *two)
days--;
}
} else {
days = fabs(floor(one->sse - two->sse) / 86400);
double ddays = fabs(floor(one->sse - two->sse) / 86400);
days = ddays <= INT_MAX ? ddays : INT_MAX;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not so sure whether truncating the value is a good idea either. Ideally, it should return with a failure. But then again, the APIs for most timelib functions have currently no such capability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. This is not meant to be a clean solution, but merely to avoid UB; and for practical purposes, a difference of almost 6 million years might just be good enough (for now).

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

Successfully merging this pull request may close these issues.

2 participants