lib/nbt: Prevent addition of additional end tags to compounds

Soda
BodgeMaster 2022-10-14 18:30:39 +02:00
parent 71834e1018
commit 7be73f86d4
2 changed files with 16 additions and 5 deletions

View File

@ -988,10 +988,17 @@ namespace NBT {
}
ErrorOrVoid Compound::setElementPointerAt(uint64_t position, Generic* pointer) {
// built-in end tag
if (position == this->tags.size()) {
if (position == this->tags.size() || pointer->getTagType() == TagType::END) {
if (position == this->tags.size() && pointer->getTagType() == TagType::END) {
delete pointer;
// do nothing, already have one of those
} else {
delete pointer;
// End tags may only go at the end and
// the end may only hold an end tag.
return ErrorOrVoid(true, ErrorCodes::NOT_ALLOWED);
}
}
if (position > this->tags.size()) {
return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE);
}
@ -1002,8 +1009,12 @@ namespace NBT {
return ErrorOrVoid();
}
void Compound::appendPointer(Generic* pointer) {
ErrorOrVoid Compound::appendPointer(Generic* pointer) {
if (pointer->getTagType() == TagType::END) {
return ErrorOrVoid(true, ErrorCodes::NOT_ALLOWED);
}
this->tags.push_back(pointer);
return ErrorOrVoid();
}
ErrorOrVoid Compound::deleteElement(uint64_t position) {

View File

@ -257,7 +257,7 @@ namespace NBT {
ErrorOr<Generic*> getElementPointer(uint64_t position);
ErrorOrVoid setElementPointerAt(uint64_t position, Generic* pointer);
void appendPointer(Generic* pointer);
ErrorOrVoid appendPointer(Generic* pointer);
ErrorOrVoid deleteElement(uint64_t position);
uint64_t length();
};