Sat Apr 27 12:46:37 2024 UTC (13d)
lint: converting a null pointer to another pointer type is not narrowing


(rillig)
diff -r1.28 -r1.29 src/tests/usr.bin/xlint/lint1/queries.c
diff -r1.636 -r1.637 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.28 -r1.29 src/tests/usr.bin/xlint/lint1/queries.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/queries.c 2024/04/27 10:08:54 1.28
+++ src/tests/usr.bin/xlint/lint1/queries.c 2024/04/27 12:46:37 1.29
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: queries.c,v 1.28 2024/04/27 10:08:54 rillig Exp $ */ 1/* $NetBSD: queries.c,v 1.29 2024/04/27 12:46:37 rillig Exp $ */
2# 3 "queries.c" 2# 3 "queries.c"
3 3
4/* 4/*
5 * Demonstrate the case-by-case queries. Unlike warnings, queries do not 5 * Demonstrate the case-by-case queries. Unlike warnings, queries do not
6 * point to questionable code but rather to code that may be interesting to 6 * point to questionable code but rather to code that may be interesting to
7 * inspect manually on a case-by-case basis. 7 * inspect manually on a case-by-case basis.
8 * 8 *
9 * Possible use cases are: 9 * Possible use cases are:
10 * 10 *
11 * Understanding how C works internally, by making the usual arithmetic 11 * Understanding how C works internally, by making the usual arithmetic
12 * conversions visible. 12 * conversions visible.
13 * 13 *
14 * Finding code that intentionally suppresses a regular lint warning, 14 * Finding code that intentionally suppresses a regular lint warning,
@@ -522,14 +522,16 @@ Q20_void_pointer_conversion(void) @@ -522,14 +522,16 @@ Q20_void_pointer_conversion(void)
522 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to int' [Q20] */ 522 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to int' [Q20] */
523 int_ptr = void_ptr; 523 int_ptr = void_ptr;
524 /* expect+1: redundant cast from 'pointer to void' to 'pointer to int' before assignment [Q7] */ 524 /* expect+1: redundant cast from 'pointer to void' to 'pointer to int' before assignment [Q7] */
525 int_ptr = (int *)void_ptr; 525 int_ptr = (int *)void_ptr;
526 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to char' [Q20] */ 526 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to char' [Q20] */
527 char_ptr = void_ptr; 527 char_ptr = void_ptr;
528 void_ptr = char_ptr; 528 void_ptr = char_ptr;
529 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to int' [Q20] */ 529 /* expect+1: implicit narrowing conversion from void pointer to 'pointer to int' [Q20] */
530 int_ptr = void_ptr; 530 int_ptr = void_ptr;
531 /* expect+1: warning: illegal combination of 'pointer to int' and 'pointer to char', op '=' [124] */ 531 /* expect+1: warning: illegal combination of 'pointer to int' and 'pointer to char', op '=' [124] */
532 int_ptr = char_ptr; 532 int_ptr = char_ptr;
533 /* expect+1: warning: illegal combination of 'pointer to char' and 'pointer to int', op '=' [124] */ 533 /* expect+1: warning: illegal combination of 'pointer to char' and 'pointer to int', op '=' [124] */
534 char_ptr = int_ptr; 534 char_ptr = int_ptr;
 535
 536 int_ptr = (void *)0;
535} 537}

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

--- src/usr.bin/xlint/lint1/tree.c 2024/04/27 10:08:54 1.636
+++ src/usr.bin/xlint/lint1/tree.c 2024/04/27 12:46:37 1.637
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.636 2024/04/27 10:08:54 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.637 2024/04/27 12:46:37 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.636 2024/04/27 10:08:54 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.637 2024/04/27 12:46:37 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 {
@@ -1424,27 +1424,28 @@ build_assignment(op_t op, bool sys, tnod @@ -1424,27 +1424,28 @@ build_assignment(op_t op, bool sys, tnod
1424 && portable_rank_cmp(lt, rt) < 0) 1424 && portable_rank_cmp(lt, rt) < 0)
1425 /* semantics of '%s' change in C90; ... */ 1425 /* semantics of '%s' change in C90; ... */
1426 warning(118, "<<="); 1426 warning(118, "<<=");
1427 1427
1428 if (op != SHLASS && op != SHRASS 1428 if (op != SHLASS && op != SHRASS
1429 && (op == ASSIGN || lt != PTR) 1429 && (op == ASSIGN || lt != PTR)
1430 && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) { 1430 && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
1431 rn = convert(op, 0, ln->tn_type, rn); 1431 rn = convert(op, 0, ln->tn_type, rn);
1432 rt = lt; 1432 rt = lt;
1433 } 1433 }
1434 1434
1435 if (is_query_enabled[20] 1435 if (is_query_enabled[20]
1436 && lt == PTR && ln->tn_type->t_subt->t_tspec != VOID 1436 && lt == PTR && ln->tn_type->t_subt->t_tspec != VOID
1437 && rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) 1437 && rt == PTR && rn->tn_type->t_subt->t_tspec == VOID
 1438 && !is_null_pointer(rn))
1438 /* implicit narrowing conversion from void ... */ 1439 /* implicit narrowing conversion from void ... */
1439 query_message(20, type_name(ln->tn_type)); 1440 query_message(20, type_name(ln->tn_type));
1440 1441
1441 if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast && 1442 if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
1442 types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) && 1443 types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
1443 is_cast_redundant(rn)) { 1444 is_cast_redundant(rn)) {
1444 /* redundant cast from '%s' to '%s' before assignment */ 1445 /* redundant cast from '%s' to '%s' before assignment */
1445 query_message(7, type_name(rn->u.ops.left->tn_type), 1446 query_message(7, type_name(rn->u.ops.left->tn_type),
1446 type_name(rn->tn_type)); 1447 type_name(rn->tn_type));
1447 } 1448 }
1448 1449
1449 return build_op(op, sys, ln->tn_type, ln, rn); 1450 return build_op(op, sys, ln->tn_type, ln, rn);
1450} 1451}