Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/xlint/lint1 lint: warn that bsearch effectively disc...
details: https://anonhg.NetBSD.org/src/rev/9c2eff5827e6
branches: trunk
changeset: 1024088:9c2eff5827e6
user: rillig <rillig%NetBSD.org@localhost>
date: Sat Oct 09 20:03:20 2021 +0000
description:
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.
diffstat:
tests/usr.bin/xlint/lint1/msg_346.c | 8 +++---
tests/usr.bin/xlint/lint1/msg_346.exp | 1 +
usr.bin/xlint/lint1/tree.c | 39 +++++++++++++++++++++++++++++++---
3 files changed, 40 insertions(+), 8 deletions(-)
diffs (113 lines):
diff -r 01c16f07e299 -r 9c2eff5827e6 tests/usr.bin/xlint/lint1/msg_346.c
--- a/tests/usr.bin/xlint/lint1/msg_346.c Sat Oct 09 20:00:41 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_346.c Sat Oct 09 20:03:20 2021 +0000
@@ -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 @@
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));
}
diff -r 01c16f07e299 -r 9c2eff5827e6 tests/usr.bin/xlint/lint1/msg_346.exp
--- a/tests/usr.bin/xlint/lint1/msg_346.exp Sat Oct 09 20:00:41 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_346.exp Sat Oct 09 20:03:20 2021 +0000
@@ -7,3 +7,4 @@
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]
diff -r 01c16f07e299 -r 9c2eff5827e6 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c Sat Oct 09 20:00:41 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c Sat Oct 09 20:03:20 2021 +0000
@@ -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 @@
}
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 @@
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 @@
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);
}
Home |
Main Index |
Thread Index |
Old Index