From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33141) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VrPfw-0005kV-Dm for qemu-devel@nongnu.org; Fri, 13 Dec 2013 05:08:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VrPfq-0000y2-Eb for qemu-devel@nongnu.org; Fri, 13 Dec 2013 05:08:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:30766) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VrPfq-0000xv-6n for qemu-devel@nongnu.org; Fri, 13 Dec 2013 05:08:46 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rBDA8ipW013623 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 13 Dec 2013 05:08:44 -0500 From: Gerd Hoffmann Date: Fri, 13 Dec 2013 11:08:31 +0100 Message-Id: <1386929311-520-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PATCH v3] inet_listen_opts: add error checking List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann Don't use atoi() function which doesn't detect errors, switch to strtol and error out on failures. Also add a range check while being at it. [ v3: oops, v2 didn't build ] [ v2: use parse_uint_full instead of strtol ] Signed-off-by: Gerd Hoffmann --- util/qemu-sockets.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 6b97dc1..c3560c1 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -133,8 +133,18 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) ai.ai_family = PF_INET6; /* lookup */ - if (port_offset) - snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); + if (port_offset) { + int baseport; + if (parse_uint_full(port, &baseport, 10) < 0) { + error_setg(errp, "can't convert to a number: %s", port); + return -1; + } + if (baseport < 0 || baseport + port_offset > 65535) { + error_setg(errp, "port %s out of range", port); + return -1; + } + snprintf(port, sizeof(port), "%d", baseport + port_offset); + } rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); if (rc != 0) { error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, -- 1.8.3.1