lib/nbt: Build an end tag object into compound tag objects to prevent creation of endless compounds

Soda
BodgeMaster 2022-10-14 18:08:49 +02:00
parent ccce564219
commit 71834e1018
2 changed files with 24 additions and 4 deletions

View File

@ -940,23 +940,27 @@ namespace NBT {
Compound::Compound() { Compound::Compound() {
this->type = TagType::COMPOUND; this->type = TagType::COMPOUND;
this->endPointer = new End();
} }
Compound::Compound(tiny_utf8::string name) { Compound::Compound(tiny_utf8::string name) {
this->type = TagType::COMPOUND; this->type = TagType::COMPOUND;
this->name = name; this->name = name;
this->endPointer = new End();
} }
Compound::Compound(tiny_utf8::string name, std::vector<Generic*> data) { Compound::Compound(tiny_utf8::string name, std::vector<Generic*> data) {
this->type = TagType::COMPOUND; this->type = TagType::COMPOUND;
this->name = name; this->name = name;
this->tags = data; this->tags = data;
this->endPointer = new End();
} }
Compound::~Compound() { Compound::~Compound() {
for (uint64_t i=0; i<this->tags.size(); i++) { for (uint64_t i=0; i<this->tags.size(); i++) {
delete this->tags.at(i); delete this->tags.at(i);
} }
delete this->endPointer;
} }
ErrorOrVoid Compound::serializeWithoutHeader(std::vector<uint8_t>* rawData) { ErrorOrVoid Compound::serializeWithoutHeader(std::vector<uint8_t>* rawData) {
@ -966,20 +970,29 @@ namespace NBT {
return result; return result;
} }
} }
this->endPointer->serialize(rawData);
return ErrorOrVoid(); return ErrorOrVoid();
} }
ErrorOr<Generic*> Compound::getElementPointer(uint64_t position) { ErrorOr<Generic*> Compound::getElementPointer(uint64_t position) {
if (position >= this->tags.size()) { if (position > this->tags.size()) {
return ErrorOr<Generic*>(true, ErrorCodes::OUT_OF_RANGE); return ErrorOr<Generic*>(true, ErrorCodes::OUT_OF_RANGE);
} }
if (position == this->tags.size()) {
return this->endPointer;
}
return ErrorOr<Generic*>(this->tags.at(position)); return ErrorOr<Generic*>(this->tags.at(position));
} }
ErrorOrVoid Compound::setElementPointerAt(uint64_t position, Generic* pointer) { ErrorOrVoid Compound::setElementPointerAt(uint64_t position, Generic* pointer) {
if (position >= this->tags.size()) { // built-in end tag
if (position == this->tags.size()) {
return ErrorOrVoid(true, ErrorCodes::NOT_ALLOWED);
}
if (position > this->tags.size()) {
return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE); return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE);
} }
@ -994,7 +1007,11 @@ namespace NBT {
} }
ErrorOrVoid Compound::deleteElement(uint64_t position) { ErrorOrVoid Compound::deleteElement(uint64_t position) {
if (position >= this->tags.size()) { // built-in end tag
if (position == this->tags.size()) {
return ErrorOrVoid(true, ErrorCodes::NOT_ALLOWED);
}
if (position > this->tags.size()) {
return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE); return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE);
} }
@ -1005,7 +1022,8 @@ namespace NBT {
} }
uint64_t Compound::length() { uint64_t Compound::length() {
return this->tags.size(); // account for built-in end tag
return this->tags.size()+1;
} }
Int32Array::Int32Array() { Int32Array::Int32Array() {

View File

@ -244,6 +244,8 @@ namespace NBT {
class Compound: public Generic { class Compound: public Generic {
private: private:
std::vector<Generic*> tags; std::vector<Generic*> tags;
// built-in end tag
End* endPointer;
public: public:
Compound(); Compound();
Compound(tiny_utf8::string name); Compound(tiny_utf8::string name);