1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-22 23:39:42 +00:00

Merge branch 'if_all_the_asserts' into 'master'

Replace all the `asserts` with `throw` in components/esm4/reader.cpp

See merge request OpenMW/openmw!3047
This commit is contained in:
jvoisin 2023-05-22 17:33:55 +00:00
commit 7f191a2a8a

View file

@ -28,7 +28,6 @@
#undef DEBUG_GROUPSTACK #undef DEBUG_GROUPSTACK
#include <cassert>
#include <iomanip> // for debugging #include <iomanip> // for debugging
#include <iostream> // for debugging #include <iostream> // for debugging
#include <sstream> // for debugging #include <sstream> // for debugging
@ -178,7 +177,8 @@ namespace ESM4
openRaw(std::move(stream), filename); openRaw(std::move(stream), filename);
// should at least have the size of ESM3 record header (20 or 24 bytes for ESM4) // should at least have the size of ESM3 record header (20 or 24 bytes for ESM4)
assert(mFileSize >= 16); if (mFileSize < 16)
throw std::runtime_error("File too small");
std::uint32_t modVer = 0; std::uint32_t modVer = 0;
if (getExact(modVer)) // get the first 4 bytes of the record header only if (getExact(modVer)) // get the first 4 bytes of the record header only
{ {
@ -404,7 +404,8 @@ namespace ESM4
void Reader::skipRecordData() void Reader::skipRecordData()
{ {
assert(mCtx.recordRead <= mCtx.recordHeader.record.dataSize && "Skipping after reading more than available"); if (mCtx.recordRead > mCtx.recordHeader.record.dataSize)
throw std::runtime_error("Skipping after reading more than available");
mStream->ignore(mCtx.recordHeader.record.dataSize - mCtx.recordRead); mStream->ignore(mCtx.recordHeader.record.dataSize - mCtx.recordRead);
mCtx.recordRead = mCtx.recordHeader.record.dataSize; // for getSubRecordHeader() mCtx.recordRead = mCtx.recordHeader.record.dataSize; // for getSubRecordHeader()
} }
@ -514,7 +515,9 @@ namespace ESM4
mCtx.groupStack.back().second += lastGroupSize; mCtx.groupStack.back().second += lastGroupSize;
lastGroupSize = mCtx.groupStack.back().first.groupSize; lastGroupSize = mCtx.groupStack.back().first.groupSize;
assert(lastGroupSize >= mCtx.groupStack.back().second && "Read more records than available"); if (lastGroupSize < mCtx.groupStack.back().second)
throw std::runtime_error("Read more records than available");
// #if 0 // #if 0
if (mCtx.groupStack.back().second > lastGroupSize) // FIXME: debugging only if (mCtx.groupStack.back().second > lastGroupSize) // FIXME: debugging only
std::cerr << printLabel(mCtx.groupStack.back().first.label, mCtx.groupStack.back().first.type) std::cerr << printLabel(mCtx.groupStack.back().first.label, mCtx.groupStack.back().first.type)
@ -537,7 +540,8 @@ namespace ESM4
void Reader::skipGroupData() void Reader::skipGroupData()
{ {
assert(!mCtx.groupStack.empty() && "Skipping group with an empty stack"); if (mCtx.groupStack.empty())
throw std::runtime_error("Skipping group with an empty stack");
// subtract what was already read/skipped // subtract what was already read/skipped
std::uint32_t skipSize = mCtx.groupStack.back().first.groupSize - mCtx.groupStack.back().second; std::uint32_t skipSize = mCtx.groupStack.back().first.groupSize - mCtx.groupStack.back().second;
@ -572,8 +576,8 @@ namespace ESM4
const CellGrid& Reader::currCellGrid() const const CellGrid& Reader::currCellGrid() const
{ {
// Maybe should throw an exception instead? if (!mCtx.cellGridValid)
assert(mCtx.cellGridValid && "Attempt to use an invalid cell grid"); throw std::runtime_error("Attempt to use an invalid cell grid");
return mCtx.currCellGrid; return mCtx.currCellGrid;
} }