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