From patchwork Wed May 21 10:53:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 351099 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 97037140082 for ; Wed, 21 May 2014 20:54:10 +1000 (EST) Received: from localhost ([::1]:58740 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn49v-0005MT-Ri for incoming@patchwork.ozlabs.org; Wed, 21 May 2014 06:54:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54716) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn49R-0004gC-UD for qemu-devel@nongnu.org; Wed, 21 May 2014 06:53:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wn49L-0005R7-QX for qemu-devel@nongnu.org; Wed, 21 May 2014 06:53:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:22778) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wn49L-0005Qx-J8 for qemu-devel@nongnu.org; Wed, 21 May 2014 06:53:31 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4LArU1L010508 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 21 May 2014 06:53:30 -0400 Received: from nilsson.home.kraxel.org (ovpn-116-26.ams2.redhat.com [10.36.116.26]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s4LArT6V004512; Wed, 21 May 2014 06:53:30 -0400 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 1C9AA803B7; Wed, 21 May 2014 12:53:29 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 21 May 2014 12:53:27 +0200 Message-Id: <1400669607-18205-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Gerd Hoffmann Subject: [Qemu-devel] [PATCH] inet_listen_opts: add error checking X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 8818d7c..22971e2 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -133,8 +133,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 (base_port > 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,