From c004bea5e463f6d8c97ee7a96da21fca80cfde75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 5 May 2024 12:17:29 +0200 Subject: [PATCH] Use memchr in IsValidNullTerminatedString --- Core/MemMap.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Core/MemMap.h b/Core/MemMap.h index 247d4d3478..b9f01fb506 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -270,10 +270,6 @@ inline bool IsKernelAndNotVolatileAddress(const u32 address) { bool IsScratchpadAddress(const u32 address); -inline const char *GetCharPointerUnchecked(const u32 address) { - return (const char *)GetPointerUnchecked(address); -} - inline void MemcpyUnchecked(void *to_data, const u32 from_address, const u32 len) { memcpy(to_data, GetPointerUnchecked(from_address), len); } @@ -314,6 +310,8 @@ inline u32 MaxSizeAtAddress(const u32 address){ } } +// NOTE: Unlike the similar IsValidRange/IsValidAddress functions, this one is linear cost vs the size of the string, +// for hopefully-obvious reasons. inline bool IsValidNullTerminatedString(const u32 address) { u32 max_size = MaxSizeAtAddress(address); if (max_size == 0) { @@ -321,10 +319,8 @@ inline bool IsValidNullTerminatedString(const u32 address) { } const char *c = GetCharPointerUnchecked(address); - for (u32 i = 0; i < max_size; i++) { - if (c[i] == '\0') { - return true; - } + if (memchr(c, '\0', max_size)) { + return true; } return false; } @@ -341,6 +337,10 @@ inline bool IsValidRange(const u32 address, const u32 size) { return ValidSize(address, size) == size; } +inline const char *GetCharPointerUnchecked(const u32 address) { + return (const char *)GetPointerUnchecked(address); +} + // Used for auto-converted char * parameters, which can sometimes legitimately be null - // so we don't want to get caught in GetPointer's crash reporting. inline const char *GetCharPointer(const u32 address) {