Module Name: src Committed By: rillig Date: Mon May 30 08:14:53 UTC 2022
Modified Files: src/tests/usr.bin/xlint/lint1: msg_168.c msg_168.exp src/usr.bin/xlint/lint1: tree.c Log Message: 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. To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/msg_168.c \ src/tests/usr.bin/xlint/lint1/msg_168.exp cvs rdiff -u -r1.451 -r1.452 src/usr.bin/xlint/lint1/tree.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_168.c diff -u src/tests/usr.bin/xlint/lint1/msg_168.c:1.6 src/tests/usr.bin/xlint/lint1/msg_168.c:1.7 --- src/tests/usr.bin/xlint/lint1/msg_168.c:1.6 Mon May 30 08:04:00 2022 +++ src/tests/usr.bin/xlint/lint1/msg_168.c Mon May 30 08:14:53 2022 @@ -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 @@ s_init(void) { 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]; } Index: src/tests/usr.bin/xlint/lint1/msg_168.exp diff -u src/tests/usr.bin/xlint/lint1/msg_168.exp:1.6 src/tests/usr.bin/xlint/lint1/msg_168.exp:1.7 --- src/tests/usr.bin/xlint/lint1/msg_168.exp:1.6 Mon May 30 08:04:00 2022 +++ src/tests/usr.bin/xlint/lint1/msg_168.exp Mon May 30 08:14:53 2022 @@ -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] Index: src/usr.bin/xlint/lint1/tree.c diff -u src/usr.bin/xlint/lint1/tree.c:1.451 src/usr.bin/xlint/lint1/tree.c:1.452 --- src/usr.bin/xlint/lint1/tree.c:1.451 Mon May 30 07:19:28 2022 +++ src/usr.bin/xlint/lint1/tree.c Mon May 30 08:14:52 2022 @@ -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 @@ build_plus_minus(op_t op, bool sys, tnod 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 @@ build_plus_minus(op_t op, bool sys, tnod 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 @@ check_expr_misc(const tnode_t *tn, bool 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 @@ check_array_index(tnode_t *tn, bool ampe 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 */