EDIT: modified void * to be uint8_t * values. Problem still persists.
EDIT: The problem was a simple variable overflowing and had nothing to do with integer promotions.
I tackled a bug in that simplified piece of code. The types are the same as in the origin source code.
unsigned int entrySize; // entrySize is 288
int startIndex, endIndex; // both are 24536838
uint8_t *pStartAddr; // valid initialized pointer (0x34f1e40)
/*Mystery begins...*/
uint8_t *curr_addr = pStartAddr + entrySize * startIndex;
while (curr_addr <= startAddr + entrySize * endIndex)
{
externFunc(curr_addr);
curr_addr+=entrySize;
}
In quick glance this code seems obvious enough, excluding the weird type selection.
However in one of our crashes it seems that curr_addr
gets an invalid pointer.
My hunch was that there is a problem with entrySize * startIndex
since their multiplication sets on the 32th bit, and the having a startIndex
and endIndex
as signed types might confuse the compiler of the desired value to use.
After changing their types the unsinged long, the problem was solved. but I am unable to figure out what exactly is wrong.
I am working on 64bit machine, x86_64 CPU, gcc (GCC) 4.8.5 20150623 and linux red hat distribution (version 4.8.5-28)
I assumed that the problems start happening when the calculation above sets on the 32th bit of entrySize * startIndex
. However, it still worked when I used the first startIndex
value that had the 32th bit turned on. It also displayed
My questions are:
- is the value result of
int
*unsigned int
considered as signed or unsigned? what is the rank of the result type? multiplication can probably overflow to 8 bytes types and I assume the compiler prevents losing precision, right? startAddr
is avoid*
. it is then added to whatever value&type calculated in first question. isvoid*
consideredsigned
orunsigned
? my hunch is of course anunsigned value
, but I can't back it up.- What Integer promotions takes place in startAddr + <<result>>.
- Can our while statement never halt (in practical time)? If the right side of the inequality is a signed a number (in width of at least 8 bytes), is the left side (curr_addr) will be promoted as signed number as well, result in the endless loop?
- Step by Step explanation would be most welcome :)
I read those whatever contains in those links, and still remained clueless: