From 6326aab6778ecec2221dc63ea851468d0041f20f Mon Sep 17 00:00:00 2001 From: Don Bright Date: Mon, 17 Feb 2014 14:19:12 -0600 Subject: [PATCH 1/3] fix issue #235 --- src/import.cc | 17 ++++++++++++----- testdata/scad/bugs/empty-stl.scad | 6 ++++++ testdata/stl/empty.stl | 0 testdata/stl/empty2.stl | 3 +++ tests/CMakeLists.txt | 3 ++- .../regression/echotest/empty-stl-expected.echo | 1 + 6 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 testdata/scad/bugs/empty-stl.scad create mode 100644 testdata/stl/empty.stl create mode 100644 testdata/stl/empty2.stl create mode 100644 tests/regression/echotest/empty-stl-expected.echo diff --git a/src/import.cc b/src/import.cc index 3c2cce11..72efc21c 100644 --- a/src/import.cc +++ b/src/import.cc @@ -226,7 +226,6 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const std::string line; std::getline(f, line); while (!f.eof()) { - std::getline(f, line); boost::trim(line); if (boost::regex_search(line, ex_sfe)) { @@ -257,7 +256,7 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const } } } - else + else if (binary && !f.eof()) { f.ignore(80-5+4); while (1) { @@ -283,10 +282,12 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const else { file >> poly; file.close(); - p = new PolySet(); bool err = createPolySetFromPolyhedron(poly, *p); - if (err) delete p; + if (err) { + delete p; + p = NULL; + } } #else PRINT("WARNING: OFF import requires CGAL."); @@ -301,11 +302,17 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const dxf_tesselate(p, dd, 0, Vector2d(1,1), true, false, 0); dxf_border_to_ps(p, dd); } - else + else { PRINTB("ERROR: Unsupported file format while trying to import file '%s'", this->filename); } + + if (p && p->empty()) { + delete p; + p = NULL; + } + if (p) p->convexity = this->convexity; return p; } diff --git a/testdata/scad/bugs/empty-stl.scad b/testdata/scad/bugs/empty-stl.scad new file mode 100644 index 00000000..c3bfe9d3 --- /dev/null +++ b/testdata/scad/bugs/empty-stl.scad @@ -0,0 +1,6 @@ +// openscad should exit normally. +// it should not crash and/or freeze and/or gobble RAM +import("../../stl/empty.stl"); +import("../../stl/empty2.stl"); +echo("empty stl test ok"); + diff --git a/testdata/stl/empty.stl b/testdata/stl/empty.stl new file mode 100644 index 00000000..e69de29b diff --git a/testdata/stl/empty2.stl b/testdata/stl/empty2.stl new file mode 100644 index 00000000..a5b90276 --- /dev/null +++ b/testdata/stl/empty2.stl @@ -0,0 +1,3 @@ +solid OpenSCAD_Model +endsolid OpenSCAD_Model + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 32455322..d392e988 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -824,7 +824,8 @@ list(APPEND ECHO_FILES ${FUNCTION_FILES} ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/expression-shortcircuit-tests.scad ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/parent_module-tests.scad ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/children-tests.scad - ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/range-tests.scad) + ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/range-tests.scad + ${CMAKE_SOURCE_DIR}/../testdata/scad/bugs/empty-stl.scad) list(APPEND DUMPTEST_FILES ${FEATURES_FILES} ${EXAMPLE_FILES}) list(APPEND DUMPTEST_FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/escape-test.scad diff --git a/tests/regression/echotest/empty-stl-expected.echo b/tests/regression/echotest/empty-stl-expected.echo new file mode 100644 index 00000000..b446acbb --- /dev/null +++ b/tests/regression/echotest/empty-stl-expected.echo @@ -0,0 +1 @@ +ECHO: "empty stl test ok" From ccc782e39cc26e1bbd59c5640c68299f33dd1db2 Mon Sep 17 00:00:00 2001 From: Don Bright Date: Wed, 19 Feb 2014 05:26:19 -0600 Subject: [PATCH 2/3] fix failing tests per comments on github, make slightly more robust --- src/import.cc | 11 ++++------- tests/CMakeLists.txt | 4 +++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/import.cc b/src/import.cc index 72efc21c..e042db45 100644 --- a/src/import.cc +++ b/src/import.cc @@ -206,7 +206,7 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const bool binary = false; std::streampos file_size = f.tellg(); f.seekg(80); - if (!f.eof()) { + if (f.good() && !f.eof()) { uint32_t facenum = 0; f.read((char *)&facenum, sizeof(uint32_t)); #ifdef BOOST_BIG_ENDIAN @@ -220,7 +220,7 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const char data[5]; f.read(data, 5); - if (!binary && !f.eof() && !memcmp(data, "solid", 5)) { + if (!binary && !f.eof() && f.good() && !memcmp(data, "solid", 5)) { int i = 0; double vdata[3][3]; std::string line; @@ -256,7 +256,7 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const } } } - else if (binary && !f.eof()) + else if (binary && !f.eof() && f.good()) { f.ignore(80-5+4); while (1) { @@ -308,10 +308,7 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const } - if (p && p->empty()) { - delete p; - p = NULL; - } + if (!p) p = new PolySet(); if (p) p->convexity = this->convexity; return p; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c66e7f61..b66c95e8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1013,12 +1013,14 @@ add_cmdline_test(openscad-nonascii EXE ${OPENSCAD_BINPATH} ARGS -o # Variable override (-D arg) +# FIXME - this breaks on older cmake that is very common 'in the wild' on linux # Override simple variable +if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}" VERSION_GREATER 2.8.10) add_cmdline_test(openscad-override EXE ${OPENSCAD_BINPATH} ARGS -D a=3$ -o SUFFIX echo FILES ${CMAKE_SOURCE_DIR}/../testdata/scad/misc/override.scad) - +endif() # Image output parameters add_cmdline_test(openscad-imgsize EXE ${OPENSCAD_BINPATH} From c9df4c0d26306eacfe099080181bcd5cbc4de2cd Mon Sep 17 00:00:00 2001 From: Marius Kintel Date: Wed, 19 Feb 2014 23:33:31 -0500 Subject: [PATCH 3/3] Last #235 fix: Don't return empty PolySets, as they will be interpreted as 3D object later, causing a mixed 2D-3D error --- src/import.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/import.cc b/src/import.cc index e042db45..decd88a0 100644 --- a/src/import.cc +++ b/src/import.cc @@ -307,9 +307,6 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const PRINTB("ERROR: Unsupported file format while trying to import file '%s'", this->filename); } - - if (!p) p = new PolySet(); - if (p) p->convexity = this->convexity; return p; }