fix(relinker): check byte writer bounds

This commit is contained in:
DiulioAires committed 2026-10-04 20:17:21 -03:00
1 parent 33a870d973
commit b6c97084ed
3 files changed
+16

No files matched your search

+1
View File
@@ -128,6 +128,7 @@ if(BUILD_TESTING)
add_test_executable(buffer_bounds_tests
${CMAKE_CURRENT_SOURCE_DIR}/io/tests/BufferBoundsTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/src/ByteReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/src/ByteWriter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/io/src/BufferUtils.cpp
)
target_include_directories(buffer_bounds_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/io/include)
+9
View File
@@ -1,21 +1,30 @@
#include <io/ByteWriter.hpp>
#include <cstring>
#include <stdexcept>
namespace Io {
void ByteWriter::WriteU8(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint8_t v) const {
if (offset >= buf.size())
throw std::out_of_range("ByteWriter::WriteU8 out of bounds");
buf[offset] = v;
}
void ByteWriter::WriteU16(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint16_t v) const {
if (offset > buf.size() || buf.size() - offset < 2)
throw std::out_of_range("ByteWriter::WriteU16 out of bounds");
std::memcpy(buf.data() + offset, &v, 2);
}
void ByteWriter::WriteU32(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint32_t v) const {
if (offset > buf.size() || buf.size() - offset < 4)
throw std::out_of_range("ByteWriter::WriteU32 out of bounds");
std::memcpy(buf.data() + offset, &v, 4);
}
void ByteWriter::WriteU64(std::vector<std::uint8_t>& buf, std::size_t offset, std::uint64_t v) const {
if (offset > buf.size() || buf.size() - offset < 8)
throw std::out_of_range("ByteWriter::WriteU64 out of bounds");
std::memcpy(buf.data() + offset, &v, 8);
}
@@ -1,5 +1,6 @@
#include <io/BufferUtils.hpp>
#include <io/ByteReader.hpp>
#include <io/ByteWriter.hpp>
#include <cstring>
#include <iostream>
#include <limits>
@@ -75,9 +76,14 @@ void writeBounds(const TWrite& write, const std::string& name) {
int main() {
try {
const Io::ByteReader reader;
const Io::ByteWriter writer;
readBounds<std::uint16_t>([&](const Bytes& bytes, std::size_t offset) { return reader.ReadU16(bytes, offset); }, "ByteReader::ReadU16");
readBounds<std::uint32_t>([&](const Bytes& bytes, std::size_t offset) { return reader.ReadU32(bytes, offset); }, "ByteReader::ReadU32");
readBounds<std::uint64_t>([&](const Bytes& bytes, std::size_t offset) { return reader.ReadU64(bytes, offset); }, "ByteReader::ReadU64");
writeBounds<std::uint8_t>([&](Bytes& bytes, std::size_t offset, std::uint8_t value) { writer.WriteU8(bytes, offset, value); }, "ByteWriter::WriteU8");
writeBounds<std::uint16_t>([&](Bytes& bytes, std::size_t offset, std::uint16_t value) { writer.WriteU16(bytes, offset, value); }, "ByteWriter::WriteU16");
writeBounds<std::uint32_t>([&](Bytes& bytes, std::size_t offset, std::uint32_t value) { writer.WriteU32(bytes, offset, value); }, "ByteWriter::WriteU32");
writeBounds<std::uint64_t>([&](Bytes& bytes, std::size_t offset, std::uint64_t value) { writer.WriteU64(bytes, offset, value); }, "ByteWriter::WriteU64");
readBounds<std::uint16_t>(Io::ReadU16, "ReadU16");
readBounds<std::uint32_t>(Io::ReadU32, "ReadU32");
readBounds<std::uint64_t>(Io::ReadU64, "ReadU64");