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 tricky malloc+realloc+strlen+s...



details:   https://anonhg.NetBSD.org/src/rev/74df6983b159
branches:  trunk
changeset: 937693:74df6983b159
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 23 17:49:37 2020 +0000

description:
make(1): replace tricky malloc+realloc+strlen+snprintf with Buffer

The code for handling archives is not widely used.  Therefore it does
not need to be fast.  Clarity of the code is more important.  Therefore
replace the malloc + strlen + realloc + snprintf string processing with
the Buffer type, which removes a lot of redundancy.

In the wildcard loop, the "if (sz > nsz)" looked like a mistake.  Why
should it be useful to first allocate a large buffer and then resize it
to a smaller buffer, but still twice as large as necessary?

diffstat:

 usr.bin/make/arch.c |  42 ++++++++++++++++++++++++------------------
 1 files changed, 24 insertions(+), 18 deletions(-)

diffs (91 lines):

diff -r a8f4dee4342c -r 74df6983b159 usr.bin/make/arch.c
--- a/usr.bin/make/arch.c       Sun Aug 23 17:34:46 2020 +0000
+++ b/usr.bin/make/arch.c       Sun Aug 23 17:49:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arch.c,v 1.92 2020/08/23 16:58:02 rillig Exp $ */
+/*     $NetBSD: arch.c,v 1.93 2020/08/23 17:49:37 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: arch.c,v 1.92 2020/08/23 16:58:02 rillig Exp $";
+static char rcsid[] = "$NetBSD: arch.c,v 1.93 2020/08/23 17:49:37 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)arch.c     8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: arch.c,v 1.92 2020/08/23 16:58:02 rillig Exp $");
+__RCSID("$NetBSD: arch.c,v 1.93 2020/08/23 17:49:37 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -241,7 +241,6 @@
     GNode          *gn;            /* New node */
     char           *libName;       /* Library-part of specification */
     char           *memName;       /* Member-part of specification */
-    char           *nameBuf;       /* temporary place for node name */
     char           saveChar;       /* Ending delimiter of member-name */
     Boolean        subLibName;     /* TRUE if libName should have/had
                                     * variable substitution performed on it */
@@ -393,21 +392,23 @@
            free(buf);
        } else if (Dir_HasWildcards(memName)) {
            Lst   members = Lst_Init();
-           size_t sz = MAXPATHLEN, nsz;
-           nameBuf = bmake_malloc(sz);
+           Buffer nameBuf;
 
+           Buf_Init(&nameBuf, 0);
            Dir_Expand(memName, dirSearchPath, members);
            while (!Lst_IsEmpty(members)) {
                char *member = Lst_DequeueS(members);
-               nsz = strlen(libName) + strlen(member) + 3;
-               if (sz > nsz)
-                   nameBuf = bmake_realloc(nameBuf, sz = nsz * 2);
 
-               snprintf(nameBuf, sz, "%s(%s)", libName, member);
+               Buf_Empty(&nameBuf);
+               Buf_AddStr(&nameBuf, libName);
+               Buf_AddStr(&nameBuf, "(");
+               Buf_AddStr(&nameBuf, member);
+               Buf_AddStr(&nameBuf, ")");
                free(member);
-               gn = Targ_FindNode(nameBuf, TARG_CREATE);
+
+               gn = Targ_FindNode(Buf_GetAll(&nameBuf, NULL), TARG_CREATE);
                if (gn == NULL) {
-                   free(nameBuf);
+                   Buf_Destroy(&nameBuf, TRUE);
                    return FAILURE;
                } else {
                    /*
@@ -422,13 +423,18 @@
                }
            }
            Lst_Destroy(members, NULL);
-           free(nameBuf);
+           Buf_Destroy(&nameBuf, TRUE);
        } else {
-           size_t      sz = strlen(libName) + strlen(memName) + 3;
-           nameBuf = bmake_malloc(sz);
-           snprintf(nameBuf, sz, "%s(%s)", libName, memName);
-           gn = Targ_FindNode(nameBuf, TARG_CREATE);
-           free(nameBuf);
+           Buffer nameBuf;
+
+           Buf_Init(&nameBuf, 0);
+           Buf_AddStr(&nameBuf, libName);
+           Buf_AddStr(&nameBuf, "(");
+           Buf_AddStr(&nameBuf, memName);
+           Buf_AddStr(&nameBuf, ")");
+
+           gn = Targ_FindNode(Buf_GetAll(&nameBuf, NULL), TARG_CREATE);
+           Buf_Destroy(&nameBuf, TRUE);
            if (gn == NULL) {
                return FAILURE;
            } else {



Home | Main Index | Thread Index | Old Index