Module Name: src Committed By: rillig Date: Sat Oct 9 20:03:20 UTC 2021
Modified Files: src/tests/usr.bin/xlint/lint1: msg_346.c msg_346.exp src/usr.bin/xlint/lint1: tree.c Log Message: lint: warn that bsearch effectively discards 'const' Just like strchr, bsearch takes a const pointer and returns a non-const pointer into the same object. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/tests/usr.bin/xlint/lint1/msg_346.c cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_346.exp cvs rdiff -u -r1.383 -r1.384 src/usr.bin/xlint/lint1/tree.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/msg_346.c diff -u src/tests/usr.bin/xlint/lint1/msg_346.c:1.5 src/tests/usr.bin/xlint/lint1/msg_346.c:1.6 --- src/tests/usr.bin/xlint/lint1/msg_346.c:1.5 Sat Oct 9 19:18:52 2021 +++ src/tests/usr.bin/xlint/lint1/msg_346.c Sat Oct 9 20:03:20 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: msg_346.c,v 1.5 2021/10/09 19:18:52 rillig Exp $ */ +/* $NetBSD: msg_346.c,v 1.6 2021/10/09 20:03:20 rillig Exp $ */ # 3 "msg_346.c" // Test for message: call to '%s' effectively discards 'const' from argument [346] @@ -71,18 +71,18 @@ void *bsearch(const void *key, const voi int cmp(const void *, const void *); void take_void_ptr(void *); -void take_const_void_ptr(void *); +void take_const_void_ptr(const void *); void bsearch_example(void) { + int arr[] = { 1 }; const int const_arr[] = { 1 }; - const int arr[] = { 1 }; take_const_void_ptr(bsearch("", const_arr, 4, 1, cmp)); take_const_void_ptr(bsearch("", arr, 4, 1, cmp)); take_void_ptr(bsearch("", arr, 4, 1, cmp)); - /* TODO: expect+1: warning: call to 'bsearch' effectively discards 'const' from argument [346] */ + /* expect+1: warning: call to 'bsearch' effectively discards 'const' from argument [346] */ take_void_ptr(bsearch("", const_arr, 4, 1, cmp)); } Index: src/tests/usr.bin/xlint/lint1/msg_346.exp diff -u src/tests/usr.bin/xlint/lint1/msg_346.exp:1.4 src/tests/usr.bin/xlint/lint1/msg_346.exp:1.5 --- src/tests/usr.bin/xlint/lint1/msg_346.exp:1.4 Mon Aug 16 18:51:58 2021 +++ src/tests/usr.bin/xlint/lint1/msg_346.exp Sat Oct 9 20:03:20 2021 @@ -7,3 +7,4 @@ msg_346.c(48): warning: call to 'strpbrk msg_346.c(50): warning: call to 'strrchr' effectively discards 'const' from argument [346] msg_346.c(52): warning: call to 'strstr' effectively discards 'const' from argument [346] msg_346.c(60): error: argument mismatch: 0 arg passed, 2 expected [150] +msg_346.c(87): warning: call to 'bsearch' effectively discards 'const' from argument [346] Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.383 src/usr.bin/xlint/lint1/tree.c:1.384 --- src/usr.bin/xlint/lint1/tree.c:1.383 Sun Sep 26 14:52:37 2021 +++ src/usr.bin/xlint/lint1/tree.c Sat Oct 9 20:03:20 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: tree.c,v 1.383 2021/09/26 14:52:37 rillig Exp $ */ +/* $NetBSD: tree.c,v 1.384 2021/10/09 20:03:20 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: tree.c,v 1.383 2021/09/26 14:52:37 rillig Exp $"); +__RCSID("$NetBSD: tree.c,v 1.384 2021/10/09 20:03:20 rillig Exp $"); #endif #include <float.h> @@ -1412,7 +1412,16 @@ is_const_char_pointer(const tnode_t *tn) } static bool -is_first_arg_const(const tnode_t *tn) +is_const_pointer(const tnode_t *tn) +{ + const type_t *tp; + + tp = before_conversion(tn)->tn_type; + return tp->t_tspec == PTR && tp->t_subt->t_const; +} + +static bool +is_first_arg_const_char_pointer(const tnode_t *tn) { const tnode_t *an; @@ -1425,6 +1434,20 @@ is_first_arg_const(const tnode_t *tn) return is_const_char_pointer(an->tn_left); } +static bool +is_second_arg_const_pointer(const tnode_t *tn) +{ + const tnode_t *an; + + an = tn->tn_right; + if (an == NULL || an->tn_right == NULL) + return false; + + while (an->tn_right->tn_right != NULL) + an = an->tn_right; + return is_const_pointer(an->tn_left); +} + static void check_unconst_function(const type_t *lstp, const tnode_t *rn) { @@ -1433,7 +1456,15 @@ check_unconst_function(const type_t *lst if (lstp->t_tspec == CHAR && !lstp->t_const && is_direct_function_call(rn, &function_name) && is_unconst_function(function_name) && - is_first_arg_const(rn)) { + is_first_arg_const_char_pointer(rn)) { + /* call to '%s' effectively discards 'const' from argument */ + warning(346, function_name); + } + + if (!lstp->t_const && + is_direct_function_call(rn, &function_name) && + strcmp(function_name, "bsearch") == 0 && + is_second_arg_const_pointer(rn)) { /* call to '%s' effectively discards 'const' from argument */ warning(346, function_name); }