Push the macro buffer size limit down by one level

- Turn expandMacros() into a wrapper around internal doExpandMacros()
  which returns the expanded string in a newly allocated buffer, use
  the internal version for rpmExpand() too.
This commit is contained in:
Panu Matilainen 2010-09-24 12:48:15 +03:00
parent a931202422
commit 568ba0d242
1 changed files with 24 additions and 24 deletions

View File

@ -1349,38 +1349,41 @@ expandMacro(MacroBuf mb, const char *src)
/* =============================================================== */ /* =============================================================== */
int static int doExpandMacros(rpmMacroContext mc, const char *src, char **target)
expandMacros(void * spec, rpmMacroContext mc, char * sbuf, size_t slen)
{ {
MacroBuf mb = xcalloc(1, sizeof(*mb)); MacroBuf mb = xcalloc(1, sizeof(*mb));
char *tbuf = NULL; size_t tlen = MACROBUFSIZ + strlen(src);
char *tbuf = xcalloc(tlen + 1, sizeof(*tbuf));
int rc = 0; int rc = 0;
if (sbuf == NULL || slen == 0)
goto exit;
if (mc == NULL) mc = rpmGlobalMacroContext; if (mc == NULL) mc = rpmGlobalMacroContext;
tbuf = xcalloc(slen + 1, sizeof(*tbuf));
mb->t = tbuf; mb->t = tbuf;
mb->nb = slen; mb->nb = tlen;
mb->depth = 0; mb->depth = 0;
mb->macro_trace = print_macro_trace; mb->macro_trace = print_macro_trace;
mb->expand_trace = print_expand_trace; mb->expand_trace = print_expand_trace;
mb->mc = mc; mb->mc = mc;
rc = expandMacro(mb, sbuf); rc = expandMacro(mb, src);
if (mb->nb == 0) if (mb->nb == 0)
rpmlog(RPMLOG_ERR, _("Target buffer overflow\n")); rpmlog(RPMLOG_ERR, _("Target buffer overflow\n"));
tbuf[slen] = '\0'; /* XXX just in case */ tbuf[tlen] = '\0'; /* XXX just in case */
strncpy(sbuf, tbuf, (slen - mb->nb + 1)); /* expanded output is usually much less than alloced buffer, downsize */
*target = xrealloc(tbuf, strlen(tbuf) + 1);
exit:
_free(mb); _free(mb);
_free(tbuf); return rc;
}
int expandMacros(void * spec, rpmMacroContext mc, char * sbuf, size_t slen)
{
char *target = NULL;
int rc = doExpandMacros(mc, sbuf, &target);
rstrlcpy(sbuf, target, slen);
free(target);
return rc; return rc;
} }
@ -1554,18 +1557,18 @@ rpmFreeMacros(rpmMacroContext mc)
char * char *
rpmExpand(const char *arg, ...) rpmExpand(const char *arg, ...)
{ {
size_t blen = MACROBUFSIZ; size_t blen = 0;
char *buf = NULL; char *buf = NULL, *ret = NULL;
char *pe; char *pe;
const char *s; const char *s;
va_list ap; va_list ap;
if (arg == NULL) { if (arg == NULL) {
buf = xstrdup(""); ret = xstrdup("");
goto exit; goto exit;
} }
/* precalculate unexpanded size on top of MACROBUFSIZ */ /* precalculate unexpanded size */
va_start(ap, arg); va_start(ap, arg);
for (s = arg; s != NULL; s = va_arg(ap, const char *)) for (s = arg; s != NULL; s = va_arg(ap, const char *))
blen += strlen(s); blen += strlen(s);
@ -1579,14 +1582,11 @@ rpmExpand(const char *arg, ...)
pe = stpcpy(pe, s); pe = stpcpy(pe, s);
va_end(ap); va_end(ap);
(void) expandMacros(NULL, NULL, buf, blen); (void) doExpandMacros(NULL, buf, &ret);
/* expanded output is usually much less than alloced buffer, downsize */
blen = strlen(buf);
buf = xrealloc(buf, blen + 1);
free(buf);
exit: exit:
return buf; return ret;
} }
int int