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.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization:
qemu.org |
libvirt.org