clinuxposixinteger-overflowsigfpe

Is it possible to catch integer overflow exception in C (POSIX/Linux) via SIGFPE FPE_INTOVF?


I feel like this is a stupid question, but I have found zero information about the topic (not here nor anywhere), so here's the question:

Context (what you probably already know): SIGFPE exceptions and si_code field

In POSIX/Linux we have a particular kind of signal called SIGFPE (that although its name, it's used both for floating-point and integer arithmetic errors). When we register a signal handler for SIGFPE using sigaction(), our handler also receives a field called si_code that explains why the particular SIGFPE exception has been raised. One of the possible si_code values is, for instance, FPE_INTDIV, and you can quickly test it by dividing an int by 0.

The problem: what about FPE_INTOVF?

My question is about a particular si_code, that is FPE_INTOVF. This code is described as Value signalling integer overflow in case of SIGFPE signal.

The problem is that I have never encountered an example of this signal being raised, nor I found a way to make the OS raise this kind of signal: when I do an integer overflow in C, no SIGFPE exception is thrown. But the FPE_INTOVF value for SIGFPE is a kind of hint that it's maybe possible to make the host throw in case of integer overflows (like it already does for integer division by zero).

The question:

Is FPE_INTOVF unusable? Is it just a place-holder for a type of signal that no POSIX-compliant OS has ever implemented? Or is it possible to instruct the OS to throw this kind of signal in case of integer overflow?

I'm really interested in catching FPE_INTOVF signals from a C program using sigaction().


P.S.: I recognize that unsigned integer overflow is not technically an error in C (it doesn't exist at all in C, since all unsigned integer arithmetic has "wrapping" behavior), but signed integer overflow is indeed undefined behavior, so I expect that FPE_INTOVF handles the latter.


Solution

  • TL;DR:

    Exceptions of type SIGFPE FPE_INTOVF appear to be thrown only when the underlying hardware has support for them. For instance, on Linux, these are the archs that may throw FPE_INTOVF: alpha, ia64, m68k, mips, parisc, s390, and superh.

    Not an answer to my question, but worth noting:

    My question was about FPE_INTOVF in particular, but if one just wants to check for integer overflow in general, it's worth noting that there are alternative ways to do so: compiler-specific functions like __builtin_sadd_overflow() (read more here) or compiler-specific flags like -ftrapv on GCC. Also see this and this related questions on SO.

    Credits

    I'd like to thank users KamilCuk, Steve Summit and Nate Eldredge for their contributions to this answer through their comments.


    P.S.: in case someone else wants to add other information about FPE_INTOVF, they may post another answer to this question, or they may write a comment to this very answer, and I will edit the answer to add the additional information.