Added runtime type size check to ComponentWrapper to avoid corruption when types don't match

This commit is contained in:
2016-01-17 00:53:31 +01:00
parent d8a24bbfde
commit 6385a98eca
2 changed files with 9 additions and 3 deletions
+1
View File
@@ -1,5 +1,6 @@
#include <memory>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
+8 -3
View File
@@ -5,7 +5,7 @@
#include "Entity.h"
#include "ComponentInfo.h"
#include "Util/Any.h"
Minecraft hard drilling
struct ComponentWrapper
{
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
@@ -26,8 +26,13 @@ struct ComponentWrapper
template <typename T>
T& Field(std::string name)
{
unsigned int offset = Info.Fields.at(name).Offset;
return *reinterpret_cast<T*>(&Data[offset]);
const ComponentInfo::Field_t& field = Info.Fields.at(name);
if (sizeof(T) > field.Stride) {
std::stringstream message;
message << "Type size of \"" << typeid(T).name() << "\" doesn't match size of component field \"" << Info.Name << "." << name << "\"!";
throw new std::runtime_error(message.str().c_str());
}
return *reinterpret_cast<T*>(&Data[field.Offset]);
}
template <typename T>