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 initialization of array of unk...



details:   https://anonhg.NetBSD.org/src/rev/909cb88363c9
branches:  trunk
changeset: 984319:909cb88363c9
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jul 02 22:46:43 2021 +0000

description:
lint: fix initialization of array of unknown size

The size of the resulting array was computed wrong.

diffstat:

 tests/usr.bin/xlint/lint1/c99_init_array.c   |   4 +-
 tests/usr.bin/xlint/lint1/c99_init_array.exp |   1 +
 usr.bin/xlint/lint1/init.c                   |  38 ++++++++++++++++++---------
 3 files changed, 28 insertions(+), 15 deletions(-)

diffs (100 lines):

diff -r 0a09446d22a2 -r 909cb88363c9 tests/usr.bin/xlint/lint1/c99_init_array.c
--- a/tests/usr.bin/xlint/lint1/c99_init_array.c        Fri Jul 02 21:52:36 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/c99_init_array.c        Fri Jul 02 22:46:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: c99_init_array.c,v 1.1 2021/07/02 21:52:36 rillig Exp $        */
+/*     $NetBSD: c99_init_array.c,v 1.2 2021/07/02 22:46:43 rillig Exp $        */
 # 3 "c99_init_array.c"
 
 /*
@@ -11,7 +11,7 @@
 // one mentioned.
 int arr_11[] = { [10] = 10, [0] = 0 };
 typedef int ctassert_11[sizeof(arr_11) / sizeof(arr_11[0]) == 11 ? -1 : 1];
-/* TODO: expect-1: error: negative array dimension (-1) [20] */
+/* expect-1: error: negative array dimension (-1) [20] */
 
 // Without an explicit subscript designator, the subscript counts up.
 int arr_3[] = { [1] = 1, [0] = 0, 1, 2 };
diff -r 0a09446d22a2 -r 909cb88363c9 tests/usr.bin/xlint/lint1/c99_init_array.exp
--- a/tests/usr.bin/xlint/lint1/c99_init_array.exp      Fri Jul 02 21:52:36 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/c99_init_array.exp      Fri Jul 02 22:46:43 2021 +0000
@@ -1,1 +1,2 @@
+c99_init_array.c(13): error: negative array dimension (-1) [20]
 c99_init_array.c(18): error: negative array dimension (-1) [20]
diff -r 0a09446d22a2 -r 909cb88363c9 usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c        Fri Jul 02 21:52:36 2021 +0000
+++ b/usr.bin/xlint/lint1/init.c        Fri Jul 02 22:46:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init.c,v 1.200 2021/06/29 21:05:32 rillig Exp $        */
+/*     $NetBSD: init.c,v 1.201 2021/07/02 22:46:43 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.200 2021/06/29 21:05:32 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.201 2021/07/02 22:46:43 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -118,14 +118,29 @@
  * See C99 6.7.8p17.
  */
 struct brace_level {
-       const type_t    *bl_type;       /* The type of the current object that
-                                        * is initialized at this brace
-                                        * level. */
+       /*
+        * The type of the current object that is initialized at this brace
+        * level.
+        */
+       const type_t    *bl_type;
 
        struct designation bl_designation;      /* .member[123].member */
 
-       const sym_t     *bl_member;             /* for structs and unions */
-       size_t          bl_subscript;           /* for arrays */
+       /*
+        * The next member of the struct or union that is to be initialized,
+        * unless a specific member is selected by a designator.
+        */
+       const sym_t     *bl_member;
+       /*
+        * The subscript of the next array element that is to be initialized,
+        * unless a specific subscript is selected by a designator.
+        */
+       size_t          bl_subscript;
+       /*
+        * The maximum subscript that has ever be seen; only relevant for an
+        * array of unknown size at the outermost brace level.
+        */
+       size_t          bl_max_subscript;
        bool            bl_scalar_done: 1;      /* for scalars */
        bool            bl_confused: 1;         /* skip further checks */
 
@@ -700,6 +715,8 @@
                break;
        case ARRAY:
                bl->bl_subscript++;
+               if (bl->bl_subscript > bl->bl_max_subscript)
+                       bl->bl_max_subscript = bl->bl_subscript;
                break;
        default:
                bl->bl_scalar_done = true;
@@ -822,12 +839,7 @@
        if (in->in_sym->s_type->t_incomplete_array &&
            in->in_brace_level->bl_enclosing == NULL)
                update_type_of_array_of_unknown_size(in->in_sym,
-                   in->in_brace_level->bl_subscript);
-       /*
-        * XXX: bl_subscript is not entirely correct.
-        * It should rather be max(actually used subscript) + 1.
-        * int arr[] = { [100] = 100, [0] = 0 };
-        */
+                   in->in_brace_level->bl_max_subscript);
 }
 
 static void



Home | Main Index | Thread Index | Old Index