Hey Guys,
I've detected a strange behavior in the TI ARM C Compiler (version 5.1.4).
It addresses the expression simplification, when compiling with optimization level 0 (-O0).
Here's the code snippet:
static unsigned char* CDECL AllocatorAdd(unsigned char* pbyData, RTS_UI32 ui32Size, RTS_RESULT *pResult) { unsigned char* pbyCopiedPos = NULL; unsigned int uiAlignment; unsigned int uiDiv; ... /* check size */ if(pbyCopiedPos + ui32Size > s_pbyAllocatorEndPosition || pbyCopiedPos + ui32Size <= pbyCopiedPos) { RTS_SETRESULT(pResult, ERR_NOMEMORY); return NULL; } ...
The issue occurs in the size check. Because ui32Size can lead to an overflow, the second line in the if statement checks for this overflow. Actually, it's optimized away for some reason...
Here's the disassembly:
It seems, that the second check has been replaced by a check to equal zero.
However, if the check gets the following parameters, it fails, even the C code is correct:
pbyCopiedPos: 0x6003 00A0
s_pbyAllocatorEndPosition: 0x6004 FB60
ui32Size: 0xFFFF FFE8
The line with the statement "RTS_SETRESULT(pResult, ERR_NOMEMORY);" is NOT reached, although it should be.
What the hell is going on here?
Kind regards,
Michael