From patchwork Wed Dec 11 12:00:30 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 300022 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2ABDC2C00A4 for ; Wed, 11 Dec 2013 23:01:07 +1100 (EST) Received: from localhost ([::1]:55972 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VqiTQ-0006Pb-P5 for incoming@patchwork.ozlabs.org; Wed, 11 Dec 2013 07:01:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59696) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VqiT3-0006P0-Mv for qemu-devel@nongnu.org; Wed, 11 Dec 2013 07:00:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VqiSx-0000Ka-Oq for qemu-devel@nongnu.org; Wed, 11 Dec 2013 07:00:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36824) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VqiSx-0000KV-Gs for qemu-devel@nongnu.org; Wed, 11 Dec 2013 07:00:35 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rBBC0XKW003583 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 11 Dec 2013 07:00:34 -0500 Received: from nilsson.home.kraxel.org (vpn1-5-88.ams2.redhat.com [10.36.5.88]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id rBBC0W7a009565; Wed, 11 Dec 2013 07:00:33 -0500 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 0B95280856; Wed, 11 Dec 2013 13:00:32 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Wed, 11 Dec 2013 13:00:30 +0100 Message-Id: <1386763230-9202-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 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 --- util/qemu-sockets.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 6b97dc1..5636510 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -133,8 +133,20 @@ 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; + errno = 0; + baseport = strtol(port, NULL, 10); + if (errno != 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,