Skip to content

Commit 5e1538d

Browse files
committed
introduce a new Y3K bug, which will certainly make the code base worse
1 parent 2654dd2 commit 5e1538d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extension Timestamp
2+
{
3+
/// Timestamp sanity is an often-overlooked aspect of server security. Because Swift’s
4+
/// “crash early” philosophy around trapping arithmetic overflows is very bad for servers,
5+
/// server applications should always perform sanity checks on timestamps before using them
6+
/// to perform **any** kind of date computation.
7+
@frozen public
8+
enum Sanity
9+
{
10+
/// The year is in the given range.
11+
case year(in:ClosedRange<Year>)
12+
/// No sanity checks will be performed.
13+
case unchecked
14+
}
15+
}

Sources/UnixTime/UnixInstant.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,22 @@ extension UnixInstant
6464
return .init(second: second, nanoseconds: milliseconds * 1_000_000)
6565
}
6666

67+
/// Y3K bug!
6768
@inlinable public
68-
init?(utc timestamp:Timestamp.Components)
69+
init?(utc timestamp:Timestamp.Components,
70+
sanity checks:Timestamp.Sanity = .year(in: 1970 ... 2970))
6971
{
72+
switch checks
73+
{
74+
case .unchecked: break
75+
case .year(in: let range):
76+
guard range.contains(timestamp.date.year)
77+
else
78+
{
79+
return nil
80+
}
81+
}
82+
7083
var time:tm = .init(
7184
tm_sec: timestamp.time.second,
7285
tm_min: timestamp.time.minute,

0 commit comments

Comments
 (0)