[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/5] Add an explicit type parameter to the P2ROUNDUP() macro



From: Paul Durrant <pdurrant@xxxxxxxxxx>

Because it uses signed logic internally it is currently quite vulnerable to
mismatched argument types leading to weird evaluations. Therefore it's safer
to give it an explicit type parameter and have it cast its other arguments to
that type.

Signed-off-by: Paul Durrant <pdurrant@xxxxxxxxxx>
---
 src/common/util.h  | 4 ++--
 src/xen/system.c   | 2 +-
 src/xenbus/cache.c | 9 +++++----
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/common/util.h b/src/common/util.h
index 36a36dd639ec..31a224d5e9f4 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -36,8 +36,8 @@
 
 #include "assert.h"
 
-#define        P2ROUNDUP(_x, _a)   \
-        (-(-(_x) & -(_a)))
+#define        P2ROUNDUP(_t, _x, _a)   \
+        (-(-((_t)(_x)) & -(((_t)(_a)))))
 
 static FORCEINLINE LONG
 __ffs(
diff --git a/src/xen/system.c b/src/xen/system.c
index 7220faafd25f..6b2a3dc1eff9 100644
--- a/src/xen/system.c
+++ b/src/xen/system.c
@@ -922,7 +922,7 @@ SystemAllocateVcpuInfo(
 
     Size = sizeof (vcpu_info_t) * HVM_MAX_VCPUS;
     Size += sizeof (BOOLEAN) * HVM_MAX_VCPUS;
-    Size = P2ROUNDUP(Size, PAGE_SIZE);
+    Size = P2ROUNDUP(ULONG, Size, PAGE_SIZE);
 
     Context->Mdl = DriverGetNamedPages("VCPU_INFO", Size >> PAGE_SHIFT);
 
diff --git a/src/xenbus/cache.c b/src/xenbus/cache.c
index 13fb0e5cfdb8..3813e414de58 100644
--- a/src/xenbus/cache.c
+++ b/src/xenbus/cache.c
@@ -299,7 +299,8 @@ CacheCreateSlab(
     LONG                SlabCount;
     NTSTATUS            status;
 
-    NumberOfBytes = P2ROUNDUP(FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer) +
+    NumberOfBytes = P2ROUNDUP(ULONG,
+                              FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer) +
                               Cache->Size,
                               PAGE_SIZE);
     Count = (NumberOfBytes - FIELD_OFFSET(XENBUS_CACHE_SLAB, Buffer)) /
@@ -323,7 +324,7 @@ CacheCreateSlab(
     Slab->Cache = Cache;
     Slab->MaximumOccupancy = (USHORT)Count;
 
-    Size = P2ROUNDUP(Count, BITS_PER_ULONG);
+    Size = P2ROUNDUP(ULONG, Count, BITS_PER_ULONG);
     Size /= 8;
 
     Slab->Mask = __CacheAllocate(Size);
@@ -421,7 +422,7 @@ __CacheMaskScan(
     ULONG       Size;
     ULONG       Index;
 
-    Size = P2ROUNDUP(Maximum, BITS_PER_ULONG);
+    Size = P2ROUNDUP(ULONG, Maximum, BITS_PER_ULONG);
     Size /= sizeof (ULONG);
     ASSERT(Size != 0);
 
@@ -777,7 +778,7 @@ CacheCreate(
     if (!NT_SUCCESS(status))
         goto fail2;
 
-    Size = P2ROUNDUP(Size, sizeof (ULONG_PTR));
+    Size = P2ROUNDUP(ULONG, Size, sizeof (ULONG_PTR));
 
     if (Cap == 0)
         Cap = ULONG_MAX;
-- 
2.25.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.