Module Name:    src
Committed By:   rillig
Date:           Fri Apr  8 21:48:19 UTC 2022

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_048.c msg_048.exp
        src/usr.bin/xlint: Makefile.inc
        src/usr.bin/xlint/lint1: decl.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/xlint/lint1/msg_048.c \
    src/tests/usr.bin/xlint/lint1/msg_048.exp
cvs rdiff -u -r1.20 -r1.21 src/usr.bin/xlint/Makefile.inc
cvs rdiff -u -r1.268 -r1.269 src/usr.bin/xlint/lint1/decl.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_048.c
diff -u src/tests/usr.bin/xlint/lint1/msg_048.c:1.3 src/tests/usr.bin/xlint/lint1/msg_048.c:1.4
--- src/tests/usr.bin/xlint/lint1/msg_048.c:1.3	Fri Apr  8 21:29:29 2022
+++ src/tests/usr.bin/xlint/lint1/msg_048.c	Fri Apr  8 21:48:19 2022
@@ -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 @@ enum int_limits {
 	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."
Index: src/tests/usr.bin/xlint/lint1/msg_048.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_048.exp:1.3 src/tests/usr.bin/xlint/lint1/msg_048.exp:1.4
--- src/tests/usr.bin/xlint/lint1/msg_048.exp:1.3	Fri Apr  8 21:29:29 2022
+++ src/tests/usr.bin/xlint/lint1/msg_048.exp	Fri Apr  8 21:48:19 2022
@@ -1 +1 @@
-msg_048.c(23): error: syntax error ':' [249]
+msg_048.c(20): warning: overflow in enumeration values: MIN [48]

Index: src/usr.bin/xlint/Makefile.inc
diff -u src/usr.bin/xlint/Makefile.inc:1.20 src/usr.bin/xlint/Makefile.inc:1.21
--- src/usr.bin/xlint/Makefile.inc:1.20	Sun Feb 27 07:50:09 2022
+++ src/usr.bin/xlint/Makefile.inc	Fri Apr  8 21:48:19 2022
@@ -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 @@ CPPFLAGS+=	-I${.CURDIR}/../common
 
 CLEANFILES+=	*.gcno *.gcda *.gcov
 
+CFLAGS+=	${ACTIVE_CC:Mgcc:%=-ftrapv}
+
 .if exists(${.CURDIR}/../../Makefile.inc)
 .include "${.CURDIR}/../../Makefile.inc"
 .endif

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.268 src/usr.bin/xlint/lint1/decl.c:1.269
--- src/usr.bin/xlint/lint1/decl.c:1.268	Sun Apr  3 10:05:22 2022
+++ src/usr.bin/xlint/lint1/decl.c	Fri Apr  8 21:48:19 2022
@@ -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 @@ enumeration_constant(sym_t *sym, int val
 	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;
 }
 

Reply via email to