Change: ExtractData throws exception instead of returning false if data is invalid.

Explanation: The return value of ExtractData is never checked anywhere in code. However if it returns false, outData remains uninitialized. All code using ExtractData assumes outData is initialized and proceeds to using it. I haven't encountered a real-life case where this goes wrong - but the simple fact that it can go wrong is a red flag. Instead of relying on every bit of code checking the return value and handling this properly, I think it makes much more sense to have ExtractData throw an exception. It obviously is an exceptional situation, and throwing makes sure that no code that doesn't explicitly handle such a scenario continues running and potentially causing harm.
pull/3068/head
Max Vollmer 2020-03-11 09:40:42 +00:00
parent 7e23773776
commit 84e060a816
2 changed files with 5 additions and 5 deletions

View File

@ -398,7 +398,7 @@ namespace glTF2
inline uint8_t* GetPointer();
template<class T>
bool ExtractData(T*& outData);
void ExtractData(T*& outData);
void WriteData(size_t count, const void* src_buffer, size_t src_stride);

View File

@ -637,10 +637,12 @@ namespace {
}
template<class T>
bool Accessor::ExtractData(T*& outData)
void Accessor::ExtractData(T*& outData)
{
uint8_t* data = GetPointer();
if (!data) return false;
if (!data) {
throw DeadlyImportError("GLTF: data is NULL");
}
const size_t elemSize = GetElementSize();
const size_t totalSize = elemSize * count;
@ -661,8 +663,6 @@ bool Accessor::ExtractData(T*& outData)
memcpy(outData + i, data + i*stride, elemSize);
}
}
return true;
}
inline void Accessor::WriteData(size_t count, const void* src_buffer, size_t src_stride)