[Slirp] [PATCH 1/6] util: add slirp_fmt() helpers
Daniel P. Berrangé
berrange at redhat.com
Mon Jan 27 13:09:47 UTC 2020
On Mon, Jan 27, 2020 at 10:43:36AM +0100, Philippe Mathieu-Daudé wrote:
> On 1/27/20 10:24 AM, marcandre.lureau at redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau at redhat.com>
> >
> > Various calls to snprintf() in libslirp assume that snprintf() returns
> > "only" the number of bytes written (excluding terminating NUL).
> >
> > https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04
> >
> > "Upon successful completion, the snprintf() function shall return the
> > number of bytes that would be written to s had n been sufficiently
> > large excluding the terminating null byte."
> >
> > Introduce slirp_fmt() that handles several pathological cases the
> > way libslirp usually expect:
> >
> > - treat error as fatal (instead of silently returning -1)
> >
> > - fmt0() will always \0 end
> >
> > - return the number of bytes actually written (instead of what would
> > have been written, which would usually result in OOB later), including
> > the ending \0 for fmt0()
> > diff --git a/src/util.c b/src/util.c
> > index e596087..e3b6257 100644
> > --- a/src/util.c
> > +++ b/src/util.c
> > @@ -364,3 +364,65 @@ void slirp_pstrcpy(char *buf, int buf_size, const char *str)
> > }
> > *q = '\0';
> > }
> > +
> > +static int slirp_vsnprintf(char *str, size_t size,
> > + const char *format, va_list args)
> > +{
> > + int rv = vsnprintf(str, size, format, args);
Should use g_vsnprintf()
The GLib impl guarantees GNU compatible format strings, which fixes
the horror of Windows platform format strings.
> > diff --git a/src/util.h b/src/util.h
> > index e9c3073..5530c46 100644
> > --- a/src/util.h
> > +++ b/src/util.h
> > @@ -181,4 +181,7 @@ static inline int slirp_socket_set_fast_reuse(int fd)
> > void slirp_pstrcpy(char *buf, int buf_size, const char *str);
> > +int slirp_fmt(char *str, size_t size, const char *format, ...);
> > +int slirp_fmt0(char *str, size_t size, const char *format, ...);
Ought to annotate with the "gnu_printf" attribute so that the compilers
can check for various format string mistakes.
Regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
More information about the Slirp
mailing list