Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): use fixed-size arrays for Enum_ToString



details:   https://anonhg.NetBSD.org/src/rev/da530c2d0866
branches:  trunk
changeset: 936927:da530c2d0866
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 09 07:03:06 2020 +0000

description:
make(1): use fixed-size arrays for Enum_ToString

Declaring the ToStringSize as "static const size_t" made it an ordinary
integer expression.  To avoid variable length arrays, the commonly
accepted way is to declare the length as an enum constant, as that is
considered an integer constant expression, which in turn makes the
declaration a fixed-size array.

diffstat:

 usr.bin/make/enum.h |   8 +++-----
 usr.bin/make/var.c  |  16 ++++++++--------
 2 files changed, 11 insertions(+), 13 deletions(-)

diffs (79 lines):

diff -r e3d8ea097743 -r da530c2d0866 usr.bin/make/enum.h
--- a/usr.bin/make/enum.h       Sun Aug 09 06:43:29 2020 +0000
+++ b/usr.bin/make/enum.h       Sun Aug 09 07:03:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: enum.h,v 1.3 2020/08/09 02:53:21 christos Exp $        */
+/*     $NetBSD: enum.h,v 1.4 2020/08/09 07:03:06 rillig Exp $  */
 
 /*
  Copyright (c) 2020 Roland Illig <rillig%NetBSD.org@localhost>
@@ -59,10 +59,8 @@
        #v1 ENUM__SEP ENUM__JOIN_6(v2, v3, v4, v5, v6, v7)
 
 #define ENUM__RTTI(typnam, specs, joined) \
-       static const EnumToStringSpec typnam ## _ ## ToStringSpecs[] = specs; 
-#if 0
-       static const size_t typnam ## _ ## ToStringSize = sizeof joined
-#endif
+       static const EnumToStringSpec typnam ## _ ## ToStringSpecs[] = specs; \
+       enum { typnam ## _ ## ToStringSize = sizeof joined }
 
 #define ENUM__SPEC(v) { v, #v }
 
diff -r e3d8ea097743 -r da530c2d0866 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Aug 09 06:43:29 2020 +0000
+++ b/usr.bin/make/var.c        Sun Aug 09 07:03:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.435 2020/08/09 02:53:21 christos Exp $       */
+/*     $NetBSD: var.c,v 1.436 2020/08/09 07:03:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.435 2020/08/09 02:53:21 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.436 2020/08/09 07:03:06 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.435 2020/08/09 02:53:21 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.436 2020/08/09 07:03:06 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -3064,8 +3064,8 @@
        mod = p;
 
        if (DEBUG(VAR)) {
-           char vflags_str[2048];
-           char eflags_str[2048];
+           char eflags_str[VarEvalFlags_ToStringSize];
+           char vflags_str[VarFlags_ToStringSize];
            Boolean is_single_char = mod[0] != '\0' &&
                (mod[1] == endc || mod[1] == ':');
 
@@ -3211,8 +3211,8 @@
            goto bad_modifier;
 
        if (DEBUG(VAR)) {
-           char eflags_str[2048];
-           char vflags_str[2048];
+           char eflags_str[VarEvalFlags_ToStringSize];
+           char vflags_str[VarFlags_ToStringSize];
            const char *q = st.newVal == var_Error ? "" : "\"";
            const char *newVal = st.newVal == var_Error ? "error" : st.newVal;
 
@@ -3348,7 +3348,7 @@
     const char *extramodifiers;
     Var *v;
     char *nstr;
-    char eflags_str[2048];
+    char eflags_str[VarEvalFlags_ToStringSize];
 
     VAR_DEBUG("%s: %s with %s\n", __func__, str,
              Enum_ToString(eflags_str, sizeof eflags_str, eflags,



Home | Main Index | Thread Index | Old Index