From 7e049fcfd110e1a348183c6cdbcd9fc7ee682341 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 13 Jul 2022 21:00:42 +0200 Subject: [PATCH] lib/error: rename and add error constants --- src/lib/error.h++ | 16 ++++++++++++++-- src/lib/nbt.cpp | 22 +++++++++++----------- src/test/nbt_helpers.cpp | 40 ++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/lib/error.h++ b/src/lib/error.h++ index f8fd555..d0cbf97 100644 --- a/src/lib/error.h++ +++ b/src/lib/error.h++ @@ -68,9 +68,21 @@ namespace ErrorCodes { // Using them is optional as ErrorOr<> accepts any uint8_t value as // error code, but they are useful for readability. + // Ahh yes, very useful. + const uint8_t SUCCESS = 0; + // IndexOutOfRangeException equivalent - const uint8_t RANGE_ERROR = 1; + const uint8_t OUT_OF_RANGE = 1; // when going out of bounds in a non-predetermined way - const uint8_t OVERRUN_ERROR = 2; + const uint8_t OVERRUN = 2; + + // when checking for presence of something, for example CLI arguments + const uint8_t NOT_PRESENT = 3; + + const uint8_t WRONG_USAGE = 4; + + const uint8_t UNIMPLEMENTED = 254; + + const uint8_t UNKNOWN = 255; } diff --git a/src/lib/nbt.cpp b/src/lib/nbt.cpp index c320291..0db2f97 100644 --- a/src/lib/nbt.cpp +++ b/src/lib/nbt.cpp @@ -34,17 +34,17 @@ namespace NBT { namespace helper { ErrorOr readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize(true, ErrorCodes::RANGE_ERROR); + if (dataSize(true, ErrorCodes::OUT_OF_RANGE); return ErrorOr((int8_t) data[currentPosition]); } ErrorOr readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize(true, ErrorCodes::RANGE_ERROR); + if (dataSize(true, ErrorCodes::OUT_OF_RANGE); return ErrorOr((int16_t) ((static_cast(data[currentPosition]) << 8) | static_cast(data[currentPosition+1]))); } ErrorOr readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize(true, ErrorCodes::RANGE_ERROR); + if (dataSize(true, ErrorCodes::OUT_OF_RANGE); return ErrorOr((int32_t) ( (static_cast(data[currentPosition ]) << 24) | (static_cast(data[currentPosition+1]) << 16) | @@ -54,7 +54,7 @@ namespace NBT { } ErrorOr readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { - if (dataSize(true, ErrorCodes::RANGE_ERROR); + if (dataSize(true, ErrorCodes::OUT_OF_RANGE); return ErrorOr((int64_t) ( (static_cast(data[currentPosition ]) << 56) | (static_cast(data[currentPosition+1]) << 48) | @@ -73,8 +73,8 @@ namespace NBT { ErrorOr readFloat32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { float* value = new float; uint8_t* valueAsBytes = reinterpret_cast(value); - if (dataSize<=currentPosition) return ErrorOr(true, ErrorCodes::RANGE_ERROR); - if (dataSize(true, ErrorCodes::OVERRUN_ERROR); + if (dataSize<=currentPosition) return ErrorOr(true, ErrorCodes::OUT_OF_RANGE); + if (dataSize(true, ErrorCodes::OVERRUN); #ifdef FOSSVG_BIG_ENDIAN *valueAsBytes = data[currentPosition]; *(valueAsBytes+1) = data[currentPosition+1]; @@ -101,8 +101,8 @@ namespace NBT { ErrorOr readFloat64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) { double* value = new double; uint8_t* valueAsBytes = reinterpret_cast(value); - if (dataSize<=currentPosition) return ErrorOr(true, ErrorCodes::RANGE_ERROR); - if (dataSize(true, ErrorCodes::OVERRUN_ERROR); + if (dataSize<=currentPosition) return ErrorOr(true, ErrorCodes::OUT_OF_RANGE); + if (dataSize(true, ErrorCodes::OVERRUN); #ifdef FOSSVG_BIG_ENDIAN *valueAsBytes = data[currentPosition]; *(valueAsBytes+1) = data[currentPosition+1]; @@ -137,7 +137,7 @@ namespace NBT { if (size.isError) return ErrorOr>(true, size.errorCode); // get content - if (currentPosition+4+size.value > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN_ERROR); + if (currentPosition+4+size.value > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN); std::vector result = std::vector(); for (int i=0; i>(true, size.errorCode); // get content - if (currentPosition+4+(size.value*4) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN_ERROR); + if (currentPosition+4+(size.value*4) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN); std::vector result = std::vector(); for (int i=0; i nextInt32 = readInt32(data, dataSize, currentPosition+4+(i*4)); @@ -175,7 +175,7 @@ namespace NBT { if (size.isError) return ErrorOr>(true, size.errorCode); // get content - if (currentPosition+4+(size.value*8) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN_ERROR); + if (currentPosition+4+(size.value*8) > dataSize) return ErrorOr>(true, ErrorCodes::OVERRUN); std::vector result = std::vector(); for (int i=0; i nextInt64 = readInt64(data, dataSize, currentPosition+4+(i*8)); diff --git a/src/test/nbt_helpers.cpp b/src/test/nbt_helpers.cpp index 3351794..d77f8b1 100644 --- a/src/test/nbt_helpers.cpp +++ b/src/test/nbt_helpers.cpp @@ -44,7 +44,7 @@ int main(){ // out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt8(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt8(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt8(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt8 NBT helper test" << std::endl; @@ -72,11 +72,11 @@ int main(){ // partially out of bounds currentPosition = 9; ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt16(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt16 NBT helper test" << std::endl; @@ -104,11 +104,11 @@ int main(){ // partially out of bounds currentPosition = 7; ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt32(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt32 NBT helper test" << std::endl; @@ -141,11 +141,11 @@ int main(){ // partially out of bounds currentPosition = 3; ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt64(dataForIntTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt64 NBT helper test" << std::endl; @@ -194,15 +194,15 @@ int main(){ // read overrun currentPosition = 20; ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN); // read with size partially out of bounds currentPosition = 114; ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // read out of bounds currentPosition = 200; ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt8Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt8Array NBT helper test" << std::endl; @@ -246,15 +246,15 @@ int main(){ // read overrun currentPosition = 20; ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN); // read with size partially out of bounds currentPosition = 114; ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // read out of bounds currentPosition = 200; ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt32Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt32Array NBT helper test" << std::endl; @@ -307,15 +307,15 @@ int main(){ // read overrun currentPosition = 20; ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN); // read with size partially out of bounds currentPosition = 114; ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); // read out of bounds currentPosition = 200; ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readInt64Array(dataForIntArrayTest, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readInt64Array NBT helper test" << std::endl; @@ -383,11 +383,11 @@ int main(){ // read overrun currentPosition = 1; ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN); // read out of bounds currentPosition = 4; ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readFloat32(dataForFloat32Test, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readFloat32 NBT helper test" << std::endl; @@ -414,11 +414,11 @@ int main(){ // read overrun currentPosition = 1; ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN_ERROR); + ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).errorCode == ErrorCodes::OVERRUN); // read out of bounds currentPosition = 8; ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).isError == true); - ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); + ASSERT(NBT::helper::readFloat64(dataForFloat64Test, dataSize, currentPosition).errorCode == ErrorCodes::OUT_OF_RANGE); std::cout << "Passed readFloat64 NBT helper test" << std::endl;