lib/nbt: Get rid of that ugly #define return hack

Instead of doing #define return, the boolean returnValue is set and
a goto statement is used to get to the code that does what the macro
used to do.
Soda
BodgeMaster 2022-10-15 18:55:58 +02:00
parent ca0af3306f
commit 8b62ec9c88
1 changed files with 35 additions and 14 deletions

View File

@ -1307,8 +1307,8 @@ namespace NBT {
// - BodgeMaster // - BodgeMaster
} }
bool returnValue;
uint64_t currentPosition = initialPosition; uint64_t currentPosition = initialPosition;
#define return if (processedDataSize!=nullptr) *processedDataSize = currentPosition-initialPosition; return
while (currentPosition<dataSize) { while (currentPosition<dataSize) {
ErrorOr<uint64_t> nextTagSize = Helper::totalTagSize(data, dataSize, currentPosition); ErrorOr<uint64_t> nextTagSize = Helper::totalTagSize(data, dataSize, currentPosition);
if (nextTagSize.isError) { if (nextTagSize.isError) {
@ -1316,7 +1316,8 @@ namespace NBT {
// attempt parsing the name // attempt parsing the name
ErrorOr<tiny_utf8::string> tagName = Helper::readString(data, dataSize, currentPosition+1); ErrorOr<tiny_utf8::string> tagName = Helper::readString(data, dataSize, currentPosition+1);
if (tagName.isError) { if (tagName.isError) {
return false; returnValue = false;
goto returnNow;
} }
// used seek to the start of the list's/compounds contents // used seek to the start of the list's/compounds contents
@ -1332,7 +1333,8 @@ namespace NBT {
// type byte + two name size bytes = 3 // type byte + two name size bytes = 3
if (!validateRawListContents(data, dataSize, currentPosition + (uint64_t) nameSize + 3, processedTagSize)) { if (!validateRawListContents(data, dataSize, currentPosition + (uint64_t) nameSize + 3, processedTagSize)) {
delete processedTagSize; delete processedTagSize;
return false; returnValue = false;
goto returnNow;
} }
*processedTagSize += (uint64_t) nameSize + 3; *processedTagSize += (uint64_t) nameSize + 3;
} }
@ -1340,7 +1342,8 @@ namespace NBT {
// type byte + two name size bytes = 3 // type byte + two name size bytes = 3
if (!validateRawNBTData(data, dataSize, currentPosition + (uint64_t) nameSize + 3, processedTagSize)) { if (!validateRawNBTData(data, dataSize, currentPosition + (uint64_t) nameSize + 3, processedTagSize)) {
delete processedTagSize; delete processedTagSize;
return false; returnValue = false;
goto returnNow;
} }
*processedTagSize += (uint64_t) nameSize + 3; *processedTagSize += (uint64_t) nameSize + 3;
} }
@ -1349,17 +1352,20 @@ namespace NBT {
delete processedTagSize; delete processedTagSize;
continue; continue;
} }
return false; returnValue = false;
goto returnNow;
} }
if (currentPosition + nextTagSize.value > dataSize) { if (currentPosition + nextTagSize.value > dataSize) {
return false; returnValue = false;
goto returnNow;
} }
// recursion abort condition // recursion abort condition
if (data[currentPosition]==TagType::END) { if (data[currentPosition]==TagType::END) {
currentPosition++; currentPosition++;
return true; returnValue = true;
goto returnNow;
} }
// nameSize cannot be an error here bc it got checked in // nameSize cannot be an error here bc it got checked in
@ -1371,11 +1377,15 @@ namespace NBT {
// This shouldn't matter too much here as the only error condition // This shouldn't matter too much here as the only error condition
// the parser function deals with rn is an overrun which is already // the parser function deals with rn is an overrun which is already
// being guarded against with // being guarded against with
// if (currentPosition + nextTagSize.value > dataSize) return false; // if (currentPosition + nextTagSize.value > dataSize) {
// returnValue = false;
// goto returnNow;
// }
// It might, however, turn out to be a useful check in the future. // It might, however, turn out to be a useful check in the future.
ErrorOr<tiny_utf8::string> name = Helper::readString(data, dataSize, currentPosition+1); ErrorOr<tiny_utf8::string> name = Helper::readString(data, dataSize, currentPosition+1);
if (name.isError) { if (name.isError) {
return false; returnValue = false;
goto returnNow;
} }
switch (data[currentPosition]) { switch (data[currentPosition]) {
@ -1393,14 +1403,18 @@ namespace NBT {
// This shouldn't matter too much here as the only // This shouldn't matter too much here as the only
// error condition the parser function deals with rn is // error condition the parser function deals with rn is
// an overrun which is already being guarded against with // an overrun which is already being guarded against with
// if (currentPosition + nextTagSize.value > dataSize) return false; // if (currentPosition + nextTagSize.value > dataSize) {
// returnValue = false;
// goto returnNow;
// }
// It might, however, turn out to be a useful check // It might, however, turn out to be a useful check
// in the future. // in the future.
// //
// type byte + two name size bytes = 3 // type byte + two name size bytes = 3
ErrorOr<tiny_utf8::string> content = Helper::readString(data, dataSize, currentPosition+nameSize+3); ErrorOr<tiny_utf8::string> content = Helper::readString(data, dataSize, currentPosition+nameSize+3);
if (content.isError) { if (content.isError) {
return false; returnValue = false;
goto returnNow;
} }
break; break;
} }
@ -1408,12 +1422,19 @@ namespace NBT {
case TagType::INT64_ARRAY: case TagType::INT64_ARRAY:
break; break;
default: default:
return false; returnValue = false;
goto returnNow;
} }
currentPosition += nextTagSize.value; currentPosition += nextTagSize.value;
} }
return true; returnValue = true;
#undef return goto returnNow;
returnNow:
if (processedDataSize!=nullptr) {
*processedDataSize = currentPosition-initialPosition;
}
return returnValue;
} }
} }