xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Marek Marczykowski <marmarek@mimuw.edu.pl>
To: xen-devel@lists.xensource.com
Cc: marmarek@mimuw.edu.pl
Subject: [PATCH 4 of 6 RESENT] xl: Use macros for param parsing in network-attach, to prevent use explicit sizeof("param")
Date: Sun, 05 Jun 2011 18:50:34 +0200	[thread overview]
Message-ID: <0863dfd23b3a10bee6d3.1307292634@devel14> (raw)
In-Reply-To: <patchbomb.1307292630@devel14>

# HG changeset patch
# User Marek Marczykowski <marmarek@mimuw.edu.pl>
# Date 1307145042 -7200
# Node ID 0863dfd23b3a10bee6d3eda30415dff0eef2bee1
# Parent  9fe949c7ab9601bb5500a53c538f7a23b61e1bcb
xl: Use macros for param parsing in network-attach, to prevent use explicit sizeof("param")

'script=' length was wrong... Replaced calls to strncmp("param", *argv,
explicit sizeof("param")) with macro and helper function to extract parameter
value. Also introduce replace_string function to simplify code.

Signed-off-by: Marek Marczykowski <marmarek@mimuw.edu.pl>

diff -r 9fe949c7ab96 -r 0863dfd23b3a tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c	Wed Jun 01 23:15:54 2011 +0200
+++ b/tools/libxl/xl_cmdimpl.c	Sat Jun 04 01:50:42 2011 +0200
@@ -1229,6 +1229,24 @@ static int handle_domain_death(libxl_ctx
     return restart;
 }
 
+/* for now used only by main_networkattach, but can be reused elsewhere */
+static int match_option_size(const char *prefix, size_t len,
+        char *arg, char **argopt)
+{
+    int rc = strncmp(prefix, arg, len);
+    if (!rc) *argopt = arg+len;
+    return !rc;
+}
+#define match_option(_prefix, _arg, _oparg) \
+    match_option_size((_prefix "="), sizeof((_prefix)) + 1, (_arg), &(_oparg))
+
+static void replace_string(char **str, const char *val)
+{
+    free(*str);
+    *str = strdup(val);
+}
+
+
 static int preserve_domain(libxl_ctx *ctx, uint32_t domid, libxl_event *event,
                            libxl_domain_config *d_config, libxl_dominfo *info)
 {
@@ -3925,7 +3943,7 @@ int main_networkattach(int argc, char **
 {
     int opt;
     libxl_device_nic nic;
-    char *endptr;
+    char *endptr, *oparg;
     const char *tok;
     int i;
     unsigned int val;
@@ -3944,17 +3962,17 @@ int main_networkattach(int argc, char **
     }
     libxl_device_nic_init(&nic, -1);
     for (argv += optind+1, argc -= optind+1; argc > 0; ++argv, --argc) {
-        if (!strncmp("type=", *argv, 5)) {
-            if (!strncmp("vif", (*argv) + 5, 4)) {
+        if (match_option("type", *argv, oparg)) {
+            if (!strcmp("vif", oparg)) {
                 nic.nictype = LIBXL_NIC_TYPE_VIF;
-            } else if (!strncmp("ioemu", (*argv) + 5, 5)) {
+            } else if (!strcmp("ioemu", oparg)) {
                 nic.nictype = LIBXL_NIC_TYPE_IOEMU;
             } else {
                 fprintf(stderr, "Invalid parameter `type'.\n");
                 return 1;
             }
-        } else if (!strncmp("mac=", *argv, 4)) {
-            tok = strtok((*argv) + 4, ":");
+        } else if (match_option("mac", *argv, oparg)) {
+            tok = strtok(oparg, ":");
             for (i = 0; tok && i < 6; tok = strtok(NULL, ":"), ++i) {
                 val = strtoul(tok, &endptr, 16);
                 if ((tok == endptr) || (val > 255)) {
@@ -3963,29 +3981,24 @@ int main_networkattach(int argc, char **
                 }
                 nic.mac[i] = val;
             }
-        } else if (!strncmp("bridge=", *argv, 7)) {
-            free(nic.bridge);
-            nic.bridge = strdup((*argv) + 7);
-        } else if (!strncmp("ip=", *argv, 3)) {
-            free(nic.ip);
-            nic.ip = strdup((*argv) + 3);
-        } else if (!strncmp("script=", *argv, 6)) {
-            free(nic.script);
-            nic.script = strdup((*argv) + 6);
-        } else if (!strncmp("backend=", *argv, 8)) {
-            if(libxl_name_to_domid(ctx, ((*argv) + 8), &val)) {
+        } else if (match_option("bridge", *argv, oparg)) {
+            replace_string(&nic.bridge, oparg);
+        } else if (match_option("ip", *argv, oparg)) {
+            replace_string(&nic.ip, oparg);
+        } else if (match_option("script", *argv, oparg)) {
+            replace_string(&nic.script, oparg);
+        } else if (match_option("backend", *argv, oparg)) {
+            if(libxl_name_to_domid(ctx, oparg, &val)) {
                 fprintf(stderr, "Specified backend domain does not exist, defaulting to Dom0\n");
                 val = 0;
             }
             nic.backend_domid = val;
-        } else if (!strncmp("vifname=", *argv, 8)) {
-            free(nic.ifname);
-            nic.ifname = strdup((*argv) + 8);
-        } else if (!strncmp("model=", *argv, 6)) {
-            free(nic.model);
-            nic.model = strdup((*argv) + 6);
-        } else if (!strncmp("rate=", *argv, 5)) {
-        } else if (!strncmp("accel=", *argv, 6)) {
+        } else if (match_option("vifname", *argv, oparg)) {
+            replace_string(&nic.ifname, oparg);
+        } else if (match_option("model", *argv, oparg)) {
+            replace_string(&nic.model, oparg);
+        } else if (match_option("rate", *argv, oparg)) {
+        } else if (match_option("accel", *argv, oparg)) {
         } else {
             fprintf(stderr, "unrecognized argument `%s'\n", *argv);
             return 1;

  parent reply	other threads:[~2011-06-05 16:50 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-05 16:50 [PATCH 0 of 6 RESENT] A bunch of fixes for libxl Marek Marczykowski
2011-06-05 16:50 ` [PATCH 1 of 6 RESENT] libxl: Remove frontend and backend devices from xenstore after destroy Marek Marczykowski
2011-06-06 10:00   ` Ian Campbell
2011-06-27 16:29     ` Ian Jackson
2011-06-05 16:50 ` [PATCH 2 of 6 RESENT] libxl: Accept disk name in libxl_devid_to_device_disk Marek Marczykowski
2011-06-06 11:24   ` Ian Campbell
2011-06-06 11:31     ` Ian Campbell
2011-06-27 16:28     ` [PATCH 2 of 6 RESENT] libxl: Accept disk name in libxl_devid_to_device_disk [and 1 more messages] Ian Jackson
2011-06-05 16:50 ` [PATCH 3 of 6 RESENT] libxl: Allocate memory for strings in libxl_device_disk Marek Marczykowski
2011-06-06 11:24   ` Ian Campbell
2011-06-27 16:25   ` Ian Jackson
2011-06-05 16:50 ` Marek Marczykowski [this message]
2011-06-07 12:02   ` [PATCH 4 of 6 RESENT] xl: Use macros for param parsing in network-attach, to prevent use explicit sizeof("param") Stefano Stabellini
2011-06-05 16:50 ` [PATCH 5 of 6 RESENT] xen.lowlevel.xl: Return None on empty domain name Marek Marczykowski
2011-06-27 16:34   ` Ian Jackson
     [not found]   ` <19976.45675.438028.965156@mariner.uk.xensource.com>
2011-06-28 21:37     ` Backport libxl bugfixes to 4.1 (was: Re: [PATCH 5 of 6 RESENT] xen.lowlevel.xl: Return None on empty domain name) Marek Marczykowski
2011-06-28 21:57       ` Keir Fraser
2011-06-30 16:47       ` Ian Jackson
2011-08-25 11:05         ` Ian Jackson
2011-08-25 11:12           ` Backport libxl bugfixes to 4.1 Marek Marczykowski
2011-08-25 17:13             ` [PATCH 0 of 5] A bunch of fixes for libxl Marek Marczykowski
2011-08-25 17:13               ` [PATCH 1 of 5] libxl: Remove frontend and backend devices from xenstore after destroy Marek Marczykowski
2011-08-25 17:13               ` [PATCH 2 of 5] libxl: Accept disk name in libxl_devid_to_device_disk Marek Marczykowski
2011-08-25 17:13               ` [PATCH 3 of 5] libxl: Allocate memory for strings in libxl_device_disk Marek Marczykowski
2011-08-25 17:13               ` [PATCH 4 of 5] xen.lowlevel.xl: Return None on empty domain name Marek Marczykowski
2011-08-25 17:13               ` [PATCH 5 of 5] libxl: Do not SEGV when no 'removable' disk parameter in xenstore Marek Marczykowski
2011-08-30 17:19               ` [PATCH 0 of 5] A bunch of fixes for libxl Ian Jackson
2011-06-05 16:50 ` [PATCH 6 of 6 RESENT] libxl: Do not SEGV when no 'removable' disk parameter in xenstore Marek Marczykowski
2011-06-07 11:57   ` Stefano Stabellini
2011-06-07 12:00     ` Marek Marczykowski
2011-06-08 10:41       ` [PATCH] " Marek Marczykowski
2011-06-08 10:50         ` Stefano Stabellini
2011-06-27 16:37           ` [PATCH] libxl: Do not SEGV when no 'removable' disk parameter in xenstore [and 1 more messages] Ian Jackson
2011-06-08 10:42       ` [PATCH RESENTv2] libxl: Do not SEGV when no 'removable' disk parameter in xenstore Marek Marczykowski

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=0863dfd23b3a10bee6d3.1307292634@devel14 \
    --to=marmarek@mimuw.edu.pl \
    --cc=xen-devel@lists.xensource.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).