Sun Sep 5 17:49:55 2021 UTC ()
lint: do not warn about comparison 'unsigned <= 0'

Seen in scanners generated by Flex, and about 50 occurrences in the
NetBSD src and xsrc tree, all of which are not suspicious of being bugs.


(rillig)
diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_162.c
diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_162.exp
diff -r1.86 -r1.87 src/usr.bin/xlint/lint1/Makefile
diff -r1.378 -r1.379 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_162.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/msg_162.c 2021/09/05 16:47:24 1.5
+++ src/tests/usr.bin/xlint/lint1/msg_162.c 2021/09/05 17:49:55 1.6
@@ -1,63 +1,63 @@ @@ -1,63 +1,63 @@
1/* $NetBSD: msg_162.c,v 1.5 2021/09/05 16:47:24 rillig Exp $ */ 1/* $NetBSD: msg_162.c,v 1.6 2021/09/05 17:49:55 rillig Exp $ */
2# 3 "msg_162.c" 2# 3 "msg_162.c"
3 3
4// Test for message: comparison of %s with %s, op %s [162] 4// Test for message: comparison of %s with %s, op %s [162]
5 5
6/* lint1-extra-flags: -hp */ 6/* lint1-extra-flags: -hp */
7 7
8void 8void
9left_unsigned(unsigned int ui) 9left_unsigned(unsigned int ui)
10{ 10{
11 if (ui < -5.0) { 11 if (ui < -5.0) {
12 } 12 }
13 13
14 /* expect+1: warning: comparison of unsigned int with negative constant, op < [162] */ 14 /* expect+1: warning: comparison of unsigned int with negative constant, op < [162] */
15 if (ui < -5) { 15 if (ui < -5) {
16 } 16 }
17 17
18 /* expect+1: warning: comparison of unsigned int with 0, op < [162] */ 18 /* expect+1: warning: comparison of unsigned int with 0, op < [162] */
19 if (ui < 0) { 19 if (ui < 0) {
20 } 20 }
21 21
22 /* expect+1: warning: comparison of unsigned int with 0, op >= [162] */ 22 /* expect+1: warning: comparison of unsigned int with 0, op >= [162] */
23 if (ui >= 0) { 23 if (ui >= 0) {
24 } 24 }
25 25
26 /* expect+1: warning: comparison of unsigned int with 0, op <= [162] */ 26 /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
27 if (ui <= 0) { 27 if (ui <= 0) {
28 } 28 }
29} 29}
30 30
31void 31void
32right_unsigned(unsigned int ui) 32right_unsigned(unsigned int ui)
33{ 33{
34 34
35 if (-5.0 > ui) { 35 if (-5.0 > ui) {
36 } 36 }
37 37
38 /* expect+1: warning: comparison of negative constant with unsigned int, op > [162] */ 38 /* expect+1: warning: comparison of negative constant with unsigned int, op > [162] */
39 if (-5 > ui) { 39 if (-5 > ui) {
40 } 40 }
41 41
42 /* expect+1: warning: comparison of 0 with unsigned int, op > [162] */ 42 /* expect+1: warning: comparison of 0 with unsigned int, op > [162] */
43 if (0 > ui) { 43 if (0 > ui) {
44 } 44 }
45 45
46 /* expect+1: warning: comparison of 0 with unsigned int, op <= [162] */ 46 /* expect+1: warning: comparison of 0 with unsigned int, op <= [162] */
47 if (0 <= ui) { 47 if (0 <= ui) {
48 } 48 }
49 49
50 /* expect+1: warning: comparison of 0 with unsigned int, op >= [162] */ 50 /* before 2021-09-05: comparison of 0 with unsigned int, op >= [162] */
51 if (0 >= ui) { 51 if (0 >= ui) {
52 } 52 }
53} 53}
54 54
55/* 55/*
56 * Lint does not care about these comparisons, even though they are obviously 56 * Lint does not care about these comparisons, even though they are obviously
57 * out of range. 57 * out of range.
58 */ 58 */
59void 59void
60compare_signed_char(signed char sc) 60compare_signed_char(signed char sc)
61{ 61{
62 if (sc == -129) 62 if (sc == -129)
63 return; 63 return;
@@ -87,31 +87,38 @@ void take_bool(_Bool); @@ -87,31 +87,38 @@ void take_bool(_Bool);
87 87
88void 88void
89compare_operators(unsigned int x) 89compare_operators(unsigned int x)
90{ 90{
91 /* expect+1: warning: comparison of unsigned int with negative constant, op < [162] */ 91 /* expect+1: warning: comparison of unsigned int with negative constant, op < [162] */
92 take_bool(x < -1); 92 take_bool(x < -1);
93 /* expect+1: warning: comparison of unsigned int with 0, op < [162] */ 93 /* expect+1: warning: comparison of unsigned int with 0, op < [162] */
94 take_bool(x < 0); 94 take_bool(x < 0);
95 take_bool(x < 1); 95 take_bool(x < 1);
96 96
97 /* expect+1: warning: comparison of unsigned int with negative constant, op <= [162] */ 97 /* expect+1: warning: comparison of unsigned int with negative constant, op <= [162] */
98 take_bool(x <= -1); 98 take_bool(x <= -1);
99 /* 99 /*
100 * XXX: The expression 'x <= 0' is equivalent to 'x < 1', so lint 100 * Before tree.c 1.379 from 2021-09-05, lint warned about
101 * should not warn about it, just as it doesn't warn about the 101 * 'unsigned <= 0' as well as '0 >= unsigned'. In all cases where
102 * inverted condition, which is 'x > 0'. 102 * the programmer knows whether the underlying data type is signed or
 103 * unsigned, it is clearer to express the same thought as
 104 * 'unsigned == 0', but that's a stylistic issue only.
 105 *
 106 * Removing this particular case of the warning is not expected to
 107 * miss any bugs. The expression 'x <= 0' is equivalent to 'x < 1',
 108 * so lint should not warn about it, just as it doesn't warn about
 109 * the inverted condition, which is 'x > 0'.
103 */ 110 */
104 /* expect+1: warning: comparison of unsigned int with 0, op <= [162] */ 111 /* before 2021-09-05: comparison of unsigned int with 0, op <= [162] */
105 take_bool(x <= 0); 112 take_bool(x <= 0);
106 take_bool(x <= 1); 113 take_bool(x <= 1);
107 114
108 /* expect+1: warning: comparison of unsigned int with negative constant, op > [162] */ 115 /* expect+1: warning: comparison of unsigned int with negative constant, op > [162] */
109 take_bool(x > -1); 116 take_bool(x > -1);
110 take_bool(x > 0); 117 take_bool(x > 0);
111 take_bool(x > 1); 118 take_bool(x > 1);
112 119
113 /* expect+1: warning: comparison of unsigned int with negative constant, op >= [162] */ 120 /* expect+1: warning: comparison of unsigned int with negative constant, op >= [162] */
114 take_bool(x >= -1); 121 take_bool(x >= -1);
115 /* expect+1: warning: comparison of unsigned int with 0, op >= [162] */ 122 /* expect+1: warning: comparison of unsigned int with 0, op >= [162] */
116 take_bool(x >= 0); 123 take_bool(x >= 0);
117 take_bool(x >= 1); 124 take_bool(x >= 1);

cvs diff -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/Attic/msg_162.exp (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/Attic/msg_162.exp 2021/09/05 16:47:24 1.5
+++ src/tests/usr.bin/xlint/lint1/Attic/msg_162.exp 2021/09/05 17:49:55 1.6
@@ -1,18 +1,15 @@ @@ -1,18 +1,15 @@
1msg_162.c(15): warning: comparison of unsigned int with negative constant, op < [162] 1msg_162.c(15): warning: comparison of unsigned int with negative constant, op < [162]
2msg_162.c(19): warning: comparison of unsigned int with 0, op < [162] 2msg_162.c(19): warning: comparison of unsigned int with 0, op < [162]
3msg_162.c(23): warning: comparison of unsigned int with 0, op >= [162] 3msg_162.c(23): warning: comparison of unsigned int with 0, op >= [162]
4msg_162.c(27): warning: comparison of unsigned int with 0, op <= [162] 
5msg_162.c(39): warning: comparison of negative constant with unsigned int, op > [162] 4msg_162.c(39): warning: comparison of negative constant with unsigned int, op > [162]
6msg_162.c(43): warning: comparison of 0 with unsigned int, op > [162] 5msg_162.c(43): warning: comparison of 0 with unsigned int, op > [162]
7msg_162.c(47): warning: comparison of 0 with unsigned int, op <= [162] 6msg_162.c(47): warning: comparison of 0 with unsigned int, op <= [162]
8msg_162.c(51): warning: comparison of 0 with unsigned int, op >= [162] 
9msg_162.c(76): warning: comparison of unsigned char with negative constant, op == [162] 7msg_162.c(76): warning: comparison of unsigned char with negative constant, op == [162]
10msg_162.c(92): warning: comparison of unsigned int with negative constant, op < [162] 8msg_162.c(92): warning: comparison of unsigned int with negative constant, op < [162]
11msg_162.c(94): warning: comparison of unsigned int with 0, op < [162] 9msg_162.c(94): warning: comparison of unsigned int with 0, op < [162]
12msg_162.c(98): warning: comparison of unsigned int with negative constant, op <= [162] 10msg_162.c(98): warning: comparison of unsigned int with negative constant, op <= [162]
13msg_162.c(105): warning: comparison of unsigned int with 0, op <= [162] 11msg_162.c(116): warning: comparison of unsigned int with negative constant, op > [162]
14msg_162.c(109): warning: comparison of unsigned int with negative constant, op > [162] 12msg_162.c(121): warning: comparison of unsigned int with negative constant, op >= [162]
15msg_162.c(114): warning: comparison of unsigned int with negative constant, op >= [162] 13msg_162.c(123): warning: comparison of unsigned int with 0, op >= [162]
16msg_162.c(116): warning: comparison of unsigned int with 0, op >= [162] 14msg_162.c(127): warning: comparison of unsigned int with negative constant, op == [162]
17msg_162.c(120): warning: comparison of unsigned int with negative constant, op == [162] 15msg_162.c(132): warning: comparison of unsigned int with negative constant, op != [162]
18msg_162.c(125): warning: comparison of unsigned int with negative constant, op != [162] 

cvs diff -r1.86 -r1.87 src/usr.bin/xlint/lint1/Makefile (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/Makefile 2021/09/05 16:36:56 1.86
+++ src/usr.bin/xlint/lint1/Makefile 2021/09/05 17:49:55 1.87
@@ -1,36 +1,35 @@ @@ -1,36 +1,35 @@
1# $NetBSD: Makefile,v 1.86 2021/09/05 16:36:56 rillig Exp $ 1# $NetBSD: Makefile,v 1.87 2021/09/05 17:49:55 rillig Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4 4
5PROG= lint1 5PROG= lint1
6SRCS= cgram.y \ 6SRCS= cgram.y \
7 ckbool.c ckctype.c ckgetopt.c debug.c \ 7 ckbool.c ckctype.c ckgetopt.c debug.c \
8 decl.c emit.c emit1.c err.c func.c init.c inittyp.c lex.c \ 8 decl.c emit.c emit1.c err.c func.c init.c inittyp.c lex.c \
9 main1.c mem.c mem1.c oper.c scan.l tree.c tyname.c 9 main1.c mem.c mem1.c oper.c scan.l tree.c tyname.c
10 10
11MAN= lint.7 11MAN= lint.7
12YHEADER= 12YHEADER=
13#DBG= -g 13#DBG= -g
14#CPPFLAGS+= -DYYDEBUG=1 14#CPPFLAGS+= -DYYDEBUG=1
15#YFLAGS+= -v 15#YFLAGS+= -v
16#LFLAGS+= -d 16#LFLAGS+= -d
17 17
18CWARNFLAGS.clang+= -Wno-error=implicit-int-float-conversion 18CWARNFLAGS.clang+= -Wno-error=implicit-int-float-conversion
19LINTFLAGS+= -T 19LINTFLAGS+= -T
20LOBJS.${PROG}+= ${SRCS:M*.y:.y=.ln} 20LOBJS.${PROG}+= ${SRCS:M*.y:.y=.ln}
21LOBJS.${PROG}+= ${SRCS:M*.l:.l=.ln} 21LOBJS.${PROG}+= ${SRCS:M*.l:.l=.ln}
22LINTFLAGS.scan.c+= -X 107,126,330,331,332,333 # strict bool mode 22LINTFLAGS.scan.c+= -X 107,126,330,331,332,333 # strict bool mode
23LINTFLAGS.scan.c+= -X 162 # comparison of 'unsigned <= 0' 
24LINTFLAGS.scan.c+= -X 192,214 # due to suppressed bool errors 23LINTFLAGS.scan.c+= -X 192,214 # due to suppressed bool errors
25LINTFLAGS.scan.c+= -X 307 # static variable unused 24LINTFLAGS.scan.c+= -X 307 # static variable unused
26 25
27CPPFLAGS+= -DIS_LINT1 26CPPFLAGS+= -DIS_LINT1
28CPPFLAGS+= -I${.CURDIR} 27CPPFLAGS+= -I${.CURDIR}
29CPPFLAGS+= ${DEBUG:D-DDEBUG -DYYDEBUG} 28CPPFLAGS+= ${DEBUG:D-DDEBUG -DYYDEBUG}
30 29
31COPTS.err.c+= ${${ACTIVE_CC} == "clang":? -Wno-format-nonliteral :} 30COPTS.err.c+= ${${ACTIVE_CC} == "clang":? -Wno-format-nonliteral :}
32 31
33BINDIR= /usr/libexec 32BINDIR= /usr/libexec
34 33
35CLEANFILES+= ${MAN} ${MAN}.date 34CLEANFILES+= ${MAN} ${MAN}.date
36 35

cvs diff -r1.378 -r1.379 src/usr.bin/xlint/lint1/tree.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/tree.c 2021/09/05 16:03:55 1.378
+++ src/usr.bin/xlint/lint1/tree.c 2021/09/05 17:49:55 1.379
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.378 2021/09/05 16:03:55 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.379 2021/09/05 17:49:55 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: tree.c,v 1.378 2021/09/05 16:03:55 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.379 2021/09/05 17:49:55 rillig Exp $");
41#endif 41#endif
42 42
43#include <float.h> 43#include <float.h>
44#include <limits.h> 44#include <limits.h>
45#include <math.h> 45#include <math.h>
46#include <signal.h> 46#include <signal.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "lint1.h" 50#include "lint1.h"
51#include "cgram.h" 51#include "cgram.h"
52 52
53static tnode_t *build_integer_constant(tspec_t, int64_t); 53static tnode_t *build_integer_constant(tspec_t, int64_t);
@@ -4222,39 +4222,39 @@ check_integer_comparison(op_t op, tnode_ @@ -4222,39 +4222,39 @@ check_integer_comparison(op_t op, tnode_
4222 4222
4223 if ((hflag || pflag) && ((lt == CHAR && is_out_of_char_range(rn)) || 4223 if ((hflag || pflag) && ((lt == CHAR && is_out_of_char_range(rn)) ||
4224 (rt == CHAR && is_out_of_char_range(ln)))) { 4224 (rt == CHAR && is_out_of_char_range(ln)))) {
4225 /* nonportable character comparison, op %s */ 4225 /* nonportable character comparison, op %s */
4226 warning(230, op_name(op)); 4226 warning(230, op_name(op));
4227 return; 4227 return;
4228 } 4228 }
4229 if (is_uinteger(lt) && !is_uinteger(rt) && 4229 if (is_uinteger(lt) && !is_uinteger(rt) &&
4230 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 4230 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4231 if (rn->tn_val->v_quad < 0) { 4231 if (rn->tn_val->v_quad < 0) {
4232 /* comparison of %s with %s, op %s */ 4232 /* comparison of %s with %s, op %s */
4233 warning(162, type_name(ln->tn_type), 4233 warning(162, type_name(ln->tn_type),
4234 "negative constant", op_name(op)); 4234 "negative constant", op_name(op));
4235 } else if (op == LT || op == GE || (hflag && op == LE)) { 4235 } else if (op == LT || op == GE) {
4236 /* comparison of %s with %s, op %s */ 4236 /* comparison of %s with %s, op %s */
4237 warning(162, type_name(ln->tn_type), "0", op_name(op)); 4237 warning(162, type_name(ln->tn_type), "0", op_name(op));
4238 } 4238 }
4239 return; 4239 return;
4240 } 4240 }
4241 if (is_uinteger(rt) && !is_uinteger(lt) && 4241 if (is_uinteger(rt) && !is_uinteger(lt) &&
4242 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 4242 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4243 if (ln->tn_val->v_quad < 0) { 4243 if (ln->tn_val->v_quad < 0) {
4244 /* comparison of %s with %s, op %s */ 4244 /* comparison of %s with %s, op %s */
4245 warning(162, "negative constant", 4245 warning(162, "negative constant",
4246 type_name(rn->tn_type), op_name(op)); 4246 type_name(rn->tn_type), op_name(op));
4247 } else if (op == GT || op == LE || (hflag && op == GE)) { 4247 } else if (op == GT || op == LE) {
4248 /* comparison of %s with %s, op %s */ 4248 /* comparison of %s with %s, op %s */
4249 warning(162, "0", type_name(rn->tn_type), op_name(op)); 4249 warning(162, "0", type_name(rn->tn_type), op_name(op));
4250 } 4250 }
4251 return; 4251 return;
4252 } 4252 }
4253} 4253}
4254 4254
4255/* 4255/*
4256 * Return whether the expression can be used for static initialization. 4256 * Return whether the expression can be used for static initialization.
4257 * 4257 *
4258 * Constant initialization expressions must be constant or an address 4258 * Constant initialization expressions must be constant or an address
4259 * of a static object with an optional offset. In the first case, 4259 * of a static object with an optional offset. In the first case,
4260 * the result is returned in *offsp. In the second case, the static 4260 * the result is returned in *offsp. In the second case, the static