]> git.sur5r.net Git - fstl/commitdiff
Added check for stl corruption
authorMatt Keeter <matt.j.keeter@gmail.com>
Tue, 25 Mar 2014 22:53:07 +0000 (18:53 -0400)
committerMatt Keeter <matt.j.keeter@gmail.com>
Tue, 25 Mar 2014 22:53:07 +0000 (18:53 -0400)
src/loader.cpp
src/loader.h
src/window.cpp
src/window.h

index d7ce46a4a0e167ca560e18725c870e6f358f4f24..355f40c9c982ee621616a9105a6bf5fd5caf0c73 100644 (file)
@@ -4,6 +4,7 @@
 Loader::Loader(QObject* parent, const QString& filename)
     : QThread(parent), filename(filename)
 {
+    // Nothing to do here
 }
 
 void Loader::run()
@@ -16,6 +17,19 @@ void Loader::run()
             emit error_ascii_stl();
             return;
         }
+
+        // Skip the rest of the buffer
+        file.read(75);
+
+        // Assume we're on a little-endian system for simplicity
+        uint32_t tri_count;
+        file.read(reinterpret_cast<char*>(&tri_count), sizeof(tri_count));
+
+        if (file.size() != 84 + tri_count*50)
+        {
+            emit error_bad_stl();
+            return;
+        }
     }
 
     emit got_mesh(Mesh::load_stl(filename));
index 85e0c53a299a651c92b8a09e9ba395c3088d8aa1..1de1157929976f6286226fa297d86b66bb5e130d 100644 (file)
@@ -16,6 +16,7 @@ signals:
     void loaded_file(QString filename);
     void got_mesh(Mesh* m);
     void error_ascii_stl();
+    void error_bad_stl();
 
 private:
     const QString filename;
index 5f8ac9e2667198fdf21daaed05cca7393b85d735..2d809bfb0610c8a3924213148528d9693c377c22 100644 (file)
@@ -77,6 +77,14 @@ void Window::on_ascii_stl()
                           "Please convert to binary <code>.stl</code> and retry");
 }
 
+void Window::on_bad_stl()
+{
+    QMessageBox::critical(this, "Error",
+                          "<b>Error:</b><br>"
+                          "This <code>.stl</code> file is invalid or corrupted.<br>"
+                          "Please export it from the original source, verify, and retry.");
+}
+
 void Window::enable_open()
 {
     open_action->setEnabled(true);
@@ -101,6 +109,8 @@ bool Window::load_stl(const QString& filename)
             canvas, &Canvas::load_mesh);
     connect(loader, &Loader::error_ascii_stl,
               this, &Window::on_ascii_stl);
+    connect(loader, &Loader::error_bad_stl,
+              this, &Window::on_bad_stl);
 
     connect(loader, &Loader::finished,
             loader, &Loader::deleteLater);
index 84848226c465091361f6586f0d837cb798e0f00c..3ac5e9567401ed2cd534079a81c77b1389060b73 100644 (file)
@@ -16,6 +16,7 @@ public slots:
     void on_open();
     void on_about();
     void on_ascii_stl();
+    void on_bad_stl();
 
     void enable_open();
     void disable_open();