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.
This commit is contained in:
rillig 2022-04-08 21:48:19 +00:00
parent 321092b8ae
commit cbe246cc1b
4 changed files with 13 additions and 13 deletions

View File

@ -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."

View File

@ -1 +1 @@
msg_048.c(23): error: syntax error ':' [249]
msg_048.c(20): warning: overflow in enumeration values: MIN [48]

View File

@ -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

View File

@ -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, bool impl)
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;
}