From: Karel Zak Date: Thu, 11 Jul 2024 12:15:44 +0000 (+0200) Subject: cal: make sure day_in_week() does not overrun array size [coverity scan] X-Git-Tag: v2.42-start~259^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=54ff6aff58a59ad50a77483786b209da25e1ed73;p=thirdparty%2Futil-linux.git cal: make sure day_in_week() does not overrun array size [coverity scan] It's very theoretical, but protect against this possibility. Signed-off-by: Karel Zak --- diff --git a/misc-utils/cal.c b/misc-utils/cal.c index 89bce4051..1af95124c 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -1175,6 +1175,9 @@ static int day_in_week(const struct cal_control *ctl, int day, static const int reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; static const int old[] = { 5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2 }; + if (month > 0 && month - 1 >= (int) ARRAY_SIZE(old)) + month = ARRAY_SIZE(old); /* for sure */ + if (year != ctl->reform_year + 1) year -= month < MARCH; else