]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cal: make sure day_in_week() does not overrun array size [coverity scan]
authorKarel Zak <kzak@redhat.com>
Thu, 11 Jul 2024 12:15:44 +0000 (14:15 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 11 Jul 2024 12:15:44 +0000 (14:15 +0200)
It's very theoretical, but protect against this possibility.

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/cal.c

index 89bce405151a4c8f721adf96cb32140c23db9c0d..1af95124c49af62231415aed06653c00f599a7b2 100644 (file)
@@ -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