Skip to content

Commit

Permalink
Merge pull request #757 from zeux/gltf-fp
Browse files Browse the repository at this point in the history
gltfpack: Sanitize floats before writing them to JSON
  • Loading branch information
zeux authored Aug 30, 2024
2 parents 3dc0706 + 888cfe9 commit 64c904d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gltf/json.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This file is part of gltfpack; see gltfpack.h for version/license details
#include "gltfpack.h"

#include <float.h>
#include <math.h>
#include <stdio.h>

void comma(std::string& s)
Expand All @@ -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;
}

Expand Down

0 comments on commit 64c904d

Please sign in to comment.