[libvirt] [PATCH 3/6] virCgroupController: Check the enum fits into 'int'

Michal Privoznik mprivozn at redhat.com
Fri Mar 27 14:26:51 UTC 2015


Throughout our code, the virCgroupController enum is used in two ways.
First as an index to an array of cgroup controllers:

struct virCgroup {
    char *path;

    struct virCgroupController controllers[VIR_CGROUP_CONTROLLER_LAST];
};

Second way is that when calling virCgroupNew() a bitmask of the enum
items can be passed to selectively detect only some controllers. For
instance:

int
virCgroupNewVcpu(virCgroupPtr domain,
                 int vcpuid,
                 bool create,
                 virCgroupPtr *group)
{
    ...
    controllers = ((1 << VIR_CGROUP_CONTROLLER_CPU) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUSET));

    if (virCgroupNew(-1, name, domain, controllers, group) < 0)
        goto cleanup;
}

Even though it's highly unlikely that so many new controllers will be
invented so that we would overflow when constructing the bitmask, it
doesn't hurt to check at compile time either.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/util/vircgroup.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index bfb3a9b..eee15ca 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -46,6 +46,11 @@ enum {
 };
 
 VIR_ENUM_DECL(virCgroupController);
+/* Items of this enum are used later in virCgroupNew to create
+ * bit array stored in int. Like this:
+ *   1 << VIR_CGROUP_CONTROLLER_CPU
+ * Make sure we will not overflow */
+verify(VIR_CGROUP_CONTROLLER_LAST < 8 * sizeof(int));
 
 bool virCgroupAvailable(void);
 
-- 
2.0.5




More information about the libvir-list mailing list