From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43004) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWmPF-0008E4-BO for qemu-devel@nongnu.org; Tue, 11 Dec 2018 13:05:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWmPA-0007W3-Qc for qemu-devel@nongnu.org; Tue, 11 Dec 2018 13:05:17 -0500 From: Laurent Vivier Date: Tue, 11 Dec 2018 19:03:49 +0100 Message-Id: <20181211180352.19644-28-laurent@vivier.eu> In-Reply-To: <20181211180352.19644-1-laurent@vivier.eu> References: <20181211180352.19644-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PULL 27/30] cutils: Assert in-range base for string-to-integer conversions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , qemu-trivial@nongnu.org, Laurent Vivier , Richard Henderson , Eduardo Habkost , Michael Tokarev , Paolo Bonzini , Eric Blake From: Eric Blake POSIX states that the value of endptr is unspecified if strtol() fails with EINVAL due to an invalid base argument. Since none of the callers to check_strtox_error() initialized endptr, we could end up propagating uninitialized data back to a caller on error. However, passing an out-of-range base is already a sign of poor programming, so let's just assert that base is in range, at which point check_strtox_error() can be tightened to assert that it is receiving an initialized ep that points somewhere within the caller's original string, regardless of whether strto*() succeeded or failed with ERANGE. Reported-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Eric Blake Reviewed-by: Markus Armbruster Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20181206151856.77503-1-eblake@redhat.com> Signed-off-by: Laurent Vivier --- util/cutils.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/util/cutils.c b/util/cutils.c index 698bd315bd..0621565930 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -280,6 +280,7 @@ int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result) static int check_strtox_error(const char *nptr, char *ep, const char **endptr, int libc_errno) { + assert(ep >= nptr); if (endptr) { *endptr = ep; } @@ -327,6 +328,7 @@ int qemu_strtoi(const char *nptr, const char **endptr, int base, char *ep; long long lresult; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -379,6 +381,7 @@ int qemu_strtoui(const char *nptr, const char **endptr, int base, char *ep; long long lresult; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -435,6 +438,7 @@ int qemu_strtol(const char *nptr, const char **endptr, int base, { char *ep; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -477,6 +481,7 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base, { char *ep; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -504,6 +509,7 @@ int qemu_strtoi64(const char *nptr, const char **endptr, int base, { char *ep; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -527,6 +533,7 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, { char *ep; + assert((unsigned) base <= 36 && base != 1); if (!nptr) { if (endptr) { *endptr = nptr; @@ -594,6 +601,7 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr, char *endp = (char *)s; unsigned long long val = 0; + assert((unsigned) base <= 36 && base != 1); if (!s) { r = -EINVAL; goto out; -- 2.19.2