Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src lint: demonstrate wrong handling of conversion to _Bool



details:   https://anonhg.NetBSD.org/src/rev/a4220dc140a8
branches:  trunk
changeset: 949344:a4220dc140a8
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 10 11:24:42 2021 +0000

description:
lint: demonstrate wrong handling of conversion to _Bool

diffstat:

 distrib/sets/lists/tests/mi                |   4 +-
 tests/usr.bin/xlint/lint1/Makefile         |   4 +-
 tests/usr.bin/xlint/lint1/d_c99_bool.c     |  49 ++++++++++++++++++++++++++++++
 tests/usr.bin/xlint/lint1/d_c99_bool.exp   |  17 ++++++++++
 tests/usr.bin/xlint/lint1/t_integration.sh |   3 +-
 5 files changed, 74 insertions(+), 3 deletions(-)

diffs (127 lines):

diff -r e8a21febba22 -r a4220dc140a8 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Jan 10 11:17:53 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Jan 10 11:24:42 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1007 2021/01/02 10:22:42 rillig Exp $
+# $NetBSD: mi,v 1.1008 2021/01/10 11:24:42 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -5745,6 +5745,8 @@
 ./usr/tests/usr.bin/xlint/lint1/d_bltinoffsetof.c              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_anon_struct.c            tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_anon_union.c             tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/d_c99_bool.c                   tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/d_c99_bool.exp                 tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_complex_num.c            tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_complex_split.c          tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/d_c99_compound_literal_comma.c tests-usr.bin-tests     compattestfile,atf
diff -r e8a21febba22 -r a4220dc140a8 tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 10 11:17:53 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 10 11:24:42 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.25 2021/01/02 10:22:42 rillig Exp $
+# $NetBSD: Makefile,v 1.26 2021/01/10 11:24:42 rillig Exp $
 
 NOMAN=         # defined
 
@@ -11,6 +11,8 @@
 FILESDIR=      ${TESTSDIR}
 FILES+=                d_alignof.c
 FILES+=                d_bltinoffsetof.c
+FILES+=                d_c99_bool.c
+FILES+=                d_c99_bool.exp
 FILES+=                d_c99_anon_struct.c
 FILES+=                d_c99_anon_union.c
 FILES+=                d_c99_complex_num.c
diff -r e8a21febba22 -r a4220dc140a8 tests/usr.bin/xlint/lint1/d_c99_bool.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool.c    Sun Jan 10 11:24:42 2021 +0000
@@ -0,0 +1,49 @@
+/*     $NetBSD: d_c99_bool.c,v 1.1 2021/01/10 11:24:42 rillig Exp $    */
+# 3 "d_bool.c"
+
+/*
+ * C99 6.3.1.2 says: "When any scalar value is converted to _Bool, the result
+ * is 0 if the value compares equal to 0; otherwise the result is 1."
+ *
+ * This is different from the other integer types, which get truncated or
+ * invoke undefined behavior.
+ */
+
+/* Below, the wrong assertions produce warning 20. */
+
+int int_0_converts_to_false[(_Bool)0 ? -1 : 1];
+int int_0_converts_to_true_[(_Bool)0 ? 1 : -1];
+
+int int_1_converts_to_false[(_Bool)1 ? -1 : 1];
+int int_1_converts_to_true_[(_Bool)1 ? 1 : -1];
+
+int int_2_converts_to_false[(_Bool)2 ? -1 : 1];
+int int_2_converts_to_true_[(_Bool)2 ? 1 : -1];
+
+int int_256_converts_to_false[(_Bool)256 ? -1 : 1]; // FIXME
+int int_256_converts_to_true_[(_Bool)256 ? 1 : -1]; // FIXME
+
+int null_pointer_converts_to_false[(_Bool)(void *)0 ? -1 : 1];
+int null_pointer_converts_to_true_[(_Bool)(void *)0 ? 1 : -1];
+
+int nonnull_pointer_converts_to_false[(_Bool)"not null" ? -1 : 1]; // FIXME 133
+int nonnull_pointer_converts_to_true_[(_Bool)"not null" ? 1 : -1]; // FIXME 133
+
+int double_minus_1_0_converts_to_false[(_Bool)-1.0 ? -1 : 1]; // FIXME 119
+int double_minus_1_0_converts_to_true_[(_Bool)-1.0 ? 1 : -1]; // FIXME 20, 119
+
+int double_minus_0_5_converts_to_false[(_Bool)-0.5 ? -1 : 1]; // FIXME 119
+int double_minus_0_5_converts_to_true_[(_Bool)-0.5 ? 1 : -1]; // FIXME 20, 119
+
+int double_minus_0_0_converts_to_false[(_Bool)-0.0 ? -1 : 1];
+int double_minus_0_0_converts_to_true_[(_Bool)-0.0 ? 1 : -1];
+
+int double_0_0_converts_to_false[(_Bool)0.0 ? -1 : 1];
+int double_0_0_converts_to_true_[(_Bool)0.0 ? 1 : -1];
+
+/* The C99 rationale explains in 6.3.1.2 why (_Bool)0.5 is true. */
+int double_0_5_converts_to_false[(_Bool)0.5 ? -1 : 1]; // FIXME 20
+int double_0_5_converts_to_true_[(_Bool)0.5 ? 1 : -1]; // FIXME 20
+
+int double_1_0_converts_to_false[(_Bool)1.0 ? -1 : 1];
+int double_1_0_converts_to_true_[(_Bool)1.0 ? 1 : -1];
diff -r e8a21febba22 -r a4220dc140a8 tests/usr.bin/xlint/lint1/d_c99_bool.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool.exp  Sun Jan 10 11:24:42 2021 +0000
@@ -0,0 +1,17 @@
+d_bool.c(15): negative array dimension (-1) [20]
+d_bool.c(17): negative array dimension (-1) [20]
+d_bool.c(20): negative array dimension (-1) [20]
+d_bool.c(24): negative array dimension (-1) [20]
+d_bool.c(27): negative array dimension (-1) [20]
+d_bool.c(29): warning: conversion of pointer to '_Bool' loses bits [133]
+d_bool.c(30): warning: conversion of pointer to '_Bool' loses bits [133]
+d_bool.c(32): warning: conversion of 'double' to '_Bool' is out of range [119]
+d_bool.c(33): warning: conversion of 'double' to '_Bool' is out of range [119]
+d_bool.c(33): negative array dimension (-1) [20]
+d_bool.c(35): warning: conversion of 'double' to '_Bool' is out of range [119]
+d_bool.c(36): warning: conversion of 'double' to '_Bool' is out of range [119]
+d_bool.c(36): negative array dimension (-1) [20]
+d_bool.c(39): negative array dimension (-1) [20]
+d_bool.c(42): negative array dimension (-1) [20]
+d_bool.c(46): negative array dimension (-1) [20]
+d_bool.c(48): negative array dimension (-1) [20]
diff -r e8a21febba22 -r a4220dc140a8 tests/usr.bin/xlint/lint1/t_integration.sh
--- a/tests/usr.bin/xlint/lint1/t_integration.sh        Sun Jan 10 11:17:53 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/t_integration.sh        Sun Jan 10 11:24:42 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_integration.sh,v 1.21 2021/01/09 14:33:53 rillig Exp $
+# $NetBSD: t_integration.sh,v 1.22 2021/01/10 11:24:42 rillig Exp $
 #
 # Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -65,6 +65,7 @@
 test_case bltinoffsetof
 test_case c99_anon_struct
 test_case c99_anon_union
+test_case c99_bool
 test_case c99_compound_literal_comma
 test_case c99_decls_after_stmt2
 test_case c99_flex_array_packed



Home | Main Index | Thread Index | Old Index