From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43140) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvPia-0000z8-8s for qemu-devel@nongnu.org; Fri, 13 Jun 2014 07:32:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WvPiS-0007zg-Ox for qemu-devel@nongnu.org; Fri, 13 Jun 2014 07:32:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38944) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvPiS-0007zZ-HW for qemu-devel@nongnu.org; Fri, 13 Jun 2014 07:32:16 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s5DBWF5I012439 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 13 Jun 2014 07:32:16 -0400 From: Gerd Hoffmann Date: Fri, 13 Jun 2014 13:32:12 +0200 Message-Id: <1402659132-13490-2-git-send-email-kraxel@redhat.com> In-Reply-To: <1402659132-13490-1-git-send-email-kraxel@redhat.com> References: <1402659132-13490-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PULL 1/1] 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. Signed-off-by: Gerd Hoffmann Reviewed-by: Markus Armbruster --- util/qemu-sockets.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index e3d29ee..a4a1e9d 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -131,8 +131,19 @@ 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) { + unsigned long long baseport; + if (parse_uint_full(port, &baseport, 10) < 0) { + error_setg(errp, "can't convert to a number: %s", port); + return -1; + } + if (baseport > 65535 || + baseport + port_offset > 65535) { + error_setg(errp, "port %s out of range", port); + return -1; + } + snprintf(port, sizeof(port), "%d", (int)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