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 undefined behavior on enum ove...



details:   https://anonhg.NetBSD.org/src/rev/b987379c7497
branches:  trunk
changeset: 364715:b987379c7497
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Apr 08 21:48:19 2022 +0000

description:
lint: fix undefined behavior on enum overflow (since 1995)

GCC had optimized the check away, due to the obvious integer overflow 'x
- 1 == INT_MAX'.  To prevent further bugs like this, compile with
-ftrapv.

diffstat:

 tests/usr.bin/xlint/lint1/msg_048.c   |  12 +++++-------
 tests/usr.bin/xlint/lint1/msg_048.exp |   2 +-
 usr.bin/xlint/Makefile.inc            |   4 +++-
 usr.bin/xlint/lint1/decl.c            |   8 ++++----
 4 files changed, 13 insertions(+), 13 deletions(-)

diffs (85 lines):

diff -r d0daa2fcb0e9 -r b987379c7497 tests/usr.bin/xlint/lint1/msg_048.c
--- a/tests/usr.bin/xlint/lint1/msg_048.c       Fri Apr 08 21:29:29 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_048.c       Fri Apr 08 21:48:19 2022 +0000
@@ -1,10 +1,10 @@
-/*     $NetBSD: msg_048.c,v 1.3 2022/04/08 21:29:29 rillig Exp $       */
+/*     $NetBSD: msg_048.c,v 1.4 2022/04/08 21:48:19 rillig Exp $       */
 # 3 "msg_048.c"
 
 // Test for message: overflow in enumeration values: %s [48]
 
 /*
- * Before decl.c 1.TODO from 2022-04-TODO, the comparison for enum constant
+ * Before decl.c 1.269 from 2022-04-08, the comparison for enum constant
  * overflow was done in signed arithmetic, and since 'enumval' wrapped
  * around, its value became INT_MIN, at least on platforms where integer
  * overflow was defined as 2-complements wrap-around.  When comparing
@@ -16,9 +16,7 @@
        MAX_MINUS_2 = 0x7ffffffd,
        MAX_MINUS_1,
        MAX,
-       /* TODO: expect: overflow in enumeration values: MIN */
-       MIN
+       /* expect+1: overflow in enumeration values: MIN [48] */
+       MIN,
+       MIN_PLUS_1
 };
-
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
diff -r d0daa2fcb0e9 -r b987379c7497 tests/usr.bin/xlint/lint1/msg_048.exp
--- a/tests/usr.bin/xlint/lint1/msg_048.exp     Fri Apr 08 21:29:29 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_048.exp     Fri Apr 08 21:48:19 2022 +0000
@@ -1,1 +1,1 @@
-msg_048.c(23): error: syntax error ':' [249]
+msg_048.c(20): warning: overflow in enumeration values: MIN [48]
diff -r d0daa2fcb0e9 -r b987379c7497 usr.bin/xlint/Makefile.inc
--- a/usr.bin/xlint/Makefile.inc        Fri Apr 08 21:29:29 2022 +0000
+++ b/usr.bin/xlint/Makefile.inc        Fri Apr 08 21:48:19 2022 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.20 2022/02/27 07:50:09 rillig Exp $
+#      $NetBSD: Makefile.inc,v 1.21 2022/04/08 21:48:19 rillig Exp $
 
 .include <bsd.own.mk>
 
@@ -19,6 +19,8 @@
 
 CLEANFILES+=   *.gcno *.gcda *.gcov
 
+CFLAGS+=       ${ACTIVE_CC:Mgcc:%=-ftrapv}
+
 .if exists(${.CURDIR}/../../Makefile.inc)
 .include "${.CURDIR}/../../Makefile.inc"
 .endif
diff -r d0daa2fcb0e9 -r b987379c7497 usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Fri Apr 08 21:29:29 2022 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Fri Apr 08 21:48:19 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.268 2022/04/03 10:05:22 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.269 2022/04/08 21:48:19 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: decl.c,v 1.268 2022/04/03 10:05:22 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.269 2022/04/08 21:48:19 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -1926,11 +1926,11 @@
        sym->s_type = dcs->d_tagtyp;
        sym->s_value.v_tspec = INT;
        sym->s_value.v_quad = val;
-       if (impl && val - 1 == TARG_INT_MAX) {
+       if (impl && val == TARG_INT_MIN) {
                /* overflow in enumeration values: %s */
                warning(48, sym->s_name);
        }
-       enumval = val + 1;
+       enumval = val == TARG_INT_MAX ? TARG_INT_MIN : val + 1;
        return sym;
 }
 



Home | Main Index | Thread Index | Old Index