Module Name:    src
Committed By:   rillig
Date:           Mon Mar  1 00:51:01 UTC 2021

Modified Files:
        src/usr.bin/xlint/lint1: lex.c

Log Message:
lint: only warn once about integer constant overflow on 32-bit

Previously, the test msg_056.c warned twice about the integer literal,
but only on 32-bit platforms.  On 64-bit platforms, there was only a
single warning since the integer constant was converted to type
__uint128_t, and this prevented the second warning.  On 32-bit targets,
there is no __uint128_t though.

Fixes part of PR bin/55976.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/xlint/lint1/lex.c

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

Modified files:

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.10 src/usr.bin/xlint/lint1/lex.c:1.11
--- src/usr.bin/xlint/lint1/lex.c:1.10	Sun Feb 28 18:51:51 2021
+++ src/usr.bin/xlint/lint1/lex.c	Mon Mar  1 00:51:01 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.10 2021/02/28 18:51:51 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.11 2021/03/01 00:51:01 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: lex.c,v 1.10 2021/02/28 18:51:51 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.11 2021/03/01 00:51:01 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -478,6 +478,7 @@ lex_integer_constant(const char *yytext,
 	char	c, *eptr;
 	tspec_t	typ;
 	bool	ansiu;
+	bool	warned = false;
 #ifdef TARG_INT128_MAX
 	__uint128_t uq = 0;
 	static	tspec_t contypes[2][4] = {
@@ -531,9 +532,11 @@ lex_integer_constant(const char *yytext,
 
 	uq = strtouq(cp, &eptr, base);
 	lint_assert(eptr == cp + len);
-	if (errno != 0)
+	if (errno != 0) {
 		/* integer constant out of range */
 		warning(252);
+		warned = true;
+	}
 
 	/*
 	 * If the value is too big for the current type, we must choose
@@ -550,7 +553,7 @@ lex_integer_constant(const char *yytext,
 			typ = LONG;
 		} else {
 			typ = ULONG;
-			if (uq > TARG_ULONG_MAX) {
+			if (uq > TARG_ULONG_MAX && !warned) {
 				/* integer constant out of range */
 				warning(252);
 			}
@@ -570,7 +573,7 @@ lex_integer_constant(const char *yytext,
 	case UINT:
 		if (uq > TARG_UINT_MAX) {
 			typ = ULONG;
-			if (uq > TARG_ULONG_MAX) {
+			if (uq > TARG_ULONG_MAX && !warned) {
 				/* integer constant out of range */
 				warning(252);
 			}
@@ -581,14 +584,14 @@ lex_integer_constant(const char *yytext,
 			typ = ULONG;
 			if (!sflag)
 				ansiu = true;
-			if (uq > TARG_ULONG_MAX) {
+			if (uq > TARG_ULONG_MAX && !warned) {
 				/* integer constant out of range */
 				warning(252);
 			}
 		}
 		break;
 	case ULONG:
-		if (uq > TARG_ULONG_MAX) {
+		if (uq > TARG_ULONG_MAX && !warned) {
 			/* integer constant out of range */
 			warning(252);
 		}
@@ -601,7 +604,7 @@ lex_integer_constant(const char *yytext,
 		}
 		break;
 	case UQUAD:
-		if (uq > TARG_UQUAD_MAX) {
+		if (uq > TARG_UQUAD_MAX && !warned) {
 			/* integer constant out of range */
 			warning(252);
 		}
@@ -618,7 +621,7 @@ lex_integer_constant(const char *yytext,
 		break;
 	case UINT128:
 #ifdef TARG_INT128_MAX
-		if (uq > TARG_UINT128_MAX) {
+		if (uq > TARG_UINT128_MAX && !warned) {
 			/* integer constant out of range */
 			warning(252);
 		}

Reply via email to