From 74c639ec3a14d7818a2f64f940efffca39b98594 Mon Sep 17 00:00:00 2001 From: fincs Date: Sun, 5 Dec 2021 19:31:42 +0100 Subject: [PATCH] random.c: Avoid UB type punning that results in miscompilation --- nx/source/kernel/random.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/nx/source/kernel/random.c b/nx/source/kernel/random.c index 6d875ae7..24e68558 100644 --- a/nx/source/kernel/random.c +++ b/nx/source/kernel/random.c @@ -18,10 +18,17 @@ #define ROTL32(x, n) (((x) << (n)) | ((x) >> (32-(n)))) #define U32TO8_LITTLE(p, v) \ - (*(u32*)(p) = (v)) + ({ \ + u32 __temp = (v); \ + __builtin_memcpy((void*)(p), &__temp, 4); \ + }) #define U8TO32_LITTLE(p) \ - (*(const u32*)(p)) + ({ \ + u32 __temp; \ + __builtin_memcpy(&__temp, (const void*)(p), 4); \ + __temp; \ + }) #define ROTATE(v,c) (ROTL32(v,c)) #define XOR(v,w) ((v) ^ (w))