diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2016-10-26 11:36:54 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2016-10-26 11:44:43 +0300 |
commit | 6487e873f3169c2bffbd52808b6c749e6c104ff5 (patch) | |
tree | 796694f1daf1a3b531a2bd7fe1589b66fd17d59f /lib/headerfmt.c | |
parent | b2649aa16e3f97986b566de0255e314308f94139 (diff) | |
download | rpm-6487e873f3169c2bffbd52808b6c749e6c104ff5.tar.gz |
Make variable error messages available to all of headerfmt.c
In some cases static error messages are not enough, such as RhBug:855305
which commit 73674678b2b96a36ec542ec3bc23b227c6129b5f already fixed.
Make this a common facility by wrapping the error handling inside
a new hsaError() function which takes variable arguments and all.
Watch out for % in messages, they need to be escaped now.
No functional or error message changes intended here though.
Diffstat (limited to 'lib/headerfmt.c')
-rw-r--r-- | lib/headerfmt.c | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/lib/headerfmt.c b/lib/headerfmt.c index 43311cd96..951dbc851 100644 --- a/lib/headerfmt.c +++ b/lib/headerfmt.c @@ -4,6 +4,7 @@ #include "system.h" +#include <stdarg.h> #include <rpm/header.h> #include <rpm/rpmtag.h> #include <rpm/rpmstring.h> @@ -217,6 +218,25 @@ static char * hsaReserve(headerSprintfArgs hsa, size_t need) return hsa->val + hsa->vallen; } +RPM_GNUC_PRINTF(2, 3) +static void hsaError(headerSprintfArgs hsa, const char *fmt, ...) +{ + /* Use thread local static buffer as headerFormat() errmsg arg is const */ + static __thread char errbuf[BUFSIZ]; + + if (fmt == NULL) { + hsa->errmsg = NULL; + } else { + va_list ap; + + va_start(ap, fmt); + vsnprintf(errbuf, sizeof(errbuf), fmt, ap); + va_end(ap); + + hsa->errmsg = errbuf; + } +} + /** * Search tags for a name. * @param hsa headerSprintf args @@ -333,13 +353,13 @@ static int parseFormat(headerSprintfArgs hsa, char * str, chptr = start; while (*chptr && *chptr != '{' && *chptr != '%') { if (!risdigit(*chptr) && *chptr != '-') { - hsa->errmsg = _("invalid field width"); + hsaError(hsa, _("invalid field width")); goto errxit; } chptr++; } if (!*chptr || *chptr == '%') { - hsa->errmsg = _("missing { after %"); + hsaError(hsa, _("missing { after %%")); goto errxit; } @@ -361,7 +381,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str, dst = next = start; while (*next && *next != '}') next++; if (!*next) { - hsa->errmsg = _("missing } after %{"); + hsaError(hsa, _("missing } after %%{")); goto errxit; } *next++ = '\0'; @@ -372,7 +392,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str, if (*chptr != '\0') { *chptr++ = '\0'; if (!*chptr) { - hsa->errmsg = _("empty tag format"); + hsaError(hsa, _("empty tag format")); goto errxit; } token->u.tag.type = chptr; @@ -383,17 +403,14 @@ static int parseFormat(headerSprintfArgs hsa, char * str, } if (!*start) { - hsa->errmsg = _("empty tag name"); + hsaError(hsa, _("empty tag name")); goto errxit; } token->type = PTOK_TAG; if (findTag(hsa, token, start)) { - /* Use static buffer as hsa->errmsg is const char * */ - static __thread char errmsg[1024]; - snprintf(errmsg, 1024, _("unknown tag: \"%s\""), start); - hsa->errmsg = (const char *)(&errmsg); + hsaError(hsa, _("unknown tag: \"%s\""), start); goto errxit; } @@ -413,7 +430,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str, } if (!start) { - hsa->errmsg = _("] expected at end of array"); + hsaError(hsa, _("] expected at end of array")); goto errxit; } @@ -425,7 +442,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str, case ']': if (state != PARSER_IN_ARRAY) { - hsa->errmsg = _("unexpected ]"); + hsaError(hsa, _("unexpected ]")); goto errxit; } *start++ = '\0'; @@ -435,7 +452,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str, case '}': if (state != PARSER_IN_EXPR) { - hsa->errmsg = _("unexpected }"); + hsaError(hsa, _("unexpected }")); goto errxit; } *start++ = '\0'; @@ -486,19 +503,19 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, char * chptr; char * end; - hsa->errmsg = NULL; + hsaError(hsa, NULL); chptr = str; while (*chptr && *chptr != '?') chptr++; if (*chptr != '?') { - hsa->errmsg = _("? expected in expression"); + hsaError(hsa, _("? expected in expression")); return 1; } *chptr++ = '\0';; if (*chptr != '{') { - hsa->errmsg = _("{ expected after ? in expression"); + hsaError(hsa, _("{ expected after ? in expression")); return 1; } @@ -510,7 +527,7 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, /* XXX fix segfault on "rpm -q rpm --qf='%|NAME?{%}:{NAME}|\n'"*/ if (!(end && *end)) { - hsa->errmsg = _("} expected in expression"); + hsaError(hsa, _("} expected in expression")); token->u.cond.ifFormat = freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens); return 1; @@ -518,7 +535,7 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, chptr = end; if (*chptr != ':' && *chptr != '|') { - hsa->errmsg = _(": expected following ? subexpression"); + hsaError(hsa, _(": expected following ? subexpression")); token->u.cond.ifFormat = freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens); return 1; @@ -536,7 +553,7 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, chptr++; if (*chptr != '{') { - hsa->errmsg = _("{ expected after : in expression"); + hsaError(hsa, _("{ expected after : in expression")); token->u.cond.ifFormat = freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens); return 1; @@ -550,7 +567,7 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, /* XXX fix segfault on "rpm -q rpm --qf='%|NAME?{a}:{%}|{NAME}\n'" */ if (!(end && *end)) { - hsa->errmsg = _("} expected in expression"); + hsaError(hsa, _("} expected in expression")); token->u.cond.ifFormat = freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens); return 1; @@ -558,7 +575,7 @@ static int parseExpression(headerSprintfArgs hsa, sprintfToken token, chptr = end; if (*chptr != '|') { - hsa->errmsg = _("| expected at end of expression"); + hsaError(hsa, _("| expected at end of expression")); token->u.cond.ifFormat = freeFormat(token->u.cond.ifFormat, token->u.cond.numIfTokens); token->u.cond.elseFormat = @@ -734,8 +751,8 @@ static char * singleSprintf(headerSprintfArgs hsa, sprintfToken token, if (numElements > 1 && count != numElements) switch (td->type) { default: - hsa->errmsg = - _("array iterator used with different sized arrays"); + hsaError(hsa, + _("array iterator used with different sized arrays")); return NULL; break; case RPM_BIN_TYPE: |