-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
time: unchecked overflow in Add and AddDays #20678
Comments
What is the C++ time library? Got a link? |
I'm not sure whether it's public. So, no. |
time.Duration does do saturated arithmetic. For time.Time that really seems like overkill. There's no reasonable way you could end up with times that large anyway. |
The The trouble is, at the moment those limits are ad-hoc: the point at which methods stop working is a complicated function of both the method ( Some points of interest are shown here: https://play.golang.org/p/-WgwEfN1Vs In practice, it appears that the portable range is limited by the functions involving dates, which break down outside the range [ For the use-case I have in mind, I explicitly need two distinct unreasonable times: one far enough in the past to represent "infinite past", and one far enough in the future to represent "infinite future". It doesn't really matter what the specific values are, but they need to be well-behaved w.r.t. all "reasonable" values: for example, It would be nice to also have |
Fixed the link: Stelliferous Era |
If you want to make a proposal for the proper min/max and do the implementation, I guess it is OK with me. It does match Duration. |
I'm interested in interoperating with two libraries in particular:
|
Ah, fun: there may have been an undetected overflow in If the overflow was in Sub, it may have been fixed in CL 131196 (#17858), although I haven't verified either way. |
Looks like there is definitely an overflow in
|
If we do anything more, I think we should clamp the year to |y|<100e9, similar to absl (same as absl? unclear). |
I'm trying to define a mapping between
time.Time
and a C++ time library.The library that I'm trying to map to supports distinct "infinite past" and "infinite future" times, which need to be mapped to distinct
time.Time
values. The logical choices would seem to be the maximum and minimum representabletime.Time
values.One way to try to obtain those is to call
(time.Time).AddDate
with absurdly positive or absurdly negative values.AddDate
does not return an error, and it cannot reasonably panic on overflow (because the package does not define a way for users to check for such an overflow ahead of time). That leaves one "obvious" behavior: saturation.Sadly, the current implementation fails to provide that behavior, and instead silently overflows to nonsensical values (https://play.golang.org/p/UUC2JG7Xcj).
(Further evidence for #19624?)
The text was updated successfully, but these errors were encountered: