Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should my library handle SIGSEGV on bad pointer input?

I'm writing a small library that takes a FILE * pointer as input.

If I immediately check this FILE * pointer and find it leads to a segfault, is it more correct to handle the signal, set errno, and exit gracefully; or to do nothing and use the caller's installed signal handler, if he has one?

The prevailing wisdom seems to be "libraries should never cause a crash." But my thinking is that, since this particular signal is certainly the caller's fault, then I shouldn't attempt to hide that information from him. He may have his own handler installed to react to the problem in his own way. The same information CAN be retrieved with errno, but the default disposition for SIGSEGV was set for a good reason, and passing the signal up respects this philosophy by either forcing the caller to be handle his errors, or by crashing and protecting him from further damage.

Would you agree with this analysis, or do you see some compelling reason to handle SIGSEGV in this situation?

like image 708
User123abc Avatar asked Jan 23 '12 19:01

User123abc


1 Answers

Taking over handlers is not library business, I'd say it's somewhat offensive of them unless explicitly asked for. To minimize crashes library may validate their input to some certain extent. Beyond that: garbage in — garbage out.

like image 175
Michael Krelin - hacker Avatar answered Oct 21 '22 16:10

Michael Krelin - hacker