From 658a64dc4ef9d8c4617b7d3f1fc5b694055ad942 Mon Sep 17 00:00:00 2001
From: Stefan Bienert <stefan.bienert@unibas.ch>
Date: Tue, 27 Feb 2018 11:05:21 +0100
Subject: [PATCH] Fixed star parser, prevent him from going into an infinite
 loop if an early line starts with a "d"

---
 modules/io/src/mol/star_parser.cc          | 2 ++
 modules/io/tests/test_star_parser.cc       | 7 +++++++
 modules/io/tests/testfiles/broken_data.cif | 1 +
 3 files changed, 10 insertions(+)
 create mode 100644 modules/io/tests/testfiles/broken_data.cif

diff --git a/modules/io/src/mol/star_parser.cc b/modules/io/src/mol/star_parser.cc
index 80043259b..4f559b195 100644
--- a/modules/io/src/mol/star_parser.cc
+++ b/modules/io/src/mol/star_parser.cc
@@ -550,6 +550,8 @@ void StarParser::Parse()
       case 'd':
         if (tline.length()>=5 && StringRef("data_", 5)==tline.substr(0, 5)) {
           this->ParseData();
+        } else {
+          throw IOException("Missing 'data_' control structure");
         }
         break;
       case 'g':
diff --git a/modules/io/tests/test_star_parser.cc b/modules/io/tests/test_star_parser.cc
index 6ff217b74..a8ed75451 100644
--- a/modules/io/tests/test_star_parser.cc
+++ b/modules/io/tests/test_star_parser.cc
@@ -389,6 +389,13 @@ BOOST_AUTO_TEST_CASE(star_items_as_row)
   BOOST_TEST_MESSAGE("  done.");
 }
 
+BOOST_AUTO_TEST_CASE(star_broken_data)
+{
+  std::ifstream s("testfiles/broken_data.cif");
+  LoopTestParser star_p(s);
+  BOOST_CHECK_THROW(star_p.Parse(), IOException);
+}
+
 BOOST_AUTO_TEST_CASE(star_missing_data)
 {
   std::ifstream s("testfiles/missing_data.cif");
diff --git a/modules/io/tests/testfiles/broken_data.cif b/modules/io/tests/testfiles/broken_data.cif
new file mode 100644
index 000000000..2ae242255
--- /dev/null
+++ b/modules/io/tests/testfiles/broken_data.cif
@@ -0,0 +1 @@
+d lines starting with a 'd' kicked the parser into an infinite loop.
-- 
GitLab