Skip to content

Commit

Permalink
dep/reshadefx: Avoid snprintf() when writing float constants
Browse files Browse the repository at this point in the history
Locale-specific, causes breakage on some systems.
  • Loading branch information
stenzek committed Jul 29, 2024
1 parent e0911d7 commit 2d2bc93
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions dep/reshadefx/src/effect_codegen_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <cstdio> // snprintf
#include <cassert>
#include <algorithm> // std::find_if, std::max
#include <iomanip>
#include <locale>
#include <sstream>
#include <unordered_set>

using namespace reshadefx;
Expand Down Expand Up @@ -360,9 +363,12 @@ class codegen_glsl final : public codegen
s += std::signbit(data.as_float[i]) ? "1.0/0.0/*inf*/" : "-1.0/0.0/*-inf*/";
break;
}
char temp[64]; // Will be null-terminated by snprintf
std::snprintf(temp, sizeof(temp), "%1.8e", data.as_float[i]);
s += temp;
{
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::fixed << data.as_float[i];
s += ss.str();
}
break;
default:
assert(false);
Expand Down
12 changes: 9 additions & 3 deletions dep/reshadefx/src/effect_codegen_hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <cassert>
#include <cstring> // stricmp
#include <algorithm> // std::find_if, std::max
#include <iomanip>
#include <locale>
#include <sstream>

using namespace reshadefx;

Expand Down Expand Up @@ -339,9 +342,12 @@ class codegen_hlsl final : public codegen
s += std::signbit(data.as_float[i]) ? "1.#INF" : "-1.#INF";
break;
}
char temp[64]; // Will be null-terminated by snprintf
std::snprintf(temp, sizeof(temp), "%1.8e", data.as_float[i]);
s += temp;
{
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::fixed << data.as_float[i];
s += ss.str();
}
break;
default:
assert(false);
Expand Down

0 comments on commit 2d2bc93

Please sign in to comment.