diff --git a/components/esm3/variantimp.cpp b/components/esm3/variantimp.cpp index 31248556ec..e5337a4dbf 100644 --- a/components/esm3/variantimp.cpp +++ b/components/esm3/variantimp.cpp @@ -1,6 +1,7 @@ #include "variantimp.hpp" #include +#include #include #include @@ -9,6 +10,18 @@ namespace ESM { + namespace + { + template + T floatCast(float value) + { + constexpr float min = static_cast(std::numeric_limits::lowest()); + constexpr float max = static_cast(std::numeric_limits::max()); + if (std::isnan(value) || value < min || value > max) + return {}; + return static_cast(value); + } + } void readESMVariantValue(ESMReader& esm, Variant::Format format, VarType type, std::string& out) { @@ -60,9 +73,9 @@ namespace ESM if (std::isnan(value)) out = 0; else - out = static_cast(value); + out = floatCast(value); else if (type == VT_Long) - out = static_cast(value); + out = floatCast(value); else esm.fail("unsupported global variable integer type"); }