Skip to content

Commit c63adab

Browse files
rscharfegitster
authored andcommitted
usage: report vsnprintf(3) failure
vreportf(), which is used e.g. by die() and warning() by default, calls vsnprintf(3) to format the message to report. If that call fails, it only prints the prefix, e.g. "fatal: " or "warning: ". This at least informs users that they were supposed to get a message and reveals its severity, but leaves them wondering what it may have been about. Here's an example where vreportf() tries to print a message with a 2GB string, which is too much for vsnprintf(3): $ perl -le 'print "create refs/heads/", "a"x2**31' | git update-ref --stdin fatal: At least report the formatting error along with the offending message (unformatted) to indicate why that message is empty. Use fprintf(3) instead of error() to get the message out directly and avoid recursing back into vreportf(). With this patch we get: $ perl -le 'print "create refs/heads/", "a"x2**31' | git update-ref --stdin error: unable to format message: invalid ref format: %s fatal: ... which allows users to at least get an idea of what went wrong. Suggested-by: Jeff King <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit c63adab

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

usage.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ static void vreportf(const char *prefix, const char *err, va_list params)
1919
}
2020
memcpy(msg, prefix, prefix_len);
2121
p = msg + prefix_len;
22-
if (vsnprintf(p, pend - p, err, params) < 0)
22+
if (vsnprintf(p, pend - p, err, params) < 0) {
23+
fprintf(stderr, _("error: unable to format message: %s\n"),
24+
err);
2325
*p = '\0'; /* vsnprintf() failed, clip at prefix */
26+
}
2427

2528
for (; p != pend - 1 && *p; p++) {
2629
if (iscntrl(*p) && *p != '\t' && *p != '\n')

0 commit comments

Comments
 (0)