diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index e3d1034f6..f56662545 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -84,6 +84,13 @@ namespace hex { return result; } + constexpr inline size_t strnlen(const char *s, size_t n) { + size_t i = 0; + while (i < n && s[i] != '\x00') i++; + + return i; + } + template struct overloaded : Ts... { using Ts::operator()...; }; template diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index 05ddd39c5..e24765601 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -1,7 +1,8 @@ #include +#include + #include -#include namespace hex::fs { @@ -90,7 +91,7 @@ namespace hex::fs { return ""; auto cString = reinterpret_cast(bytes.data()); - return { cString, std::strnlen(cString, bytes.size()) }; + return { cString, hex::strnlen(cString, bytes.size()) }; } std::u8string File::readU8String(size_t numBytes) { @@ -104,7 +105,7 @@ namespace hex::fs { return u8""; auto cString = reinterpret_cast(bytes.data()); - return { cString, std::min(bytes.size(), std::strlen(reinterpret_cast(bytes.data()))) }; + return { cString, hex::strnlen(reinterpret_cast(bytes.data()), bytes.size())) }; } void File::write(const u8 *buffer, size_t size) {