From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Will Deacon <will@kernel.org>
Cc: julien.thierry.kdev@gmail.com, maz@kernel.org, oupton@kernel.org,
jean-philippe@linaro.org, andre.przywara@arm.com,
suzuki.poulose@arm.com, kvm@vger.kernel.org
Subject: Re: [PATCH kvmtool 5/6] util: Allow die_perror() to take a variable list of argument
Date: Mon, 18 May 2026 16:22:15 +0100 [thread overview]
Message-ID: <agsup8eUjERmsccV@raptor> (raw)
In-Reply-To: <agsq9UR3n9YTcA_Y@willie-the-truck>
Hi Will,
On Mon, May 18, 2026 at 04:06:29PM +0100, Will Deacon wrote:
> On Mon, May 18, 2026 at 11:12:56AM +0100, Alexandru Elisei wrote:
> > On Sun, May 17, 2026 at 09:44:30AM +0100, Will Deacon wrote:
> > > On Mon, Mar 23, 2026 at 03:02:20PM +0000, Alexandru Elisei wrote:
> > > > @@ -98,11 +99,23 @@ void __pr_debug(const char *debug, ...)
> > > > va_end(params);
> > > > }
> > > >
> > > > -void die_perror(const char *s)
> > > > +void die_perror(const char *fmt, ...)
> > > > {
> > > > + char buf[1024];
> > > > + va_list params;
> > > > int e = errno;
> > > > + int ret;
> > > > +
> > > > + va_start(params, fmt);
> > > > + ret = vsnprintf(buf, sizeof(buf), fmt, params);
> > > > + if (ret < 0) {
> > > > + e = errno;
> > > > + strncpy(buf, "vsnprintf", ARRAY_SIZE(buf) - 1);
> > >
> > > That looks really confusing to me as we're effectively hiding the real
> > > error that caused us to get here just because we couldn't pretty-print
> > > the error string! In the unlikely case that vsnprintf() fails, I think
> > > we should preserve the original errno and either print a string to say
> > > that the formatting failed _or_ pretty-print the original error with
> > > strerror().
> >
> > You have a good point.
> >
> > I'm not exactly sure what you mean by pretty-printing the original error.
> > Do you mean that we should print the message passed to die_perror()? I'm
> > don't know how I can do that reliably. I could use vprintf(), but if
> > vsnprintf() failed, wouldn't that make it likely that vprintf() will also
> > fail?
> >
> > How about this:
> >
> > diff --git a/util/util.c b/util/util.c
> > index 0f116e0ac459..4e3704f781e8 100644
> > --- a/util/util.c
> > +++ b/util/util.c
> > @@ -109,8 +109,9 @@ void die_perror(const char *fmt, ...)
> > va_start(params, fmt);
> > ret = vsnprintf(buf, sizeof(buf), fmt, params);
> > if (ret < 0) {
> > - e = errno;
> > - strncpy(buf, "vsnprintf", ARRAY_SIZE(buf) - 1);
> > + fprintf(stderr, "vsnprintf failed with error %d (%s)\n",
> > + errno, strerror(errno));
> > + strncpy(buf, "Error", ARRAY_SIZE(buf) - 1);
> > }
> > va_end(params);
> >
> > but we're still losing the original message.
>
> Sorry, I think my suggestion to use strerror() was unhelpful. Maybe just
> do something like:
>
> ret = vsnprintf(buf, sizeof(buf), fmt, params);
> if (ret < 0) {
> perror("vsnprintf");
> buf[0] = '\0';
> }
>
> ...
>
> errno = e;
> perror(buf);
>
Gotcha.
Thanks,
Alex
next prev parent reply other threads:[~2026-05-18 15:22 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-23 15:02 [PATCH kvmtool 0/6] x86 compilation fixes and arm64 PMU improvements Alexandru Elisei
2026-03-23 15:02 ` [PATCH kvmtool 1/6] virtio: Do not modify const strings in virtio_9p_rootdir_parser() Alexandru Elisei
2026-03-23 15:02 ` [PATCH kvmtool 2/6] disk/core: Do not modify const strings in disk_img_name_parser() Alexandru Elisei
2026-05-17 8:29 ` Will Deacon
2026-05-18 9:51 ` Alexandru Elisei
2026-03-23 15:02 ` [PATCH kvmtool 3/6] arm64: Initialise the PMU last Alexandru Elisei
2026-05-17 8:33 ` Will Deacon
2026-05-18 9:53 ` Alexandru Elisei
2026-03-23 15:02 ` [PATCH kvmtool 4/6] util: Set exit status to errno in die_perror() Alexandru Elisei
2026-03-23 15:02 ` [PATCH kvmtool 5/6] util: Allow die_perror() to take a variable list of argument Alexandru Elisei
2026-05-17 8:44 ` Will Deacon
2026-05-18 10:12 ` Alexandru Elisei
2026-05-18 15:06 ` Will Deacon
2026-05-18 15:22 ` Alexandru Elisei [this message]
2026-03-23 15:02 ` [PATCH kvmtool 6/6] arm64: Improve KVM_ARM_VCPU_PMU_V3_CTRL diagnostics Alexandru Elisei
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=agsup8eUjERmsccV@raptor \
--to=alexandru.elisei@arm.com \
--cc=andre.przywara@arm.com \
--cc=jean-philippe@linaro.org \
--cc=julien.thierry.kdev@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox