From 208a51acfe407a47c8d74768d3e8a2e05915eeab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCller?= Date: Mon, 6 Jun 2016 16:59:16 +0200 Subject: [PATCH] Catch some possible seg faults when opening invalid xtc files. --- pygmx/xtcio.pyx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pygmx/xtcio.pyx b/pygmx/xtcio.pyx index ff22aa7..8bf334b 100644 --- a/pygmx/xtcio.pyx +++ b/pygmx/xtcio.pyx @@ -67,6 +67,9 @@ cdef array get_xtc_index(t_fileio *fio): return cache[:-1] +class XTCError(Exception): pass + + cdef class XTCReader: cdef: t_fileio *fio @@ -158,6 +161,11 @@ cdef class XTCReader: real time, prec rvec *x + if not os.path.exists(filename): + raise OSError('File not found: {}'.format(filename)) + if filename.decode().split('.')[-1] != 'xtc': + raise XTCError('File is not of xtc type: {}'.format(filename)) + self.fio = open_xtc(filename, b'r') read_first_xtc(self.fio, &self.natoms, &step, &time, box, &x, &prec, &_bOK)