diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2010-09-24 12:48:15 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2010-09-27 14:55:12 +0300 |
commit | 568ba0d2421f89f7f98531b81ba9fe783dbdc749 (patch) | |
tree | 453228a4acb9cc39f713d9fde4276fff121b3588 /rpmio/macro.c | |
parent | a931202422b38cbac52aeb507572a597ddee9106 (diff) | |
download | rpm-568ba0d2421f89f7f98531b81ba9fe783dbdc749.tar.gz rpm-568ba0d2421f89f7f98531b81ba9fe783dbdc749.tar.bz2 rpm-568ba0d2421f89f7f98531b81ba9fe783dbdc749.zip |
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.
Diffstat (limited to 'rpmio/macro.c')
-rw-r--r-- | rpmio/macro.c | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/rpmio/macro.c b/rpmio/macro.c index 4df1e87ad..292db66d6 100644 --- a/rpmio/macro.c +++ b/rpmio/macro.c @@ -1349,38 +1349,41 @@ expandMacro(MacroBuf mb, const char *src) /* =============================================================== */ -int -expandMacros(void * spec, rpmMacroContext mc, char * sbuf, size_t slen) +static int doExpandMacros(rpmMacroContext mc, const char *src, char **target) { 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; - if (sbuf == NULL || slen == 0) - goto exit; - if (mc == NULL) mc = rpmGlobalMacroContext; - tbuf = xcalloc(slen + 1, sizeof(*tbuf)); - mb->t = tbuf; - mb->nb = slen; + mb->nb = tlen; mb->depth = 0; mb->macro_trace = print_macro_trace; mb->expand_trace = print_expand_trace; mb->mc = mc; - rc = expandMacro(mb, sbuf); + rc = expandMacro(mb, src); if (mb->nb == 0) rpmlog(RPMLOG_ERR, _("Target buffer overflow\n")); - tbuf[slen] = '\0'; /* XXX just in case */ - strncpy(sbuf, tbuf, (slen - mb->nb + 1)); + tbuf[tlen] = '\0'; /* XXX just in case */ + /* expanded output is usually much less than alloced buffer, downsize */ + *target = xrealloc(tbuf, strlen(tbuf) + 1); -exit: _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; } @@ -1554,18 +1557,18 @@ rpmFreeMacros(rpmMacroContext mc) char * rpmExpand(const char *arg, ...) { - size_t blen = MACROBUFSIZ; - char *buf = NULL; + size_t blen = 0; + char *buf = NULL, *ret = NULL; char *pe; const char *s; va_list ap; if (arg == NULL) { - buf = xstrdup(""); + ret = xstrdup(""); goto exit; } - /* precalculate unexpanded size on top of MACROBUFSIZ */ + /* precalculate unexpanded size */ va_start(ap, arg); for (s = arg; s != NULL; s = va_arg(ap, const char *)) blen += strlen(s); @@ -1579,14 +1582,11 @@ rpmExpand(const char *arg, ...) pe = stpcpy(pe, s); va_end(ap); - (void) expandMacros(NULL, NULL, buf, blen); - - /* expanded output is usually much less than alloced buffer, downsize */ - blen = strlen(buf); - buf = xrealloc(buf, blen + 1); + (void) doExpandMacros(NULL, buf, &ret); + free(buf); exit: - return buf; + return ret; } int |