Skip to content

Commit

Permalink
Added check for stl corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter committed Mar 25, 2014
1 parent de5c4b4 commit 9cc3bd8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Loader::Loader(QObject* parent, const QString& filename)
: QThread(parent), filename(filename)
{
// Nothing to do here
}

void Loader::run()
Expand All @@ -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));
Expand Down
1 change: 1 addition & 0 deletions src/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Loader : public QThread
void loaded_file(QString filename);
void got_mesh(Mesh* m);
void error_ascii_stl();
void error_bad_stl();

private:
const QString filename;
Expand Down
10 changes: 10 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9cc3bd8

Please sign in to comment.