git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git List <git@vger.kernel.org>
Subject: Re: [PATCH] usage: report vsnprintf(3) failure
Date: Fri, 5 Apr 2024 15:20:24 -0400	[thread overview]
Message-ID: <20240405192024.GB2561807@coredump.intra.peff.net> (raw)
In-Reply-To: <3da13298-b6a6-4391-b8e8-5dae9a28b860@web.de>

On Fri, Apr 05, 2024 at 08:59:52PM +0200, René Scharfe wrote:

> 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.

Thanks, I think this is a good change and you've nicely summarized the
situation above. And the patch itself:

> diff --git a/usage.c b/usage.c
> index 09f0ed509b..7a2f7805f5 100644
> --- a/usage.c
> +++ b/usage.c
> @@ -19,8 +19,11 @@ static void vreportf(const char *prefix, const char *err, va_list params)
>  	}
>  	memcpy(msg, prefix, prefix_len);
>  	p = msg + prefix_len;
> -	if (vsnprintf(p, pend - p, err, params) < 0)
> +	if (vsnprintf(p, pend - p, err, params) < 0) {
> +		fprintf(stderr, _("error: unable to format message: %s\n"),
> +			err);
>  		*p = '\0'; /* vsnprintf() failed, clip at prefix */
> +	}

is nice and simply, and shouldn't have any unexpected side effects.

-Peff


      reply	other threads:[~2024-04-05 19:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-05 18:59 [PATCH] usage: report vsnprintf(3) failure René Scharfe
2024-04-05 19:20 ` Jeff King [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240405192024.GB2561807@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).