On Tue, May 09, 2023 at 01:25:18PM -0500, Eric Blake wrote:
On Tue, May 09, 2023 at 03:51:20PM +0100, Richard W.M. Jones wrote:
> Preserve errno even if the function fails. We need to set
> errno = err again on every exit path out of the function.
>
> Check if close_memstream failed and go to the error path if so. This
> is most important on Windows where close_memstream is what actually
> allocates the memory. But even on Unix, close_memstream (which is an
> alias for fclose) can return an error.
>
> Move the error-handling code to the end of the function.
>
> I added a comment above the 'fputs' explaining why exactly we're using
> memstream in the first place -- because it is more atomic than using
> multiple fprintf.
>
> See:
https://listman.redhat.com/archives/libguestfs/2023-May/031456.html
> Thanks: Laszlo Ersek
> ---
> server/debug.c | 28 ++++++++++++++++------------
> 1 file changed, 16 insertions(+), 12 deletions(-)
Reviewed-by: Eric Blake <eblake(a)redhat.com>
> + fail:
> + /* Try to emit what we can. */
> + errno = err;
> + vfprintf (stderr, fs, args);
> + fprintf (stderr, "\n");
> errno = err;
It might make more sense if the first line were written:
errno = err; /* In case fs uses %m */
so that no one tries to "optimize" out what looks like a redundant
assignment when both are really required.
I actually did that already in my local version!
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW