Commits

Mikhail Korobov committed 1a8c4b7 Merge

Merge pull request #10 from IMDagger/master

Fix for checking key causes SEGFAULT after trying to load non-existing file

Comments (0)

Files changed (3)

 
         cdef ifstream stream
         stream.open(path, iostream.binary)
+        if stream.fail():
+            raise IOError("It's not possible to read file stream")
 
         res = self.dct.Read(<istream*> &stream)
 
 
         cdef ifstream stream
         stream.open(path, iostream.binary)
+        if stream.fail():
+            raise IOError("It's not possible to read file stream")
 
         try:
             res = self.dct.Read(<istream*> &stream)
 from libcpp.string cimport string
+from libcpp cimport bool
 
 cdef extern from "<istream>" namespace "std" nogil:
     cdef cppclass istream:
         istream(char* filename) except +
         istream(char* filename, int mode) except +
 
+        bool fail() except +
+
         void open(char* filename) except +
         void open(char* filename, int mode) except +
         void close() except +

tests/test_dawg.py

                 d.read(f)
                 assert 'Invalid' in e.args[0]
 
+    def test_no_segfaults_after_wrong_stream(self):
+        d = dawg.DAWG()
+        wrong_path = tempfile.mktemp()  # file doesn't exists
+
+        try:
+            d.load(wrong_path)
+        except:
+            pass                     # ignore error
+        assert 'random-key' not in d # there is possible segfault
+
     def test_build_errors(self):
         with pytest.raises(dawg.Error):
             data = [b'foo\x00bar', b'bar']