mirror of
https://github.com/gbdev/rgbds.git
synced 2026-11-21 21:48:39 +00:00
Validate section patch sizes in case object files are invalid (#2242)
This commit is contained in:
1 parent
a2086efa50
commit
bec11bcaa7
3 files changed
+22
-10
No files matched your search
@@ -8,7 +8,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "helpers.hpp" // QualifiedEquivalent
|
||||
#include "helpers.hpp" // assume, QualifiedEquivalent
|
||||
#include "linkdefs.hpp"
|
||||
|
||||
struct FileStackNode;
|
||||
@@ -28,6 +28,17 @@ struct Patch {
|
||||
Expression rpn;
|
||||
uint32_t offset;
|
||||
PatchType type;
|
||||
|
||||
uint8_t typeSize() const {
|
||||
assume(type < PATCHTYPE_INVALID);
|
||||
uint8_t typeSizes[PATCHTYPE_INVALID] = {
|
||||
1, // PATCHTYPE_BYTE
|
||||
2, // PATCHTYPE_WORD
|
||||
4, // PATCHTYPE_LONG
|
||||
1, // PATCHTYPE_JR
|
||||
};
|
||||
return typeSizes[type];
|
||||
}
|
||||
};
|
||||
|
||||
struct Section {
|
||||
|
||||
@@ -383,6 +383,15 @@ static void readSection(
|
||||
patch.type = PatchType(type);
|
||||
}
|
||||
|
||||
if (patch.offset > section.size || patch.offset + patch.typeSize() > section.size) {
|
||||
fatal(
|
||||
"%s: \"%s\"'s patch #%" PRIu32 " overflows the section",
|
||||
fileName,
|
||||
section.name.c_str(),
|
||||
patchID
|
||||
);
|
||||
}
|
||||
|
||||
uint32_t rpnSize;
|
||||
tryReadLong(
|
||||
rpnSize,
|
||||
|
||||
+1
-9
@@ -560,15 +560,7 @@ static void applyFilePatches(Section §ion, Section &dataSection) {
|
||||
for (Patch &patch : section.patches) {
|
||||
int32_t value = computeRPNExpr(patch.rpn, *section.fileSymbols);
|
||||
uint32_t offset = patch.offset + section.offset;
|
||||
|
||||
uint8_t typeSizes[PATCHTYPE_INVALID] = {
|
||||
1, // PATCHTYPE_BYTE
|
||||
2, // PATCHTYPE_WORD
|
||||
4, // PATCHTYPE_LONG
|
||||
1, // PATCHTYPE_JR
|
||||
};
|
||||
uint8_t typeSize = typeSizes[patch.type];
|
||||
|
||||
uint8_t typeSize = patch.typeSize();
|
||||
if (dataSection.data.size() < offset + typeSize) {
|
||||
rpnErrorAt(
|
||||
patch.rpn,
|
||||
|
||||
Reference in new issue
Block a user