Wed May 1 05:49:33 2024 UTC (19d)
lint: fix warning about out-of-bounds bit-field value


(rillig)
diff -r1.39 -r1.40 src/tests/usr.bin/xlint/lint1/msg_132.c
diff -r1.637 -r1.638 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.39 -r1.40 src/tests/usr.bin/xlint/lint1/msg_132.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/msg_132.c 2024/05/01 05:38:11 1.39
+++ src/tests/usr.bin/xlint/lint1/msg_132.c 2024/05/01 05:49:33 1.40
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: msg_132.c,v 1.39 2024/05/01 05:38:11 rillig Exp $ */ 1/* $NetBSD: msg_132.c,v 1.40 2024/05/01 05:49:33 rillig Exp $ */
2# 3 "msg_132.c" 2# 3 "msg_132.c"
3 3
4// Test for message: conversion from '%s' to '%s' may lose accuracy [132] 4// Test for message: conversion from '%s' to '%s' may lose accuracy [132]
5 5
6/* lint1-extra-flags: -X 351 */ 6/* lint1-extra-flags: -X 351 */
7 7
8/* 8/*
9 * NetBSD's default lint flags only include a single -a, which only flags 9 * NetBSD's default lint flags only include a single -a, which only flags
10 * narrowing conversions from long. To get warnings for all narrowing 10 * narrowing conversions from long. To get warnings for all narrowing
11 * conversions, -a needs to be given more than once. 11 * conversions, -a needs to be given more than once.
12 * 12 *
13 * https://gnats.netbsd.org/14531 13 * https://gnats.netbsd.org/14531
14 */ 14 */
@@ -255,33 +255,33 @@ test_ic_shr(u64_t x) @@ -255,33 +255,33 @@ test_ic_shr(u64_t x)
255 * No matter whether the big integer is signed or unsigned, the 255 * No matter whether the big integer is signed or unsigned, the
256 * result of '&' is guaranteed to be an unsigned value. 256 * result of '&' is guaranteed to be an unsigned value.
257 */ 257 */
258 u8 = (s64 & 0xf0) >> 4; 258 u8 = (s64 & 0xf0) >> 4;
259 u8 = (s8 & 0xf0) >> 4; 259 u8 = (s8 & 0xf0) >> 4;
260 260
261 /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */ 261 /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132] */
262 return x; 262 return x;
263} 263}
264 264
265unsigned char 265unsigned char
266test_bit_fields(unsigned long long m) 266test_bit_fields(unsigned long long m)
267{ 267{
268 /* expect+1: warning: conversion from 'unsigned long long:32' to 'unsigned int:3' may lose accuracy [132] */ 268 /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned int:3' may lose accuracy [132] */
269 bits.u3 = bits.u32 & m; 269 bits.u3 = bits.u32 & m;
270 270
271 bits.u5 = bits.u3 & m; 271 bits.u5 = bits.u3 & m;
272 bits.u32 = bits.u5 & m; 272 bits.u32 = bits.u5 & m;
273 273
274 /* expect+1: warning: conversion from 'unsigned long long:32' to 'unsigned char' may lose accuracy [132] */ 274 /* expect+1: warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132] */
275 return bits.u32 & m; 275 return bits.u32 & m;
276} 276}
277 277
278/* 278/*
279 * Traditional C has an extra rule that the right-hand operand of a bit shift 279 * Traditional C has an extra rule that the right-hand operand of a bit shift
280 * operator is converted to 'int'. Before tree.c 1.467 from 2022-07-02, this 280 * operator is converted to 'int'. Before tree.c 1.467 from 2022-07-02, this
281 * conversion was implemented as a CVT node, which means a cast, not an 281 * conversion was implemented as a CVT node, which means a cast, not an
282 * implicit conversion. Changing the CVT to NOOP would have caused a wrong 282 * implicit conversion. Changing the CVT to NOOP would have caused a wrong
283 * warning 'may lose accuracy' in language levels other than traditional C. 283 * warning 'may lose accuracy' in language levels other than traditional C.
284 */ 284 */
285 285
286u64_t 286u64_t
287u64_shl(u64_t lhs, u64_t rhs) 287u64_shl(u64_t lhs, u64_t rhs)
@@ -440,16 +440,17 @@ binary_operators_on_bit_fields(void) @@ -440,16 +440,17 @@ binary_operators_on_bit_fields(void)
440 struct { 440 struct {
441 u64_t u15:15; 441 u64_t u15:15;
442 u64_t u48:48; 442 u64_t u48:48;
443 u64_t u64; 443 u64_t u64;
444 } s = { 0, 0, 0 }; 444 } s = { 0, 0, 0 };
445 445
446 u64 = s.u15 | s.u48; 446 u64 = s.u15 | s.u48;
447 u64 = s.u48 | s.u15; 447 u64 = s.u48 | s.u15;
448 u64 = s.u15 | s.u48 | s.u64; 448 u64 = s.u15 | s.u48 | s.u64;
449 u64 = s.u64 | s.u48 | s.u15; 449 u64 = s.u64 | s.u48 | s.u15;
450 cond = (s.u15 | s.u48 | s.u64) != 0; 450 cond = (s.u15 | s.u48 | s.u64) != 0;
451 cond = (s.u64 | s.u48 | s.u15) != 0; 451 cond = (s.u64 | s.u48 | s.u15) != 0;
452 452
453 /* expect+1: warning: conversion of 'int' to 'int:4' is out of range [119] */ 453 // Before tree.c from 1.638 from 2024-05-01, lint wrongly warned:
 454 // warning: conversion of 'int' to 'int:4' is out of range [119]
454 s32 = 8 - bits.u3; 455 s32 = 8 - bits.u3;
455} 456}

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

--- src/usr.bin/xlint/lint1/tree.c 2024/04/27 12:46:37 1.637
+++ src/usr.bin/xlint/lint1/tree.c 2024/05/01 05:49:33 1.638
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.637 2024/04/27 12:46:37 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.638 2024/05/01 05:49:33 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) 39#if defined(__RCSID)
40__RCSID("$NetBSD: tree.c,v 1.637 2024/04/27 12:46:37 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.638 2024/05/01 05:49:33 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 51
52 52
53typedef struct integer_constraints { 53typedef struct integer_constraints {
@@ -753,32 +753,34 @@ balance(op_t op, tnode_t **lnp, tnode_t  @@ -753,32 +753,34 @@ balance(op_t op, tnode_t **lnp, tnode_t
753 tspec_t rt = (*rnp)->tn_type->t_tspec; 753 tspec_t rt = (*rnp)->tn_type->t_tspec;
754 if (!is_arithmetic(lt) || !is_arithmetic(rt)) 754 if (!is_arithmetic(lt) || !is_arithmetic(rt))
755 return; 755 return;
756 756
757 tspec_t t = allow_c90 757 tspec_t t = allow_c90
758 ? usual_arithmetic_conversion_c90(lt, rt) 758 ? usual_arithmetic_conversion_c90(lt, rt)
759 : usual_arithmetic_conversion_trad(lt, rt); 759 : usual_arithmetic_conversion_trad(lt, rt);
760 760
761 if (t != lt) 761 if (t != lt)
762 *lnp = apply_usual_arithmetic_conversions(op, *lnp, t); 762 *lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
763 if (t != rt) 763 if (t != rt)
764 *rnp = apply_usual_arithmetic_conversions(op, *rnp, t); 764 *rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
765 765
766 unsigned lw = (*lnp)->tn_type->t_bit_field_width; 766 if (is_integer(t)) {
767 unsigned rw = (*rnp)->tn_type->t_bit_field_width; 767 unsigned lw = width_in_bits((*lnp)->tn_type);
768 if (lw < rw) 768 unsigned rw = width_in_bits((*rnp)->tn_type);
769 *lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp); 769 if (lw < rw)
770 if (rw < lw) 770 *lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
771 *rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp); 771 if (rw < lw)
 772 *rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
 773 }
772} 774}
773 775
774static tnode_t * 776static tnode_t *
775build_address(bool sys, tnode_t *tn, bool force) 777build_address(bool sys, tnode_t *tn, bool force)
776{ 778{
777 tspec_t t; 779 tspec_t t;
778 780
779 if (!force && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 781 if (!force && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
780 if (!allow_c90) 782 if (!allow_c90)
781 /* '&' before array or function: ignored */ 783 /* '&' before array or function: ignored */
782 warning(127); 784 warning(127);
783 return tn; 785 return tn;
784 } 786 }