Fix OBJ import on macOS 13.3 to 14.0 when the UI language uses a decimal comma (#17851)

On macOS 13.3 to 14.0, Apple's `strtod` reads the process locale and ignores the thread locale that `CNumericLocalesSetter` sets. With a UI language that uses a decimal comma, every OBJ vertex with a decimal point was rejected and the import failed. `objparser.cpp` now reads numbers with the new `strtod_decimal_point()` in `LocalesUtils`, which parses with fast_float and doesn't depend on the locale.
This commit is contained in:
Martin Stachon authored and GitHub committed 2026-11-25 10:14:39 -06:00
1 parent 188d11c5e9
commit 808dd7dff8
6 files changed
+136 -39

No files matched your search

+37 -37
View File
@@ -46,14 +46,14 @@ static bool obj_parseline(const char *line, ObjData &data)
return false;
EATWS();
char *endptr = 0;
double u = strtod(line, &endptr);
double u = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double v = 0;
if (*line != 0) {
v = strtod(line, &endptr);
v = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
@@ -61,7 +61,7 @@ static bool obj_parseline(const char *line, ObjData &data)
}
// The optional w is accepted but not stored: only u and v are used.
if (*line != 0) {
strtod(line, &endptr);
Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
@@ -82,17 +82,17 @@ static bool obj_parseline(const char *line, ObjData &data)
return false;
EATWS();
char *endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
@@ -112,19 +112,19 @@ static bool obj_parseline(const char *line, ObjData &data)
return false;
EATWS();
char *endptr = 0;
double u = strtod(line, &endptr);
double u = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
double v = strtod(line, &endptr);
double v = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
double w = 0;
if (*line != 0) {
w = strtod(line, &endptr);
w = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
@@ -144,17 +144,17 @@ static bool obj_parseline(const char *line, ObjData &data)
return false;
EATWS();
char *endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t'))
return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
@@ -164,24 +164,24 @@ static bool obj_parseline(const char *line, ObjData &data)
if (!data.has_vertex_color) {
data.has_vertex_color = true;
}
color_x = strtod(line, &endptr);
color_x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
color_y = strtod(line, &endptr);
color_y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
color_z = strtod(line, &endptr);
color_z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0))
return false;
line = endptr;
EATWS();
color_w = 1.0;//default define alpha = 1.0
if (*line != 0) {
color_w = strtod(line, &endptr);
color_w = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
@@ -418,7 +418,7 @@ static bool mtl_skip_numeric_token(const char *&line)
if (*begin == 0)
return false;
char *endptr = 0;
strtod(begin, &endptr);
Slic3r::strtod_decimal_point(begin, &endptr);
if (endptr == begin || (!mtl_is_space(*endptr) && *endptr != 0))
return false;
line = mtl_skip_ws(endptr);
@@ -515,14 +515,14 @@ static bool mtl_parseline(const char *line, MtlData &data)
if (cur_char == 's') {
EATWS();
char * endptr = 0;
double ns = strtod(line, &endptr);
double ns = Slic3r::strtod_decimal_point(line, &endptr);
if (data.new_mtl_unmap.find(cur_mtl_name) != data.new_mtl_unmap.end()) {
data.new_mtl_unmap[cur_mtl_name]->Ns = (float) ns;
}
} else if (cur_char == 'i') {
EATWS();
char * endptr = 0;
double ni = strtod(line, &endptr);
double ni = Slic3r::strtod_decimal_point(line, &endptr);
if (data.new_mtl_unmap.find(cur_mtl_name) != data.new_mtl_unmap.end()) {
data.new_mtl_unmap[cur_mtl_name]->Ni = (float) ni;
}
@@ -534,15 +534,15 @@ static bool mtl_parseline(const char *line, MtlData &data)
if (cur_char == 'a') {
EATWS();
char * endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
@@ -554,15 +554,15 @@ static bool mtl_parseline(const char *line, MtlData &data)
} else if (cur_char == 'd') {
EATWS();
char * endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
@@ -574,15 +574,15 @@ static bool mtl_parseline(const char *line, MtlData &data)
} else if (cur_char == 's') {
EATWS();
char * endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
@@ -594,15 +594,15 @@ static bool mtl_parseline(const char *line, MtlData &data)
} else if (cur_char == 'e') {
EATWS();
char * endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
@@ -619,7 +619,7 @@ static bool mtl_parseline(const char *line, MtlData &data)
return false;
EATWS();
char * endptr = 0;
double illum = strtod(line, &endptr);
double illum = Slic3r::strtod_decimal_point(line, &endptr);
if (data.new_mtl_unmap.find(cur_mtl_name) != data.new_mtl_unmap.end()) {
data.new_mtl_unmap[cur_mtl_name]->illum = (float) illum;
}
@@ -628,7 +628,7 @@ static bool mtl_parseline(const char *line, MtlData &data)
case 'd': {
EATWS();
char * endptr = 0;
double d = strtod(line, &endptr);
double d = Slic3r::strtod_decimal_point(line, &endptr);
if (data.new_mtl_unmap.find(cur_mtl_name) != data.new_mtl_unmap.end()) {
data.new_mtl_unmap[cur_mtl_name]->d = (float) d;
}
@@ -639,7 +639,7 @@ static bool mtl_parseline(const char *line, MtlData &data)
if (cur_char == 'r') {
EATWS();
char * endptr = 0;
double tr = strtod(line, &endptr);
double tr = Slic3r::strtod_decimal_point(line, &endptr);
if (data.new_mtl_unmap.find(cur_mtl_name) != data.new_mtl_unmap.end()) {
data.new_mtl_unmap[cur_mtl_name]->Tr = (float) tr;
}
@@ -647,15 +647,15 @@ static bool mtl_parseline(const char *line, MtlData &data)
} else if (cur_char == 'f') {
EATWS();
char * endptr = 0;
double x = strtod(line, &endptr);
double x = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double y = strtod(line, &endptr);
double y = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t')) return false;
line = endptr;
EATWS();
double z = strtod(line, &endptr);
double z = Slic3r::strtod_decimal_point(line, &endptr);
if (endptr == 0 || (*endptr != ' ' && *endptr != '\t' && *endptr != 0)) return false;
line = endptr;
EATWS();
+13
View File
@@ -105,6 +105,19 @@ double atof_decimal_point(std::string_view str)
return string_to_double_decimal_point(str.substr(i));
}
double strtod_decimal_point(const char *str, char **endptr)
{
const char *begin = str;
while (*begin == ' ' || (*begin >= '\t' && *begin <= '\r'))
++begin;
if (*begin == '+' && begin[1] != '-')
++begin;
size_t parsed = 0;
const double out = string_to_double_decimal_point(begin, &parsed);
*endptr = const_cast<char*>(parsed == 0 ? str : begin + parsed);
return out;
}
std::string float_to_string_decimal_point(double value, int precision/* = -1*/)
{
// Every branch prints the same digits in the classic locale as the stream at the end, which takes over when a branch
+3
View File
@@ -51,6 +51,9 @@ double string_to_double_decimal_point(const std::string_view str, size_t* pos =
// Parses like atof in the C locale, skipping leading whitespace and a '+',
// without the C runtime's per-call locale lookup.
double atof_decimal_point(std::string_view str);
// Parses a decimal number like strtod in the C locale, and sets *endptr past it, or to str when there is none.
// strtod on macOS 13.3 to 14.0 reads the process locale, so a CNumericLocalesSetter does not affect it.
double strtod_decimal_point(const char *str, char **endptr);
} // namespace Slic3r
+32
View File
@@ -12,6 +12,7 @@
#include <boost/lexical_cast.hpp>
#include <boost/lexical_cast/bad_lexical_cast.hpp>
#include <cstddef>
#include <string>
using namespace Slic3r;
@@ -89,6 +90,37 @@ TEST_CASE("atof_decimal_point parses what atof parses in the C locale", "[Locale
CHECK_THAT(atof_decimal_point(text), Catch::Matchers::WithinAbs(value, 1e-12));
}
TEST_CASE("strtod_decimal_point parses what strtod parses in the C locale", "[LocalesUtils]")
{
const auto [text, value, length] = GENERATE(table<const char*, double, size_t>({
{ "5", 5., 1 },
{ " 12.5 7", 12.5, 6 },
{ "\t+3", 3., 3 },
{ "-1.25", -1.25, 5 },
{ "1e2", 100., 3 },
{ ".5", 0.5, 2 },
{ "0,5", 0., 1 },
{ "+-5", 0., 0 },
{ "", 0., 0 },
{ "abc", 0., 0 },
}));
INFO(text);
char *end = nullptr;
CHECK_THAT(strtod_decimal_point(text, &end), Catch::Matchers::WithinAbs(value, 1e-12));
CHECK(size_t(end - text) == length);
}
TEST_CASE("strtod_decimal_point reads a decimal point when the process locale uses a comma", "[LocalesUtils]")
{
ProcessCommaNumericLocale comma;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
const char *text = "0.5 1";
char *end = nullptr;
CHECK_THAT(strtod_decimal_point(text, &end), Catch::Matchers::WithinAbs(0.5, 1e-12));
CHECK(end == text + 3);
}
TEST_CASE("Nozzle diameter strings read back in a locale whose decimal separator is a comma", "[LocalesUtils]")
{
CommaNumericLocale comma;
+36 -2
View File
@@ -4,6 +4,8 @@
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/catch_test_macros.hpp>
#include "libslic3r/BoundingBox.hpp"
#include "libslic3r/Color.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/Format/OBJ.hpp"
@@ -27,13 +29,13 @@ struct LoadedObj
};
// Loads an OBJ made of the given lines, with a material library that defines material "a".
LoadedObj load_textured_obj(const std::string &body)
LoadedObj load_textured_obj(const std::string &body, const std::string &materials = "newmtl a\nKd 1 0 0\n")
{
ScopedTemporaryFile obj(".obj");
ScopedTemporaryFile mtl(".mtl");
{
boost::nowide::ofstream out(mtl.string());
out << "newmtl a\nKd 1 0 0\n";
out << materials;
}
{
boost::nowide::ofstream out(obj.string());
@@ -172,3 +174,35 @@ TEST_CASE("Texture coordinates follow the corners of a mesh that is flipped on l
for (int corner = 0; corner < 3; ++corner)
check_uv_is_vt(loaded.info.uvs[face][corner], its.indices[face][corner] + 1);
}
TEST_CASE("Numbers with a decimal point load when the process locale uses a decimal comma", "[OBJ][Regression]")
{
// The GUI sets the process locale from its language. On macOS 13.3 to 14.0, strtod read that locale
// inside the parser's CNumericLocalesSetter, so it dropped every vertex and the faces failed to load.
ProcessCommaNumericLocale comma;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
const LoadedObj loaded = load_textured_obj("v 0.5 0.5 0.5 1 0.5 0.25\nv 10.5 0.5 0.5 1 0.5 0.25\n"
"v 0.5 10.5 0.5 1 0.5 0.25\nv 0.5 0.5 10.5 1 0.5 0.25\n"
"vt 0.25 0.5\n"
"usemtl a\n"
"f 1/1 3/1 2/1\nf 1/1 2/1 4/1\nf 1/1 4/1 3/1\nf 2/1 3/1 4/1\n",
"newmtl a\nKd 0.5 0.25 0.125\n");
REQUIRE(loaded.ok);
REQUIRE(loaded.mesh.facets_count() == 4);
const BoundingBoxf3 box = loaded.mesh.bounding_box();
for (int axis = 0; axis < 3; ++axis) {
CHECK_THAT(box.min[axis], WithinAbs(0.5, 1e-6));
CHECK_THAT(box.max[axis], WithinAbs(10.5, 1e-6));
}
REQUIRE(loaded.info.vertex_colors.size() == 4);
CHECK_THAT(loaded.info.vertex_colors.front()[1], WithinAbs(0.5, 1e-6));
CHECK_THAT(loaded.info.vertex_colors.front()[2], WithinAbs(0.25, 1e-6));
REQUIRE(loaded.info.face_colors.size() == 4);
CHECK_THAT(loaded.info.face_colors.front()[0], WithinAbs(0.5, 1e-6));
CHECK_THAT(loaded.info.face_colors.front()[2], WithinAbs(0.125, 1e-6));
REQUIRE(loaded.info.uvs.size() == 4);
CHECK_THAT(loaded.info.uvs.front()[0].x(), WithinAbs(0.25, 1e-6));
CHECK_THAT(loaded.info.uvs.front()[0].y(), WithinAbs(0.5, 1e-6));
}
+15
View File
@@ -266,4 +266,19 @@ struct CommaNumericLocale
#endif
};
// Switches the process-wide numeric locale, which the GUI sets from its language, to one whose
// decimal separator is a comma, when the system has one installed. Restores it when destroyed.
struct ProcessCommaNumericLocale
{
std::string previous { std::setlocale(LC_NUMERIC, nullptr) };
bool apply()
{
for (const char* name : { "de_DE.UTF-8", "de_DE.utf8", "de_DE", "de-DE", "German_Germany.1252" })
if (std::setlocale(LC_NUMERIC, name) != nullptr)
return true;
return false;
}
~ProcessCommaNumericLocale() { std::setlocale(LC_NUMERIC, previous.c_str()); }
};
#endif // SLIC3R_TEST_UTILS