简体   繁体   中英

C Possible overflow on casting to void pointer

I'm working with a code analysis tool called Polyspace. I got a "possible overflow" notification on one code section and just can figure it out :)

Error : operation [conversion from unsigned int32 to unsigned int16] on scalar overflows (results is always strictly greater than MAX UINT16) conversion from unsigned int 32 to unsigned int 16 right: [956448 .. 972799]

The code is:

typedef unsigned char       T_UBYTE;
typedef unsigned short int  T_UWORD;
typedef unsigned long int   T_ULONG;

typedef  void __far * T_EEP_ADDRESS;
..

T_EEP_ADDRESS beeeblock_GetBlockPointer(T_UWORD luw_BATAddress)
{
   T_UWORD luw_BlockPointer;
   T_EEP_ADDRESS lpul_BATEntry;
..
   luw_BlockPointer =  ( READ_EEP_32(lpul_BATEntry) & 0xFFFFuL );
..
   return (T_EEP_ADDRESS)((0x00E9800UL)+ (T_ULONG)luw_BlockPointer ); 
}

The line causing the error is this:

return (T_EEP_ADDRESS)((0x00E9800UL)+ (T_ULONG)luw_BlockPointer );

Any help would be extremely welcome :)

It looks like the type T_EEP_ADDRESS is 16 bit, and ((0x00E9800UL)+ (T_ULONG)luw_BlockPointer ) is a 32 bit-result, so you're converting a large number into a smaller one and loosing information.

What system is this on? Do you know the pointer size, since T_EEP_ADDRESS is a pointer?

There are 2 potential issues:

The error might be in the line above:

luw_BlockPointer = ( READ_EEP_32(lpul_BATEntry) & 0xFFFFuL );
sizeof(0xFFFFuL) == 8 and sizeof(luw_BlockPointer) == 2

Try to take the T_ULONG cast off as it should allow you to add an unsigned short to an unsigned long without a cast:

return (T_EEP_ADDRESS)((0x00E9800UL)+ luw_BlockPointer );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM