From 664182c2899ba341be31f4cd7364a74867ee422c Mon Sep 17 00:00:00 2001 From: Kenix3 Date: Thu, 4 Jun 2020 21:41:44 -0400 Subject: Fixes known undefined behaviour from DmaMgr and Lib_Ptr taking u32 rather than void* --- src/code/z_lib.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src/code/z_lib.c') diff --git a/src/code/z_lib.c b/src/code/z_lib.c index b30941ae5..f4209e98b 100644 --- a/src/code/z_lib.c +++ b/src/code/z_lib.c @@ -606,30 +606,37 @@ f32 Lib_PushAwayVec3f(Vec3f* start, Vec3f* pusher, f32 distanceToApproach) { void Lib_Nop801004FC(void) {} -u32 Lib_PtrSegToVirt(void* ptr) { - return(gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000; +void* Lib_PtrSegToVirt(void* ptr) { + // TODO: PHYSICAL_TO_VIRTUAL macro + // UB to cast the pointer to u32 + return (void*)(((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000); } -u32 Lib_PtrSegToVirtNull(void* ptr) { +void* Lib_PtrSegToVirtNull(void* ptr) { + // UB to cast the pointer to u32 in order to bitshift. if (((u32)ptr >> 28) == 0) { - return (u32)ptr; + return ptr; } - return(gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000; + // TODO: PHYSICAL_TO_VIRTUAL macro + // UB to cast the pointer to u32 + return (void*)(((u32)gRspSegmentPhysAddrs[((u32)ptr << 4) >> 28] + ((u32)ptr & 0xFFFFFF)) + 0x80000000); } -u32 Lib_PtrSegToK0(void* ptr) { +void* Lib_PtrSegToK0(void* ptr) { if (ptr == NULL) { - return 0; + return NULL; } else { - return (u32)ptr + 0x80000000; + // TODO: PHYSICAL_TO_VIRTUAL macro + return (void*)((u32)ptr + 0x80000000); } } -u32 Lib_PtrSegToK0Null(void* ptr) { +void* Lib_PtrSegToK0Null(void* ptr) { if (ptr == NULL) { - return 0; + return NULL; } else { - return (u32)ptr + 0x80000000; + // TODO: PHYSICAL_TO_VIRTUAL macro + return (void*)((u32)ptr + 0x80000000); } } \ No newline at end of file -- cgit v1.2.3