1

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 :)

Adam Horvath
  • 1,249
  • 1
  • 10
  • 25
Sergiu
  • 21
  • 4

2 Answers2

2

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?

Erika
  • 416
  • 5
  • 14
  • You are right, this is an embedded system and uses a 16bit int. So it is likely the cast is affected by it :). I have to double check but my guess is you are correct. – Sergiu Nov 05 '14 at 08:25
1

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 );
n0p
  • 3,399
  • 2
  • 29
  • 50
Missaka Wijekoon
  • 883
  • 7
  • 10