tools/python/xen/lowlevel: some cleanups
authorKeir Fraser <keir.fraser@citrix.com>
Tue, 10 Jun 2008 08:17:55 +0000 (09:17 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Tue, 10 Jun 2008 08:17:55 +0000 (09:17 +0100)
Mainly:
 malloc(n * m) -> calloc(n, m)
 sprintf -> snprintf

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
tools/python/xen/lowlevel/acm/acm.c
tools/python/xen/lowlevel/xc/xc.c
tools/python/xen/lowlevel/xs/xs.c

index 1daa4e626a2adb6c9d69c4a7b55c53423ce9a97b..1762aa2fda5df21a410f712c86845e750400ce67 100644 (file)
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
 #include <netinet/in.h>
+#include <xenctrl.h>
 #include <xen/xsm/acm.h>
 #include <xen/xsm/acm_ops.h>
 
-#include <xenctrl.h>
-
 #define PERROR(_m, _a...) \
 fprintf(stderr, "ERROR: " _m " (%d = %s)\n" , ## _a ,    \
     errno, strerror(errno))
index 51e3ec79c9c72b17e497066c18362f9d32dbe748..af5ac34cd84a3522efe80107e5cfe3972b7926a7 100644 (file)
@@ -298,7 +298,8 @@ static PyObject *pyxc_domain_getinfo(XcObject *self,
                                       &first_dom, &max_doms) )
         return NULL;
 
-    if ( (info = malloc(max_doms * sizeof(xc_dominfo_t))) == NULL )
+    info = calloc(max_doms, sizeof(xc_dominfo_t));
+    if (info == NULL)
         return PyErr_NoMemory();
 
     nr_doms = xc_domain_getinfo(self->xc_handle, first_dom, max_doms, info);
@@ -664,9 +665,9 @@ static PyObject *pyxc_get_device_group(XcObject *self,
     /* Maximum allowed siblings device number per group */
     max_sdevs = 1024;
 
-    if ( (sdev_array = malloc(max_sdevs * sizeof(*sdev_array))) == NULL )
+    sdev_array = calloc(max_sdevs, sizeof(*sdev_array));
+    if (sdev_array == NULL)
         return PyErr_NoMemory();
-    memset(sdev_array, 0, max_sdevs * sizeof(*sdev_array));
 
     bdf |= (bus & 0xff) << 16;
     bdf |= (dev & 0x1f) << 11;
@@ -687,16 +688,16 @@ static PyObject *pyxc_get_device_group(XcObject *self,
        return Py_BuildValue("s", "");
     }
 
-    if ( (group_str = malloc(num_sdevs * sizeof(dev_str))) == NULL )
+    group_str = calloc(num_sdevs, sizeof(dev_str));
+    if (group_str == NULL)
         return PyErr_NoMemory();
-    memset(group_str, '\0', num_sdevs * sizeof(dev_str));
 
     for ( i = 0; i < num_sdevs; i++ )
     {
         bus = (sdev_array[i] >> 16) & 0xff;
         dev = (sdev_array[i] >> 11) & 0x1f;
         func = (sdev_array[i] >> 8) & 0x7;
-        sprintf(dev_str, "%02x:%02x.%x,", bus, dev, func);
+        snprintf(dev_str, sizeof(dev_str), "%02x:%02x.%x,", bus, dev, func);
         strcat(group_str, dev_str);
     }
 
@@ -1116,7 +1117,7 @@ static PyObject *pyxc_xeninfo(XcObject *self)
     if ( xc_version(self->xc_handle, XENVER_platform_parameters, &p_parms) != 0 )
         return pyxc_error_to_exception();
 
-    sprintf(str, "virt_start=0x%lx", p_parms.virt_start);
+    snprintf(str, sizeof(str), "virt_start=0x%lx", p_parms.virt_start);
 
     xen_pagesize = xc_version(self->xc_handle, XENVER_pagesize, NULL);
     if (xen_pagesize < 0 )
index d05365cb167c2145b161f2abd836d6284947e81e..6497126d2de73c9db51a4ac0b07c92b8c5926d81 100644 (file)
@@ -415,7 +415,7 @@ static PyObject *xspy_watch(XsHandle *self, PyObject *args)
     if (i == PyList_Size(self->watches))
         PyList_Append(self->watches, token);
 
-    sprintf(token_str, "%li", (unsigned long)token);
+    snprintf(token_str, sizeof(token_str), "%li", (unsigned long)token);
     Py_BEGIN_ALLOW_THREADS
     result = xs_watch(xh, path, token_str);
     Py_END_ALLOW_THREADS
@@ -500,7 +500,7 @@ static PyObject *xspy_unwatch(XsHandle *self, PyObject *args)
     if (!PyArg_ParseTuple(args, "sO", &path, &token))
         return NULL;
 
-    sprintf(token_str, "%li", (unsigned long)token);
+    snprintf(token_str, sizeof(token_str), "%li", (unsigned long)token);
     Py_BEGIN_ALLOW_THREADS
     result = xs_unwatch(xh, path, token_str);
     Py_END_ALLOW_THREADS
@@ -535,7 +535,7 @@ static PyObject *xspy_transaction_start(XsHandle *self)
         return NULL;
     }
 
-    sprintf(thstr, "%lX", (unsigned long)th);
+    snprintf(thstr, sizeof(thstr), "%lX", (unsigned long)th);
     return PyString_FromString(thstr);
 }