From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:58922) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDd9R-0005Ad-1N for qemu-devel@nongnu.org; Mon, 17 Sep 2012 11:22:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TDd9J-0001No-Fj for qemu-devel@nongnu.org; Mon, 17 Sep 2012 11:22:20 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:52558) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDd9I-0001Ml-SA for qemu-devel@nongnu.org; Mon, 17 Sep 2012 11:22:13 -0400 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 17 Sep 2012 20:52:10 +0530 Received: from d28av03.in.ibm.com (d28av03.in.ibm.com [9.184.220.65]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q8HFLo6Z61341778 for ; Mon, 17 Sep 2012 20:51:50 +0530 Received: from d28av03.in.ibm.com (loopback [127.0.0.1]) by d28av03.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q8HFLn9g003610 for ; Tue, 18 Sep 2012 01:21:49 +1000 Date: Mon, 17 Sep 2012 20:53:43 +0530 From: Bharata B Rao Message-ID: <20120917152343.GD6879@in.ibm.com> References: <20120917152149.GB6879@in.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120917152149.GB6879@in.ibm.com> Subject: [Qemu-devel] [PATCH v7 2/5] sockets: Change inet_parse() to accept address specification without port Reply-To: bharata@linux.vnet.ibm.com List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Anthony Liguori , Anand Avati , Vijay Bellur , Stefan Hajnoczi , Amar Tumballi , Markus Armbruster , Blue Swirl , Avi Kivity , Paolo Bonzini sockets: Change inet_parse() to accept address specification without port From: Bharata B Rao inet_parse() expects address:port. Change it to work without explicit port specification. In addition, don't depend solely on the return value of sscanf but also consider the value obtained for %n directive used in sscanf. This ensures that the scanning of malformed inet address isn't flagged as success. Signed-off-by: Bharata B Rao --- qemu-sockets.c | 32 +++++++++++++++++++++++++------- 1 files changed, 25 insertions(+), 7 deletions(-) diff --git a/qemu-sockets.c b/qemu-sockets.c index c330fc5..627269f 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -376,26 +376,35 @@ err: return -1; } -/* compatibility wrapper */ +/* + * sscanf could return positive return value indicating successful + * match and assignment even when it doesn't parse the input as per + * the format specified. Hence we are depending on %n to really determine if + * inputs were successfully scanned. Checking retval for EOF isn't strictly + * necessary, but just being extra careful. + */ int inet_parse(QemuOpts *opts, const char *str) { const char *optstr, *h; char addr[64]; - char port[33]; - int pos; + char port[33] = {'\0'}; /* port is optional */ + int ret, pos, addr_pos = -1, port_pos = -1; /* parse address */ if (str[0] == ':') { /* no host given */ addr[0] = '\0'; - if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { + ret = sscanf(str, ":%32[^,]%n", port, &port_pos); + if (port_pos == -1 || ret == EOF) { fprintf(stderr, "%s: portonly parse error (%s)\n", __FUNCTION__, str); return -1; } } else if (str[0] == '[') { /* IPv6 addr */ - if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { + ret = sscanf(str, "[%64[^]]]%n:%32[^,]%n", addr, &addr_pos, + port, &port_pos); + if (addr_pos == -1 || ret == EOF) { fprintf(stderr, "%s: ipv6 parse error (%s)\n", __FUNCTION__, str); return -1; @@ -403,7 +412,9 @@ int inet_parse(QemuOpts *opts, const char *str) qemu_opt_set(opts, "ipv6", "on"); } else if (qemu_isdigit(str[0])) { /* IPv4 addr */ - if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { + ret = sscanf(str, "%64[0-9.]%n:%32[^,]%n", addr, &addr_pos, + port, &port_pos); + if (addr_pos == -1 || ret == EOF) { fprintf(stderr, "%s: ipv4 parse error (%s)\n", __FUNCTION__, str); return -1; @@ -411,7 +422,9 @@ int inet_parse(QemuOpts *opts, const char *str) qemu_opt_set(opts, "ipv4", "on"); } else { /* hostname */ - if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { + ret = sscanf(str, "%64[^:]%n:%32[^,]%n", addr, &addr_pos, + port, &port_pos); + if (addr_pos == -1 || ret == EOF) { fprintf(stderr, "%s: hostname parse error (%s)\n", __FUNCTION__, str); return -1; @@ -421,6 +434,11 @@ int inet_parse(QemuOpts *opts, const char *str) qemu_opt_set(opts, "port", port); /* parse options */ + if (port_pos != -1) { + pos = port_pos; + } else { + pos = addr_pos; + } optstr = str + pos; h = strstr(optstr, ",to="); if (h)