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: add error about out-of-bounds arra...



details:   https://anonhg.NetBSD.org/src/rev/7d9a47745026
branches:  trunk
changeset: 982086:7d9a47745026
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 30 16:07:07 2021 +0000

description:
lint: add error about out-of-bounds array subscripts

This check is not strictly necessary since any C99 compiler must
diagnose them as well, it is rather meant for demonstrating how to do
the check in lint, and for symmetry with the 'unknown member' error
message.  These provide insight into how the data structures in init.c
are meant to be accessed.

diffstat:

 tests/usr.bin/xlint/lint1/d_c99_init.c   |  12 ++++++------
 tests/usr.bin/xlint/lint1/d_c99_init.exp |   3 +++
 usr.bin/xlint/lint1/init.c               |   9 +++++++--
 3 files changed, 16 insertions(+), 8 deletions(-)

diffs (81 lines):

diff -r 0e2238c9b6f0 -r 7d9a47745026 tests/usr.bin/xlint/lint1/d_c99_init.c
--- a/tests/usr.bin/xlint/lint1/d_c99_init.c    Tue Mar 30 15:31:51 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_init.c    Tue Mar 30 16:07:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: d_c99_init.c,v 1.26 2021/03/30 15:18:19 rillig Exp $   */
+/*     $NetBSD: d_c99_init.c,v 1.27 2021/03/30 16:07:07 rillig Exp $   */
 # 3 "d_c99_init.c"
 
 /*
@@ -135,7 +135,7 @@
 struct point point_with_mixed_designators = {
        .x = 3,
        4,
-       /* TODO: remove me */
+
        5,                      /* expect: too many struct/union initializers */
        .x = 3,
 };
@@ -211,15 +211,15 @@
  * structs.
  */
 struct geometry geometry = {
-       /* TODO: remove me */
+
        .pentagons[0].points[4].x = 1,
        .points[0][0][0] = { 0, 0 },
        .points[2][4][1] = {301, 302 },
-       /* TODO: expect+1: array index 3 must be between 0 and 2 */
+       /* expect+1: array subscript cannot be > 2: 3 */
        .points[3][0][0] = {3001, 3002 },
-       /* TODO: expect+1: array index 5 must be between 0 and 4 */
+       /* expect+1: array subscript cannot be > 4: 5 */
        .points[0][5][0] = {501, 502 },
-       /* TODO: expect+1: array index 2 must be between 0 and 1 */
+       /* expect+1: array subscript cannot be > 1: 2 */
        .points[0][0][2] = {21, 22 },
 };
 
diff -r 0e2238c9b6f0 -r 7d9a47745026 tests/usr.bin/xlint/lint1/d_c99_init.exp
--- a/tests/usr.bin/xlint/lint1/d_c99_init.exp  Tue Mar 30 15:31:51 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_init.exp  Tue Mar 30 16:07:07 2021 +0000
@@ -3,6 +3,9 @@
 d_c99_init.c(80): error: too many array initializers, expected 3 [173]
 d_c99_init.c(139): error: too many struct/union initializers [172]
 d_c99_init.c(145): error: syntax error 'designator '.member' is only for struct/union' [249]
+d_c99_init.c(219): error: array subscript cannot be > 2: 3 [168]
+d_c99_init.c(221): error: array subscript cannot be > 4: 5 [168]
+d_c99_init.c(223): error: array subscript cannot be > 1: 2 [168]
 d_c99_init.c(232): error: too many struct/union initializers [172]
 d_c99_init.c(238): warning: illegal combination of integer (char) and pointer (pointer to char) [183]
 d_c99_init.c(242): warning: illegal combination of integer (char) and pointer (pointer to char) [183]
diff -r 0e2238c9b6f0 -r 7d9a47745026 usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c        Tue Mar 30 15:31:51 2021 +0000
+++ b/usr.bin/xlint/lint1/init.c        Tue Mar 30 16:07:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init.c,v 1.182 2021/03/30 15:18:19 rillig Exp $        */
+/*     $NetBSD: init.c,v 1.183 2021/03/30 16:07:07 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: init.c,v 1.182 2021/03/30 15:18:19 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.183 2021/03/30 16:07:07 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -455,6 +455,11 @@
                        error(249,
                            "designator '.member' is only for struct/union");
                }
+               if (!tp->t_incomplete_array &&
+                   dr->dr_subscript >= (size_t)tp->t_dim) {
+                       /* array subscript cannot be > %d: %ld */
+                       error(168, tp->t_dim - 1, (long)dr->dr_subscript);
+               }
                return tp->t_subt;
        default:
                /* syntax error '%s' */



Home | Main Index | Thread Index | Old Index