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: fix missing initialization for cas...



details:   https://anonhg.NetBSD.org/src/rev/157de4e7d955
branches:  trunk
changeset: 374436:157de4e7d955
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Apr 22 20:17:19 2023 +0000

description:
lint: fix missing initialization for cast to union

The left operand of a unary AST node must not be NULL.

The previous code crashed lint when run with some query enabled, as
is_cast_redundant assumes that a non-null AST node has valid operands.

$ cat <<EOF > crash.c
double
demo(void) {
        union u {
                double *num;
        } u;
        u = (union u)&((double) { 0.0 });
        return *u.num;
}
EOF
$ /usr/libexec/lint1 -w -S -g -q8 crash.c /dev/null

diffstat:

 usr.bin/xlint/lint1/debug.c |  8 +++++---
 usr.bin/xlint/lint1/tree.c  |  7 ++++---
 2 files changed, 9 insertions(+), 6 deletions(-)

diffs (65 lines):

diff -r 9332b193eb19 -r 157de4e7d955 usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c       Sat Apr 22 19:45:04 2023 +0000
+++ b/usr.bin/xlint/lint1/debug.c       Sat Apr 22 20:17:19 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.29 2023/04/22 17:49:15 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.30 2023/04/22 20:17:19 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.29 2023/04/22 17:49:15 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.30 2023/04/22 20:17:19 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -205,8 +205,10 @@ debug_node(const tnode_t *tn) // NOLINT(
                debug_printf("\n");
 
                debug_indent_inc();
+               lint_assert(tn->tn_left != NULL);
                debug_node(tn->tn_left);
-               if (is_binary(tn) || tn->tn_right != NULL)
+               lint_assert(is_binary(tn) == (tn->tn_right != NULL));
+               if (tn->tn_right != NULL)
                        debug_node(tn->tn_right);
                debug_indent_dec();
        }
diff -r 9332b193eb19 -r 157de4e7d955 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sat Apr 22 19:45:04 2023 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Apr 22 20:17:19 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.517 2023/04/22 17:49:15 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.518 2023/04/22 20:17:19 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.517 2023/04/22 17:49:15 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.518 2023/04/22 20:17:19 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -4049,7 +4049,7 @@ build_alignof(const type_t *tp)
 }
 
 static tnode_t *
-cast_to_union(const tnode_t *otn, type_t *ntp)
+cast_to_union(tnode_t *otn, type_t *ntp)
 {
 
        if (!allow_gcc) {
@@ -4066,6 +4066,7 @@ cast_to_union(const tnode_t *otn, type_t
                        ntn->tn_op = CVT;
                        ntn->tn_type = ntp;
                        ntn->tn_cast = true;
+                       ntn->tn_left = otn;
                        ntn->tn_right = NULL;
                        return ntn;
                }



Home | Main Index | Thread Index | Old Index