Problem
I am compiling a program using the Optimizing C Compiler (icc) 2.1.4. However, when I run this program, under certain circumstances, comparisons of signed short integers are executing incorrectly.
An example program which demonstrates this problem follows:
/* * icc -b elf -o bug bug.c */
int always_zero(short *ignored_parameter)
{
return 0;
}signed short ret_minus1_short(void) { short ind; short index_id = -1; short dummy;
while(always_zero(&ind)) { /* (0) -- returns 0 */
if (always_zero(&dummy)) { /* not executed */
index_id = ind; /* not executed */
} /* not executed */
}
if (index_id != -1) /* (0) -- index_id == -1 */
(void)always_zero(&dummy); /* not executed */ return index_id; /* returns -1 as a short */
}main()
{
if (ret_minus1_short() > 0)
printf("Error! It claims that (short)-1 > 0!\n");
else
printf("It correctly claims that (short)-1 <= 0!\n");
} Cause
In certain contrived circumstances, the SCO Optimizing C Compiler 2.1.4 may generate incorrect code for the handling of short integers being returned from a function call.
In the above example, the function ret_minus1_short() returns a
signed short integer in a full 32-bit register without sign extending
this value. Its caller, main(), then proceeds to access this signed
short integer by simply reading the full 32 bits of this register.
Thus, a negative signed short value of '-1' returned by
ret_minus1_short() is interpreted as a positive value of '65535'
by its caller, main().For users familiar with assembler code, this can be verified by running the commands:
icc -c -belf bug.c
dis bug.o > dis.outputThe file "dis.output" will contain output similar to this:
disassembly for bug.oret_minus1_short()
...
8048485: bb ff ff 00 00 movl $0xffff,%ebx
...
80484d4: 8b c3 movl %ebx,%eax
...main()
...
80484f9: e8 82 ff ff ff call 0xffffff82 <8048480>
80484fe: 83 f8 00 cmpl $0x0,%eax
8048501: 7e 15 jle 0x15 <8048518>
...You will see that in ret_minus1_short(), 0xffff is initially stored in the 32-bit register %ebx and the return value is moved from %ebx to %eax without any sign-extending.
Subsequently, in the main() routine, the value returned in %eax is
then compared to 0 and, if it is greater, a jump instruction is
performed. In the case of -1 (0xffff) being returned, the comparison
of this value held in %eax to the value '0' will determine that
'65535' *is* greater than 0, which is incorrect. Solution
To work around this problem, change the types of values returned by functions from "short" to "int" or "long".
This problem has been reported to SCO Engineering.