Skip to content

Commit e9c0701

Browse files
committed
Validate the commercial week range before conversion
Reject week numbers outside -53..-1 and 1..53 before conversion so integer overflow cannot make invalid input appear valid. Fixes #180.
1 parent 2140246 commit e9c0701

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

ext/date/date_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,16 @@ c_valid_commercial_p(int y, int w, int d, double sg,
992992
if (w < 0) {
993993
int rjd2;
994994

995+
if (w < -53) return 0;
995996
c_commercial_to_jd(y + 1, 1, 1, sg, &rjd2, &ns2);
996997
c_jd_to_commercial(rjd2 + w * 7, sg, &ry2, &rw2, &rd2);
997998
if (ry2 != y)
998999
return 0;
9991000
w = rw2;
10001001
}
1002+
else {
1003+
if (w < 1 || 53 < w) return 0;
1004+
}
10011005
c_commercial_to_jd(y, w, d, sg, rjd, ns);
10021006
c_jd_to_commercial(*rjd, sg, &ry2, rw, rd);
10031007
if (y != ry2 || w != *rw || d != *rd)

test/date/test_date_new.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ def test_commercial__ex
256256
end
257257
end
258258

259+
def test_commercial_p
260+
assert_equal(false, Date.valid_commercial?(2024, -53, 1))
261+
assert_equal(false, Date.valid_commercial?(2024, -1227133565, 1))
262+
end
263+
259264
def test_weeknum
260265
d = Date.weeknum
261266
dt = DateTime.weeknum

0 commit comments

Comments
 (0)