]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage: avoid crash on parse error
authorEric Blake <eblake@redhat.com>
Mon, 4 Jul 2011 02:41:38 +0000 (10:41 +0800)
committerDaniel Veillard <veillard@redhat.com>
Mon, 4 Jul 2011 02:41:38 +0000 (10:41 +0800)
Coverity detected that we could crash on bogus input.  Meanwhile,
strtok_r is rather heavy compared to strchr.

* src/storage/storage_backend_iscsi.c (virStorageBackendIQNFound):
  Check for parse failure, and use lighter-weight functions.

src/storage/storage_backend_iscsi.c

index 15b5862e6423e2135f18476674fbc9e46360b762..72887e3c71c7f54cb7756ee84e322add4894625a 100644 (file)
@@ -183,8 +183,7 @@ virStorageBackendIQNFound(const char *initiatoriqn,
     int ret = IQN_MISSING, fd = -1;
     char ebuf[64];
     FILE *fp = NULL;
-    char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL,
-        *saveptr = NULL;
+    char *line = NULL, *newline = NULL, *iqn = NULL, *token = NULL;
     virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
                                              "--mode", "iface", NULL);
 
@@ -232,8 +231,15 @@ virStorageBackendIQNFound(const char *initiatoriqn,
         iqn++;
 
         if (STREQ(iqn, initiatoriqn)) {
-            token = strtok_r(line, " ", &saveptr);
-            *ifacename = strdup(token);
+            token = strchr(line, ' ');
+            if (!token) {
+                ret = IQN_ERROR;
+                virStorageReportError(VIR_ERR_INTERNAL_ERROR,
+                                      _("Missing space when parsing output "
+                                        "of '%s'"), ISCSIADM);
+                goto out;
+            }
+            *ifacename = strndup(line, token - line);
             if (*ifacename == NULL) {
                 ret = IQN_ERROR;
                 virReportOOMError();