CGNSReader calls cgio_error_exit on error instead of throwing an exception
The issue
In some cases, when reading an invalid CGNS file, instead of returning or throwing an error, VTK exits my whole program (WTF).
This is due to VTK calling cgio_error_exit
when an error occurs.
What is the extent of the issue ? How to fix ?
The search for cgio_error_exit
should return 0 results.
Every occurrence of this function call needs to be replaced by an exception throw.
User story
I tried reading an empty .cgns
file.
What I got out of VTK (using the pyvista wrapper)
$ touch test.cgns
$ python -c 'import pyvista; pyvista.read("test.cgns"); print("OK")'
cgio_file_open:ADF 15: FREAD error.
$ # OK is never printed, but no exception was thrown ? -> That's because VTK called exit() on our program :(
What I expected out of VTK
$ touch test.cgns
$ python -c 'import pyvista; pyvista.read("test.cgns"); print("OK")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
pyvista.read("test.cgns")
File "/bla/bla/python3.10/site-packages/pyvista/something.py", line 116, in read
blablabla Invalid CGNS file blablabla
How to circumvent the bug
One can override cgio_error_exit
. The question then becomes: how to raise the exception in C and catch it in python ?
$ cat AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.cxx
#include <stdexcept>
#include <stdio.h>
extern "C" {
void vtkcgns_cgio_error_exit(const char *msg) {
printf("VTK is great: %s\n", msg);
// throw std::runtime_error(msg); // python can't catch C++ exceptions
}
}
$ g++ -shared -fPIC AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.cxx -o pain.so
$ LD_PRELOAD=$(pwd)/pain.so python -c 'import pyvista; pyvista.read("test.cgns"); print("OK")'
VTK sucks: cgio_file_openVTK sucks: cgio_get_root_idMismatch in number of children and child IDs read
2022-10-06 17:23:24.466 ( 9.289s) [ 9EBE740] vtkCGNSReader.cxx:4668 ERR| vtkCGNSReader (0
ERROR:root:Failed to parse cgns file: [...]
2022-10-06 17:23:24.493 ( 9.315s) [ 9EBE740] vtkExecutive.cxx:740 ERR| vtkCompositeDataPipeline (0x55b7bee5d7e0): Algorithm vtkCGNSReader (0x55b7beeba870) returned failure for request: vtkInformation
[...]
OK
Edited by Arthur Woimbée