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): replace snprintf with concat3



details:   https://anonhg.NetBSD.org/src/rev/bebdd92fb491
branches:  trunk
changeset: 974781:bebdd92fb491
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Aug 10 20:07:14 2020 +0000

description:
make(1): replace snprintf with concat3

This makes the code more predictable for long strings, since the main
action is never skipped.

The code also becomes simpler since there is no %s anymore, and the 3
arguments just appear in the same order as in the result.

diffstat:

 usr.bin/make/var.c |  35 +++++++++++++++--------------------
 1 files changed, 15 insertions(+), 20 deletions(-)

diffs (92 lines):

diff -r a9ee3300e56e -r bebdd92fb491 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Mon Aug 10 19:53:19 2020 +0000
+++ b/usr.bin/make/var.c        Mon Aug 10 20:07:14 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.445 2020/08/09 15:07:13 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.446 2020/08/10 20:07:14 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.445 2020/08/09 15:07:13 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.446 2020/08/10 20:07:14 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.445 2020/08/09 15:07:13 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.446 2020/08/10 20:07:14 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -500,7 +500,6 @@
 static Boolean
 Var_Export1(const char *name, VarExportFlags flags)
 {
-    char tmp[BUFSIZ];
     VarExportFlags parent = flags & VAR_EXPORT_PARENT;
     Var *v;
     char *val;
@@ -532,7 +531,7 @@
 
     val = Buf_GetAll(&v->val, NULL);
     if (!(flags & VAR_EXPORT_LITERAL) && strchr(val, '$') != NULL) {
-       int n;
+       char *expr;
 
        if (parent) {
            /*
@@ -550,12 +549,12 @@
             */
            return FALSE;
        }
-       n = snprintf(tmp, sizeof(tmp), "${%s}", name);
-       if (n < (int)sizeof(tmp)) {
-           val = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
-           setenv(name, val, 1);
-           free(val);
-       }
+
+       expr = str_concat3("${", name, "}");
+       val = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
+       setenv(name, val, 1);
+       free(val);
+       free(expr);
     } else {
        if (parent)
            v->flags &= ~VAR_REEXPORT;  /* once will do */
@@ -684,10 +683,8 @@
 void
 Var_UnExport(const char *str)
 {
-    char tmp[BUFSIZ];
     const char *varnames;
     char *varnames_freeIt;
-    int n;
     Boolean unexport_env;
 
     varnames = NULL;
@@ -758,13 +755,11 @@
             * just delete .MAKE.EXPORTED below.
             */
            if (varnames == str) {
-               n = snprintf(tmp, sizeof(tmp),
-                            "${" MAKE_EXPORTED ":N%s}", v->name);
-               if (n < (int)sizeof(tmp)) {
-                   char *cp = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
-                   Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL);
-                   free(cp);
-               }
+               char *expr = str_concat3("${" MAKE_EXPORTED ":N", v->name, "}");
+               char *cp = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
+               Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL);
+               free(cp);
+               free(expr);
            }
        }
        free(as);



Home | Main Index | Thread Index | Old Index