1
0
mirror of synced 2025-01-18 00:56:49 +01:00

fix: Out of bounds strlen read

This commit is contained in:
WerWolv 2022-08-16 00:29:53 +02:00 committed by GitHub
parent 43ab72dcb8
commit 29e970fd81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -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<class... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts>

View File

@ -1,7 +1,8 @@
#include <hex/helpers/file.hpp>
#include <hex/helpers/utils.hpp>
#include <unistd.h>
#include <cstring>
namespace hex::fs {
@ -90,7 +91,7 @@ namespace hex::fs {
return "";
auto cString = reinterpret_cast<const char *>(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<const char8_t *>(bytes.data());
return { cString, std::min(bytes.size(), std::strlen(reinterpret_cast<const char*>(bytes.data()))) };
return { cString, hex::strnlen(reinterpret_cast<const char*>(bytes.data()), bytes.size())) };
}
void File::write(const u8 *buffer, size_t size) {