Module Name:    src
Committed By:   rillig
Date:           Fri Jan  8 01:40:03 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_204.c msg_204.exp
        src/usr.bin/xlint/lint1: func.c

Log Message:
lint: demonstrate wrong message 204 (since 2020-12-31)

In func.c 1.39 from 2020-12-31 18:51:28, the check that controlling
expressions are indeed scalar was extended from while and for loops to
if statements as well.  It just seemed to have been an oversight.

This revealed a bug in lint, which didn't accept the following valid
code snippet from lib/libpthread/pthread.c:634:

        void _malloc_thread_cleanup(void) __weak;
        ...
        if (_malloc_thread_cleanup)
                _malloc_thread_cleanup();

Testing a function (instead of a function pointer) for truthiness is
probably rare since most functions are defined unconditionally.  For
weak functions it comes in handy though.

Clang-Tidy suggests to prefix the function with '&' to silence its
warning.  Doing that revealed a non-obvious behavior in build_ampersand,
which does not add the AMPER node to the expression even though it is
clearly mentioned in the code.  That is left for further research.

Once the original bug is fixed, it probably doesn't matter whether the
AMPER is discarded or retained since check_controlling_expression would
add it back.  There's probably a reason though to sometimes discard the
AMPER and sometimes retain it.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/xlint/lint1/msg_204.c \
    src/tests/usr.bin/xlint/lint1/msg_204.exp
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/xlint/lint1/func.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_204.c
diff -u src/tests/usr.bin/xlint/lint1/msg_204.c:1.1 src/tests/usr.bin/xlint/lint1/msg_204.c:1.2
--- src/tests/usr.bin/xlint/lint1/msg_204.c:1.1	Sat Jan  2 10:22:44 2021
+++ src/tests/usr.bin/xlint/lint1/msg_204.c	Fri Jan  8 01:40:03 2021
@@ -1,7 +1,34 @@
-/*	$NetBSD: msg_204.c,v 1.1 2021/01/02 10:22:44 rillig Exp $	*/
+/*	$NetBSD: msg_204.c,v 1.2 2021/01/08 01:40:03 rillig Exp $	*/
 # 3 "msg_204.c"
 
 // Test for message: controlling expressions must have scalar type [204]
 
-TODO: "Add example code that triggers the above message."
-TODO: "Add example code that almost triggers the above message."
+extern void
+extern_function(void);
+
+void (*function_pointer)(void);
+
+/*
+ * Since func.c 1.39 from 2020-12-31 18:51:28, lint had produced an error
+ * when a controlling expression was a function.  Pointers to functions were
+ * ok though.
+ */
+void
+bug_between_2020_12_31_and_2021_01_08(void)
+{
+	if (extern_function)
+		extern_function();
+
+	/*
+	 * FIXME: For some reason, the ampersand is discarded in
+	 *  build_ampersand.  This only has a visible effect if the
+	 *  t_spec in check_controlling_expression is evaluated too early,
+	 *  as has been the case before func.c 1.52 from 2021-01-08.
+	 */
+	if (&extern_function)
+		extern_function();
+
+	/* This one has always been ok since pointers are scalar types. */
+	if (function_pointer)
+		function_pointer();
+}
Index: src/tests/usr.bin/xlint/lint1/msg_204.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_204.exp:1.1 src/tests/usr.bin/xlint/lint1/msg_204.exp:1.2
--- src/tests/usr.bin/xlint/lint1/msg_204.exp:1.1	Sat Jan  2 10:22:44 2021
+++ src/tests/usr.bin/xlint/lint1/msg_204.exp	Fri Jan  8 01:40:03 2021
@@ -1 +1,2 @@
-msg_204.c(6): syntax error ':' [249]
+msg_204.c(19): controlling expressions must have scalar type [204]
+msg_204.c(28): controlling expressions must have scalar type [204]

Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.51 src/usr.bin/xlint/lint1/func.c:1.52
--- src/usr.bin/xlint/lint1/func.c:1.51	Tue Jan  5 00:22:04 2021
+++ src/usr.bin/xlint/lint1/func.c	Fri Jan  8 01:40:03 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: func.c,v 1.51 2021/01/05 00:22:04 rillig Exp $	*/
+/*	$NetBSD: func.c,v 1.52 2021/01/08 01:40:03 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: func.c,v 1.51 2021/01/05 00:22:04 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.52 2021/01/08 01:40:03 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -537,6 +537,8 @@ default_label(void)
 static tnode_t *
 check_controlling_expression(tnode_t *tn)
 {
+	// FIXME: dereference before null check
+	// FIXME: This is evaluated too early; see test msg_204
 	tspec_t t = tn->tn_type->t_tspec;
 
 	if (tn != NULL)

Reply via email to