1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
3 Date: Mon, 27 Jan 2020 10:24:09 +0100
4 Subject: [PATCH 1/2] util: add slirp_fmt() helpers
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Various calls to snprintf() in libslirp assume that snprintf() returns
10 "only" the number of bytes written (excluding terminating NUL).
12 https://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html#tag_16_159_04
14 "Upon successful completion, the snprintf() function shall return the
15 number of bytes that would be written to s had n been sufficiently
16 large excluding the terminating null byte."
18 Introduce slirp_fmt() that handles several pathological cases the
19 way libslirp usually expect:
21 - treat error as fatal (instead of silently returning -1)
23 - fmt0() will always \0 end
25 - return the number of bytes actually written (instead of what would
26 have been written, which would usually result in OOB later), including
27 the ending \0 for fmt0()
29 - warn if truncation happened (instead of ignoring)
31 Other less common cases can still be handled with strcpy/snprintf() etc.
33 Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
34 Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
35 Message-Id: <20200127092414.169796-2-marcandre.lureau@redhat.com>
36 Signed-off-by: Oguz Bektas <o.bektas@proxmox.com>
38 slirp/src/util.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
39 slirp/src/util.h | 3 +++
40 2 files changed, 65 insertions(+)
42 diff --git a/slirp/src/util.c b/slirp/src/util.c
43 index e596087..e3b6257 100644
44 --- a/slirp/src/util.c
45 +++ b/slirp/src/util.c
46 @@ -364,3 +364,65 @@ void slirp_pstrcpy(char *buf, int buf_size, const char *str)
51 +static int slirp_vsnprintf(char *str, size_t size,
52 + const char *format, va_list args)
54 + int rv = vsnprintf(str, size, format, args);
57 + g_error("vsnprintf() failed: %s", g_strerror(errno));
64 + * A snprintf()-like function that:
65 + * - returns the number of bytes written (excluding optional \0-ending)
67 + * - warn on truncation
69 +int slirp_fmt(char *str, size_t size, const char *format, ...)
74 + va_start(args, format);
75 + rv = slirp_vsnprintf(str, size, format, args);
79 + g_critical("vsnprintf() truncation");
82 + return MIN(rv, size);
86 + * A snprintf()-like function that:
87 + * - always \0-end (unless size == 0)
88 + * - returns the number of bytes actually written, including \0 ending
90 + * - warn on truncation
92 +int slirp_fmt0(char *str, size_t size, const char *format, ...)
97 + va_start(args, format);
98 + rv = slirp_vsnprintf(str, size, format, args);
102 + g_critical("vsnprintf() truncation");
104 + str[size - 1] = '\0';
107 + rv += 1; /* include \0 */
112 diff --git a/slirp/src/util.h b/slirp/src/util.h
113 index 3c6223c..0558dfc 100644
114 --- a/slirp/src/util.h
115 +++ b/slirp/src/util.h
116 @@ -177,4 +177,7 @@ static inline int slirp_socket_set_fast_reuse(int fd)
118 void slirp_pstrcpy(char *buf, int buf_size, const char *str);
120 +int slirp_fmt(char *str, size_t size, const char *format, ...);
121 +int slirp_fmt0(char *str, size_t size, const char *format, ...);