diff --git a/gltf/json.cpp b/gltf/json.cpp index 92062d0703..c46ef45a00 100644 --- a/gltf/json.cpp +++ b/gltf/json.cpp @@ -1,6 +1,8 @@ // This file is part of gltfpack; see gltfpack.h for version/license details #include "gltfpack.h" +#include +#include #include void comma(std::string& s) @@ -20,8 +22,12 @@ void append(std::string& s, size_t v) void append(std::string& s, float v) { + // sanitize +-inf to +-FLT_MAX and NaN to FLT_MAX + // it would be more consistent to use null for NaN but that makes JSON invalid, and 0 makes it hard to distinguish from valid values + float sv = fabsf(v) < FLT_MAX ? v : (v < 0 ? -FLT_MAX : FLT_MAX); + char buf[64]; - snprintf(buf, sizeof(buf), "%.9g", v); + snprintf(buf, sizeof(buf), "%.9g", sv); s += buf; }