Do not always strdup the argument in doFoo

Only a small subset of the doFoo macros needs to modify the
argument, so only do the strdup call in them.
This commit is contained in:
Michael Schroeder 2021-11-05 13:53:34 +01:00 committed by Panu Matilainen
parent 162b2d3a33
commit efc3f10754
1 changed files with 13 additions and 21 deletions

View File

@ -1168,14 +1168,14 @@ static void doFoo(MacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
char *buf = NULL; char *buf = NULL;
char *b = NULL; char *b = NULL;
if (argv && argv[1])
buf = xstrdup(argv[1]);
if (rstreq("basename", me->name)) { if (rstreq("basename", me->name)) {
buf = xstrdup(argv[1]);
if ((b = strrchr(buf, '/')) == NULL) if ((b = strrchr(buf, '/')) == NULL)
b = buf; b = buf;
else else
b++; b++;
} else if (rstreq("dirname", me->name)) { } else if (rstreq("dirname", me->name)) {
buf = xstrdup(argv[1]);
if ((b = strrchr(buf, '/')) != NULL) if ((b = strrchr(buf, '/')) != NULL)
*b = '\0'; *b = '\0';
b = buf; b = buf;
@ -1184,6 +1184,7 @@ static void doFoo(MacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
* shrink body by removing all leading and trailing whitespaces and * shrink body by removing all leading and trailing whitespaces and
* reducing intermediate whitespaces to a single space character. * reducing intermediate whitespaces to a single space character.
*/ */
buf = xstrdup(argv[1]);
size_t i = 0, j = 0; size_t i = 0, j = 0;
size_t buflen = strlen(buf); size_t buflen = strlen(buf);
int was_space = 0; int was_space = 0;
@ -1202,38 +1203,29 @@ static void doFoo(MacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
buf[j] = '\0'; buf[j] = '\0';
b = buf; b = buf;
} else if (rstreq("quote", me->name)) { } else if (rstreq("quote", me->name)) {
char *quoted = NULL; rasprintf(&buf, "%c%s%c", 0x1f, argv[1], 0x1f);
rasprintf(&quoted, "%c%s%c", 0x1f, buf, 0x1f); b = buf;
free(buf);
b = buf = quoted;
} else if (rstreq("suffix", me->name)) { } else if (rstreq("suffix", me->name)) {
buf = xstrdup(argv[1]);
if ((b = strrchr(buf, '.')) != NULL) if ((b = strrchr(buf, '.')) != NULL)
b++; b++;
} else if (rstreq("expr", me->name)) { } else if (rstreq("expr", me->name)) {
char *expr = rpmExprStrFlags(buf, 0); b = buf = rpmExprStrFlags(argv[1], 0);
if (expr) { if (!b)
free(buf);
b = buf = expr;
} else {
mb->error = 1; mb->error = 1;
}
} else if (rstreq("url2path", me->name) || rstreq("u2p", me->name)) { } else if (rstreq("url2path", me->name) || rstreq("u2p", me->name)) {
buf = xstrdup(argv[1]);
(void)urlPath(buf, (const char **)&b); (void)urlPath(buf, (const char **)&b);
if (*b == '\0') b = "/"; if (*b == '\0') b = "/";
} else if (rstreq("getenv", me->name)) { } else if (rstreq("getenv", me->name)) {
b = getenv(buf); b = getenv(argv[1]);
} else if (rstreq("getconfdir", me->name)) { } else if (rstreq("getconfdir", me->name)) {
free(buf); b = (char *)rpmConfigDir();
buf = xmalloc(MACROBUFSIZ);
sprintf(buf, "%s", rpmConfigDir());
b = buf;
} else if (rstreq("getncpus", me->name)) { } else if (rstreq("getncpus", me->name)) {
free(buf); b = buf = xmalloc(MACROBUFSIZ);
buf = xmalloc(MACROBUFSIZ);
sprintf(buf, "%u", getncpus()); sprintf(buf, "%u", getncpus());
b = buf;
} else if (rstreq("exists", me->name)) { } else if (rstreq("exists", me->name)) {
b = (access(buf, F_OK) == 0) ? "1" : "0"; b = (access(argv[1], F_OK) == 0) ? "1" : "0";
} }
if (b) { if (b) {