From 9cc3bd82e832f5d49911048d6dc053e177922abf Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Tue, 25 Mar 2014 18:53:07 -0400 Subject: [PATCH] Added check for stl corruption --- src/loader.cpp | 14 ++++++++++++++ src/loader.h | 1 + src/window.cpp | 10 ++++++++++ src/window.h | 1 + 4 files changed, 26 insertions(+) diff --git a/src/loader.cpp b/src/loader.cpp index d7ce46a..355f40c 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -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(&tri_count), sizeof(tri_count)); + + if (file.size() != 84 + tri_count*50) + { + emit error_bad_stl(); + return; + } } emit got_mesh(Mesh::load_stl(filename)); diff --git a/src/loader.h b/src/loader.h index 85e0c53..1de1157 100644 --- a/src/loader.h +++ b/src/loader.h @@ -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; diff --git a/src/window.cpp b/src/window.cpp index 5f8ac9e..2d809bf 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -77,6 +77,14 @@ void Window::on_ascii_stl() "Please convert to binary .stl and retry"); } +void Window::on_bad_stl() +{ + QMessageBox::critical(this, "Error", + "Error:
" + "This .stl file is invalid or corrupted.
" + "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); diff --git a/src/window.h b/src/window.h index 8484822..3ac5e95 100644 --- a/src/window.h +++ b/src/window.h @@ -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(); -- 2.39.5