Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect integer overflow

I am working with a large C library where some array indices are computed using int. I need to find a way to trap integer overflows at runtime in such way as to narrow to problematic line of code. Libc manual states:

FPE_INTOVF_TRAP Integer overflow (impossible in a C program unless you enable overflow trapping in a hardware-specific fashion).

however gcc option -ffpe-trap suggests that those only apply to FP numbers?
So how I do enable integer overflow trap? My system is Xeon/Core2, gcc-4.x, Linux 2.6

I have looked through similar questions but they all boil to modifying the code. I need to know however which code is problematic in the first place.
If Xeons can't trap overflows, which processors can? I have access to non-emt64 machines as well.

I have found a tool designed for llvm meanwhile: http://embed.cs.utah.edu/ioc/ There doesn't seem to be however an equivalent for gcc/icc?

like image 601
Anycorn Avatar asked Apr 17 '12 23:04

Anycorn


Video Answer


2 Answers

Ok, I may have to answer my own question.

I found gcc has -ftrapv option, a quick test does confirm that at least on my system overflow is trapped. I will post more detailed info as I learn more since it seems very useful tool.

like image 76
Anycorn Avatar answered Oct 19 '22 23:10

Anycorn


Unsigned integer arithmetic does not overflow, of course.

With signed integer arithmetic, overflow leads to undefined behaviour; anything could happen. And optimizers are getting aggressive about optimizing stuff that overflows. So, your best bet is to avoid the overflow, rather than trapping it when it happens. Consider using the CERT 'Secure Integer Library' (the URL referenced there seems to have gone AWOL/404; I'm not sure what's happened yet) or Google's 'Safe Integer Operation' library.

If you must trap overflow, you are going to need to specify which platform you are interested in (O/S including version, compiler including version), because the answer will be very platform specific.

like image 2
Jonathan Leffler Avatar answered Oct 19 '22 21:10

Jonathan Leffler