zhouguiyun-uestc / ParallelFlowDirections

MIT License
3 stars 3 forks source link

segfault #21

Open r-barnes opened 4 years ago

r-barnes commented 4 years ago

The code segfaults when running, as follows:

rick@beast:/z/ParallelFlowDirections$ ./ParallelFlowDir 
[beast:344949] *** Process received signal ***
[beast:344949] Signal: Segmentation fault (11)
[beast:344949] Signal code: Address not mapped (1)
[beast:344949] Failing at address: (nil)
[beast:344949] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x153c0)[0x7f1e551313c0]
[beast:344949] [ 1] /lib/x86_64-linux-gnu/libc.so.6(+0x186b7e)[0x7f1e550b0b7e]
[beast:344949] [ 2] ./ParallelFlowDir(+0x14b25)[0x55908cc88b25]
[beast:344949] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7f1e54f510b3]
[beast:344949] [ 4] ./ParallelFlowDir(+0x1618e)[0x55908cc8a18e]
[beast:344949] *** End of error message ***
Segmentation fault (core dumped)
rick@beast:/z/ParallelFlowDirections$ mpirun -n 4 ./ParallelFlowDir 
[beast:344985] *** Process received signal ***
[beast:344985] Signal: Segmentation fault (11)
[beast:344985] Signal code: Address not mapped (1)
[beast:344985] Failing at address: (nil)
[beast:344985] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x153c0)[0x7f6b4220a3c0]
[beast:344985] [ 1] /lib/x86_64-linux-gnu/libc.so.6(+0x186b7e)[0x7f6b42189b7e]
[beast:344985] [ 2] ./ParallelFlowDir(+0x14b25)[0x55b7f3c71b25]
[beast:344985] [ 3] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7f6b4202a0b3]
[beast:344985] [ 4] ./ParallelFlowDir(+0x1618e)[0x55b7f3c7318e]
[beast:344985] *** End of error message ***
--------------------------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 0 on node beast exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

This is because you don't perform any kind of input sanitization or CLI argument validity checking.

I've personally found CLI11 to be useful for this (example). If you decide to use it, you could include it as a submodule (see here). Otherwise, you should check the number of arguments and ensure they're what you expect potentially like this, but I think CLI11 is really the better option.

In any case, the program shouldn't segfault.

zhouguiyun-uestc commented 4 years ago

Thanks. Done!

r-barnes commented 3 years ago

Unfortunately, the problem isn't fixed for me yet.

These lines:

    if ( argc < 4 ) {
        std::cout << "Too few arguments." << std::endl;
        return -1;
    }

don't provide helpful feedback if the user enters no arguments.

And eliminating those lines means a segfault here

if ( strcmp( method, "parallel" ) == 0 ) {

if the user enters no arguments.

I think the code would be significantly simplified if you made a test.cpp which runs the test suite and limited main.cpp to only taking input datasets. That eliminates the if ( strcmp( method, "parallel" ) == 0 ) { if-clause which means you can pass all argument checking off to the CLI11 library.

zhouguiyun-uestc commented 3 years ago

Done! I have provide helpful feedback in the program. If the user enters no arguments., the argc is less than 4, and the program will exit. I think those lines do not mean a segfault here if ( strcmp( method, "parallel" ) == 0 ). The input parameters of the two modes are different, I think it is necessary to know which mode it is. Therefore, i kept this line of code if ( strcmp( method, "parallel" ) == 0 )