Skip to content

Commit 9c5654e

Browse files
committed
Increase portability by using uintptr_t rather than size_t for pointers
The only integral types guaranteed by the C standard, if they exist, to support having a pointer cast to them and back are (u)intptr_t. On most architectures, size_t and uintptr_t are typedefs for the same underlying type, so this code ends up working. However, on CHERI, and thus Arm's experimental Morello prototype, C language pointers are implemented with hardware capabilities, which are unforgeable pointers with bounds and permissions. This means that, whilst size_t remains a plain 32/64-bit integer size, (u)intotr_t is represented with a capability. Casting to size_t and back to a pointer causes the capability metadata to be lost and the resulting capability to be invalid, meaning it will trap when dereferenced. Instead, use uintptr_t, and provide fallback definitions for old versions of MSVC like for the other C99 integer types.
1 parent 5d30e42 commit 9c5654e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

cmake/sysdep.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
typedef unsigned __int32 uint32_t;
3434
typedef signed __int64 int64_t;
3535
typedef unsigned __int64 uint64_t;
36+
# if defined(_WIN64)
37+
typedef signed __int64 intptr_t;
38+
typedef unsigned __int64 uintptr_t;
39+
# else
40+
typedef signed __int32 intptr_t;
41+
typedef unsigned __int32 uintptr_t;
42+
# endif
3643
#elif defined(_MSC_VER) // && _MSC_VER >= 1600
3744
# include <stdint.h>
3845
#else

include/msgpack/zone.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static inline void* msgpack_zone_malloc(msgpack_zone* zone, size_t size)
107107
{
108108
char* aligned =
109109
(char*)(
110-
(size_t)(
110+
(uintptr_t)(
111111
zone->chunk_list.ptr + (MSGPACK_ZONE_ALIGN - 1)
112112
) / MSGPACK_ZONE_ALIGN * MSGPACK_ZONE_ALIGN
113113
);
@@ -120,7 +120,7 @@ static inline void* msgpack_zone_malloc(msgpack_zone* zone, size_t size)
120120
{
121121
void* ptr = msgpack_zone_malloc_expand(zone, size + (MSGPACK_ZONE_ALIGN - 1));
122122
if (ptr) {
123-
return (char*)((size_t)(ptr) / MSGPACK_ZONE_ALIGN * MSGPACK_ZONE_ALIGN);
123+
return (char*)((uintptr_t)(ptr) / MSGPACK_ZONE_ALIGN * MSGPACK_ZONE_ALIGN);
124124
}
125125
}
126126
return NULL;

0 commit comments

Comments
 (0)