浏览代码

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.
Max Vollmer 5 年之前
父节点
当前提交
84e060a816
共有 2 个文件被更改,包括 5 次插入5 次删除
  1. 1 1
      code/glTF2/glTF2Asset.h
  2. 4 4
      code/glTF2/glTF2Asset.inl

+ 1 - 1
code/glTF2/glTF2Asset.h

@@ -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);
 

+ 4 - 4
code/glTF2/glTF2Asset.inl

@@ -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)