[previous] 2948 [next] Suspicious: Result of cast is only representable in a two's complement implementation.
Conversion to signed

Dataflow analysis has identified a code construct which is suspicious. The code may be entirely safe but further investigation is advisable.

The value of an expression is being cast to a signed integer type. Examination of the immediate context suggests that the value could sometimes only be representable in this particular type in two's-complement representation.

In practice, two's complement representation is almost universal in modern computers, but other binary representations do exist.

Message 2948 will be generated when

Notice that on a 16 bit architecture:
  1. "32767" is a constant of type int
  2. "32768" is a constant of type long
  3. "-32768" is an expression of type long
  4. "-32767 - 1" is an expression of type int - BUT its value is not guaranteed to be -32768.

Example:


/*PRQA S 282,1278,2017,2983,2986,3120,3199,3203,3204,3227,3408,3602 ++*/
/***************************************************/
/* OPTIONS: -s int=16                              */
/***************************************************/
void foo(int n)
{
   signed int  sir;
   signed long slr = -32768L;
   int         i;

   for (i = 0; i < n; ++i)
   {
      ++slr;
   }

   sir = (signed int)slr;                  /* 2948 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents