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: revert 'do not pre-multiply pointe...



details:   https://anonhg.NetBSD.org/src/rev/b8d0aef9e657
branches:  trunk
changeset: 366543:b8d0aef9e657
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon May 30 08:14:52 2022 +0000

description:
lint: revert 'do not pre-multiply pointer expressions' from 2022-05-26

In tree.c 1.448, removing the pre-multiplication generated wrong
warnings about out-of-bounds array access.

diffstat:

 tests/usr.bin/xlint/lint1/msg_168.c   |   6 +---
 tests/usr.bin/xlint/lint1/msg_168.exp |   4 ---
 usr.bin/xlint/lint1/tree.c            |  45 +++++++++++++++++++++++++++-------
 3 files changed, 36 insertions(+), 19 deletions(-)

diffs (121 lines):

diff -r f10366138fd8 -r b8d0aef9e657 tests/usr.bin/xlint/lint1/msg_168.c
--- a/tests/usr.bin/xlint/lint1/msg_168.c       Mon May 30 08:04:00 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_168.c       Mon May 30 08:14:52 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_168.c,v 1.6 2022/05/30 08:04:00 rillig Exp $       */
+/*     $NetBSD: msg_168.c,v 1.7 2022/05/30 08:14:53 rillig Exp $       */
 # 3 "msg_168.c"
 
 // Test for message: array subscript cannot be > %d: %ld [168]
@@ -55,13 +55,9 @@
 {
        struct s s[1];
        s->offset_0 = 1;
-       /* expect+1: warning: array subscript cannot be > 0: 1 [168] */
        s->offset_1 = 2;
-       /* expect+1: warning: array subscript cannot be > 0: 4 [168] */
        s->offset_4 = 3;
-       /* expect+1: warning: array subscript cannot be > 0: 8 [168] */
        s->offset_8 = 4;
-       /* expect+1: warning: array subscript cannot be > 0: 10 [168] */
        s->offset_10 = 5;
        return s[0];
 }
diff -r f10366138fd8 -r b8d0aef9e657 tests/usr.bin/xlint/lint1/msg_168.exp
--- a/tests/usr.bin/xlint/lint1/msg_168.exp     Mon May 30 08:04:00 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_168.exp     Mon May 30 08:14:52 2022 +0000
@@ -1,6 +1,2 @@
 msg_168.c(28): warning: array subscript cannot be > 19: 20 [168]
 msg_168.c(41): warning: array subscript cannot be > 57: 58 [168]
-msg_168.c(59): warning: array subscript cannot be > 0: 1 [168]
-msg_168.c(61): warning: array subscript cannot be > 0: 4 [168]
-msg_168.c(63): warning: array subscript cannot be > 0: 8 [168]
-msg_168.c(65): warning: array subscript cannot be > 0: 10 [168]
diff -r f10366138fd8 -r b8d0aef9e657 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Mon May 30 08:04:00 2022 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Mon May 30 08:14:52 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.451 2022/05/30 07:19:28 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.452 2022/05/30 08:14:52 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.451 2022/05/30 07:19:28 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.452 2022/05/30 08:14:52 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3152,7 +3152,12 @@
                tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
                if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec)
                        rn = convert(NOOP, 0, elsz->tn_type, rn);
-               return new_tnode(op, sys, ln->tn_type, ln, rn);
+
+               tnode_t *prod = new_tnode(MULT, sys, rn->tn_type, rn, elsz);
+               if (rn->tn_op == CON)
+                       prod = fold(prod);
+
+               return new_tnode(op, sys, ln->tn_type, ln, prod);
        }
 
        /* pointer - pointer */
@@ -3161,10 +3166,14 @@
                lint_assert(op == MINUS);
 
                type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
-               tnode_t *diff = new_tnode(MINUS, sys, ptrdiff, ln, rn);
+               tnode_t *raw_diff = new_tnode(op, sys, ptrdiff, ln, rn);
                if (ln->tn_op == CON && rn->tn_op == CON)
-                       diff = fold(diff);
-               return diff;
+                       raw_diff = fold(raw_diff);
+
+               tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
+               balance(NOOP, &raw_diff, &elsz);
+
+               return new_tnode(DIV, sys, ptrdiff, raw_diff, elsz);
        }
 
        return new_tnode(op, sys, ln->tn_type, ln, rn);
@@ -4432,8 +4441,13 @@
 static void
 check_array_index(tnode_t *tn, bool amper)
 {
-       tnode_t *ln = tn->tn_left;
-       tnode_t *rn = tn->tn_right;
+       int     dim;
+       tnode_t *ln, *rn;
+       int     elsz;
+       int64_t con;
+
+       ln = tn->tn_left;
+       rn = tn->tn_right;
 
        /* We can only check constant indices. */
        if (rn->tn_op != CON)
@@ -4454,8 +4468,19 @@
        if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
                return;
 
-       int64_t con = rn->tn_val->v_quad;
-       int dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
+       /* Get the size of one array element */
+       if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
+               return;
+       elsz /= CHAR_SIZE;
+
+       /* Change the unit of the index from bytes to element size. */
+       if (is_uinteger(rn->tn_type->t_tspec)) {
+               con = (uint64_t)rn->tn_val->v_quad / elsz;
+       } else {
+               con = rn->tn_val->v_quad / elsz;
+       }
+
+       dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
 
        if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
                /* array subscript cannot be negative: %ld */



Home | Main Index | Thread Index | Old Index