Module Name: src
Committed By: rillig
Date: Sun Aug 23 17:49:37 UTC 2020
Modified Files:
src/usr.bin/make: arch.c
Log Message:
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?
To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/usr.bin/make/arch.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/make/arch.c
diff -u src/usr.bin/make/arch.c:1.92 src/usr.bin/make/arch.c:1.93
--- src/usr.bin/make/arch.c:1.92 Sun Aug 23 16:58:02 2020
+++ src/usr.bin/make/arch.c Sun Aug 23 17:49:37 2020
@@ -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 @@ Arch_ParseArchive(char **linePtr, Lst no
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 @@ Arch_ParseArchive(char **linePtr, Lst no
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 @@ Arch_ParseArchive(char **linePtr, Lst no
}
}
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 {