All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bharata B Rao <bharata@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Anand Avati <aavati@redhat.com>,
	Vijay Bellur <vbellur@redhat.com>,
	Stefan Hajnoczi <stefanha@gmail.com>,
	Amar Tumballi <amarts@redhat.com>,
	Markus Armbruster <armbru@redhat.com>,
	Blue Swirl <blauwirbel@gmail.com>, Avi Kivity <avi@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH v7 2/5] sockets: Change inet_parse() to accept address specification without port
Date: Mon, 17 Sep 2012 20:53:43 +0530	[thread overview]
Message-ID: <20120917152343.GD6879@in.ibm.com> (raw)
In-Reply-To: <20120917152149.GB6879@in.ibm.com>

sockets: Change inet_parse() to accept address specification without port

From: Bharata B Rao <bharata@linux.vnet.ibm.com>

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 <bharata@linux.vnet.ibm.com>
---

 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)

  parent reply	other threads:[~2012-09-17 15:22 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-17 15:21 [Qemu-devel] [RFC v7 0/5] GlusterFS support in QEMU - v7 Bharata B Rao
2012-09-17 15:22 ` [Qemu-devel] [PATCH v7 1/5] sockets: Make inet_parse() non static Bharata B Rao
2012-09-18 12:47   ` Kevin Wolf
2012-09-18 12:57     ` Paolo Bonzini
2012-09-20  5:57       ` Bharata B Rao
2012-09-17 15:23 ` Bharata B Rao [this message]
2012-09-18 13:22   ` [Qemu-devel] [PATCH v7 2/5] sockets: Change inet_parse() to accept address specification without port Kevin Wolf
2012-09-18 13:31     ` Paolo Bonzini
2012-09-18 14:08       ` Kevin Wolf
2012-09-20  6:30         ` Bharata B Rao
2012-09-20  6:51           ` Kevin Wolf
2012-09-17 15:24 ` [Qemu-devel] [PATCH v7 3/5] aio: Fix qemu_aio_wait() to maintain correct walking_handlers count Bharata B Rao
2012-09-17 15:25 ` [Qemu-devel] [PATCH v7 4/5] configure: Add a config option for GlusterFS as block backend Bharata B Rao
2012-09-17 15:26 ` [Qemu-devel] [PATCH v7 5/5] block: Support GlusterFS as a QEMU " Bharata B Rao
2012-09-18 14:01   ` Kevin Wolf
2012-09-20  6:41     ` Bharata B Rao
2012-09-20  7:53       ` Paolo Bonzini
2012-09-20  8:20         ` Paolo Bonzini
2012-09-20  9:12           ` Bharata B Rao
2012-09-20  9:14             ` Paolo Bonzini
2012-09-20  9:34             ` Kevin Wolf
2012-09-20 15:08               ` Paolo Bonzini
2012-09-21  3:50                 ` Bharata B Rao
2012-09-21  8:23                   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120917152343.GD6879@in.ibm.com \
    --to=bharata@linux.vnet.ibm.com \
    --cc=aavati@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=amarts@redhat.com \
    --cc=armbru@redhat.com \
    --cc=avi@redhat.com \
    --cc=blauwirbel@gmail.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=vbellur@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.