Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is signed overflow still undefined behaviour in gcc when -fwrapv is used?

Background

By default signed overflow is undefined behaviour.

My understanding of gcc (based on https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html and What does -fwrapv do?) was that using -fwrapv made gcc treat signed overflow as being well defined behaviour.

However, comments on another question seem to say that signed overflow is still undefined behaviour even when this flag is on.

Question

Is signed overflow well defined in gcc with -fwrapv? If not, then what is the purpose of -fwrapv?

like image 523
Peter de Rivaz Avatar asked Mar 01 '19 22:03

Peter de Rivaz


People also ask

Is signed overflow undefined?

In contrast, the C standard says that signed integer overflow leads to undefined behavior where a program can do anything, including dumping core or overrunning a buffer. The misbehavior can even precede the overflow. Such an overflow can occur during addition, subtraction, multiplication, division, and left shift.

Is unsigned overflow undefined behavior?

-fsanitize=unsigned-integer-overflow : Unsigned integer overflow, where the result of an unsigned integer computation cannot be represented in its type. Unlike signed integer overflow, this is not undefined behavior, but it is often unintentional.

Why is signed integer overflow undefined Behaviour?

— the sign bit has the value −(2N − 1) (one's complement). Nowadays, all processors use two's complement representation, but signed arithmetic overflow remains undefined and compiler makers want it to remain undefined because they use this undefinedness to help with optimization.

What happens when a signed int overflows?

"Signed integer overflow" means that you tried to store a value that's outside the range of values that the type can represent, and the result of that operation is undefined (in this particular case, your program halts with an error).


1 Answers

Given the GCC documentation says:

-fwrapv

This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation.

I'd characterize that as an implementation-specific extension that provides clearly defined behavior for what otherwise would be undefined behavior in standard C - if and only if the underlying hardware behaves that way.

Pedantically, I'd say it's still undefined behavior by the C standard, but you're instructing the compiler to act in a specific, non-portable but predictable manner.

like image 154
Andrew Henle Avatar answered Nov 15 '22 05:11

Andrew Henle