golang/go

time: unchecked overflow in Add and AddDays

開放

#20,678 建立於 2017年6月14日

 (10 則留言) (0 個反應) (0 位負責人)Go (19,008 個分叉)batch import
NeedsFixhelp wanted

倉庫指標

星標
 (133,883 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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 representable time.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 https://github.com/golang/go/issues/19624?)

貢獻者指南