]> xenbits.xensource.com Git - people/pauldu/xenbus.git/commitdiff
Add an explicit type parameter to the P2ROUNDUP() macro
authorPaul Durrant <pdurrant@amazon.com>
Wed, 31 Aug 2022 13:11:36 +0000 (14:11 +0100)
committerPaul Durrant <pdurrant@amazon.com>
Wed, 31 Aug 2022 15:39:51 +0000 (16:39 +0100)
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@amazon.com>
src/common/util.h
src/xen/system.c
src/xenbus/cache.c

index 36a36dd639ec61ab11ef3698521ade604d8f4778..31a224d5e9f49d19295988879427c996f1a9b723 100644 (file)
@@ -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(
index 7220faafd25f8c77d7b5351b8d417b57eb2132eb..6b2a3dc1eff91a5945634e748092340f638a6ecb 100644 (file)
@@ -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);
 
index 13fb0e5cfdb8348f534b897770f1bc8643deaf53..3813e414de58f08405dcdd946387573b4f85c283 100644 (file)
@@ -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;