]> xenbits.xensource.com Git - libvirt.git/commitdiff
xen: Remove PATH_MAX sized stack allocation from block stats code
authorMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 9 Apr 2011 10:03:44 +0000 (12:03 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Sun, 10 Apr 2011 11:35:04 +0000 (13:35 +0200)
src/xen/block_stats.c

index e7c80c90369f73cbf2bd8e86ea13496b39abc8a8..1918257aefa5e12e8cf3918b7173419f08ee7d17 100644 (file)
@@ -113,34 +113,35 @@ read_stat (const char *path)
 }
 
 static int64_t
-read_bd_stat (int device, int domid, const char *str)
+read_bd_stat(int device, int domid, const char *str)
 {
-    char path[PATH_MAX];
+    static const char *paths[] = {
+        "/sys/bus/xen-backend/devices/vbd-%d-%d/statistics/%s",
+        "/sys/bus/xen-backend/devices/tap-%d-%d/statistics/%s",
+        "/sys/devices/xen-backend/vbd-%d-%d/statistics/%s",
+        "/sys/devices/xen-backend/tap-%d-%d/statistics/%s"
+    };
+
+    int i;
+    char *path;
     int64_t r;
 
-    snprintf (path, sizeof path,
-              "/sys/bus/xen-backend/devices/vbd-%d-%d/statistics/%s",
-              domid, device, str);
-    r = read_stat (path);
-    if (r >= 0) return r;
-
-    snprintf (path, sizeof path,
-              "/sys/bus/xen-backend/devices/tap-%d-%d/statistics/%s",
-              domid, device, str);
-    r = read_stat (path);
-    if (r >= 0) return r;
-
-    snprintf (path, sizeof path,
-              "/sys/devices/xen-backend/vbd-%d-%d/statistics/%s",
-              domid, device, str);
-    r = read_stat (path);
-    if (r >= 0) return r;
-
-    snprintf (path, sizeof path,
-              "/sys/devices/xen-backend/tap-%d-%d/statistics/%s",
-              domid, device, str);
-    r = read_stat (path);
-    return r;
+    for (i = 0; i < ARRAY_CARDINALITY(paths); ++i) {
+        if (virAsprintf(&path, paths[i], domid, device, str) < 0) {
+            virReportOOMError();
+            return -1;
+        }
+
+        r = read_stat(path);
+
+        VIR_FREE(path);
+
+        if (r >= 0) {
+            return r;
+        }
+    }
+
+    return -1;
 }
 
 /* In Xenstore, /local/domain/0/backend/vbd/<domid>/<device>/state,