------- Comment #5 from edwintorok at gmail dot com 2009-04-13 06:56 ------- (In reply to comment #4) > (In reply to comment #3) > > But converting from short to int for the argument to printf should behave > > as if > > a short value was converted to int, i.e. the int value should be in range > > -32768 to 32767, right? > > Usually but not here. Since you compiled with -fstrict-overflow (implicitly > via > -O2) the compiler can assume your short variables will have proper short > values. As an optimisation the compiler could decide to store short variables > in wider int variables and to perform short arithmetic using int arithmetic. > Since you promised not to cause signed overflow those int variables would > always be the proper sign-extension of the corresponding short variables. > > But your test program does cause signed overflow, so this optimisation changes > behaviour. It's still not the compiler's fault. If you deliberately cause > signed overflow, do not compile with -fstrict-overflow. >
Understood. Then this is not wrong code, just a missing warning. (In reply to comment #0) > There are 2 bugs here: > - no warning is given with -Wstrict-overflow=5, although -fstrict-overflow > changes the behaviour of the code This is still a bug: "It warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur." > - from is of type short, so when sign extended to an int, it should only take > values allowed for a short: -32768 to 32767, not all values allowed for an int > (like 2253491) This is not a bug. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39736