fuzziqersoftware / phosg

C++ helpers for some common tasks
18 stars 17 forks source link

_exit when execv* fails #14

Closed Kristine1975 closed 2 years ago

Kristine1975 commented 2 years ago

Currently, when the constructor of Subprocess successfully forks the process, but fails to execute the new application (e.g. because it doesn't exist), it continues to execute the copy of the parent process, which will fail the moment the Subprocess object is destroyed: then, waiting on the child process (ourselves) will fail, leading to an exception being thrown. Starting with C++11, by default, destructors must not throw exceptions under penalty ofstd::terminate getting called. This leads to the child process crashing with, depending on the OS, a crashlog and an error dialog.

In resource_dasm this happens when picttoppm isn't installed, with the slightly confusing result that resource_dasm both crashes (the child process) and continues (the parent process).

To fix this, SubProcess exits as quickly and quietly as possible when it fails to execute the new application, without even doing any cleanup (by using _exit instead of exit).

Plus using ! to check whether an integer is zero gives me a headache 😉.

fuzziqersoftware commented 2 years ago

Thanks for finding this. Obviously I never tested resource_dasm without picttoppm installed; I only tested cases where it's installed and fails.

One note - if errno happens to be a multiple of 256, it might appear to the parent process that the child succeeded since only the low 8 bits are available from waitpid() and such. It looks like on most OSes, errno values don't go up that high, but perhaps we should _exit() with a constant instead just to be safe.