Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: fix integer overflow on seeing -(u...



details:   https://anonhg.NetBSD.org/src/rev/dcc4013e7e57
branches:  trunk
changeset: 365714:dcc4013e7e57
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Apr 19 23:16:14 2022 +0000

description:
lint: fix integer overflow on seeing -(uint64_t)INT64_MIN

Seen in external/cddl/osnet/dist/lib/libuutil/common/uu_strtoint.c,
which is protected by a NOLINT in ../Makefile.inc.

diffstat:

 tests/usr.bin/xlint/lint1/expr_fold.c   |  15 ++++++++++++++-
 tests/usr.bin/xlint/lint1/expr_fold.exp |   1 +
 usr.bin/xlint/lint1/tree.c              |   6 +++---
 3 files changed, 18 insertions(+), 4 deletions(-)

diffs (61 lines):

diff -r 0e9ba4889c9b -r dcc4013e7e57 tests/usr.bin/xlint/lint1/expr_fold.c
--- a/tests/usr.bin/xlint/lint1/expr_fold.c     Tue Apr 19 22:53:34 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_fold.c     Tue Apr 19 23:16:14 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expr_fold.c,v 1.6 2022/04/15 21:50:07 rillig Exp $     */
+/*     $NetBSD: expr_fold.c,v 1.7 2022/04/19 23:16:14 rillig Exp $     */
 # 3 "expr_fold.c"
 
 /*
@@ -287,3 +287,16 @@
                ? 1
                : -1;
 };
+
+/*
+ * Since Makefile.inc 1.21 from 2022-04-08 (which added -ftrapv) and before
+ * tree.c 1.436 from 2022-04-20, lint crashed with an integer overflow when
+ * calculating '-(uint64_t)INT64_MIN' in val_t.v_quad.
+ */
+void
+unary_minus_overflow(unsigned long long val)
+{
+       /* expect+1: warning: integer overflow detected, op '-' [141] */
+       if (val > -(unsigned long long)(-0x7fffffffffffffffL - 1))
+               return;
+}
diff -r 0e9ba4889c9b -r dcc4013e7e57 tests/usr.bin/xlint/lint1/expr_fold.exp
--- a/tests/usr.bin/xlint/lint1/expr_fold.exp   Tue Apr 19 22:53:34 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_fold.exp   Tue Apr 19 23:16:14 2022 +0000
@@ -32,3 +32,4 @@
 expr_fold.c(186): warning: integer overflow detected, op '<<' [141]
 expr_fold.c(190): warning: shift amount 104 is greater than bit-size 32 of 'unsigned int' [122]
 expr_fold.c(202): warning: shift amount 104 is greater than bit-size 32 of 'int' [122]
+expr_fold.c(300): warning: integer overflow detected, op '-' [141]
diff -r 0e9ba4889c9b -r dcc4013e7e57 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Tue Apr 19 22:53:34 2022 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Tue Apr 19 23:16:14 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.435 2022/04/19 22:14:30 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.436 2022/04/19 23:16:14 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.435 2022/04/19 22:14:30 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.436 2022/04/19 23:16:14 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3210,7 +3210,7 @@
                q = sl;
                break;
        case UMINUS:
-               q = -sl;
+               q = sl == INT64_MIN ? sl : -sl;
                if (sl != 0 && msb(q, t) == msb(sl, t))
                        ovfl = true;
                break;



Home | Main Index | Thread Index | Old Index