3

I have upgraded my GCC compiler from 4.1.2 to 4.8.3, When i compile my source code with 4.8.3 version i am facing below issue.

Error: iteration 8u invokes undefined behavior [-Werror=aggressive-loop-optimizations]

{
uint index;
for ( index =0 ; index<BLOCK_SIZE; index++) 
ltoP->kdlllp= riv64[7-index]; 
}

Can any one please help me in resolving this issue?

Johnney
  • 127
  • 1
  • 9

1 Answers1

3

When index is 8; riv64[7-index] is riv64[-1u], which is undefined. The 8u in the message suggests the type of index is unsigned int, in which case 7-index will evaluate to a very large value that is outside the array bounds. (7-8u will be evaluated as 7u-8u, which is equivalent to -1u. Unsigned arithmetic wraps, so that produces a very large value, UINT_MAX.)

user2736738
  • 30,591
  • 5
  • 42
  • 56
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Eric,Thanks fro your reply.What should i need to do here to fix this? – Johnney Dec 27 '17 at 11:44
  • @NaveenKumar: From the code you have shown, we can see `riv64[7-index]` is wrong. Without seeing more code, we cannot see why it is used or what the fix should be. – Eric Postpischil Dec 27 '17 at 11:58
  • Hi Eric, As you mentioned above riv64 is array with index 8, snippet. void hdcp22UpdateConfigPseudoRandomNumberRiv( hdcp22PT hdcpP, guchar riv64[8],hdcp22T endianess) /*!endpublic*/ { if ( endianess == P22_BIG_ENDIAN ) memcpy(hdcpP->hdcp22ConfigP->pseudoRandomNumberRiv, riv64, 8); else { guint index; for ( index =0 ; indexhdcp22ConfigP->pseudoRandomNumberRiv[index] = riv64[7-index]; } } – Johnney Dec 27 '17 at 12:01
  • @NaveenKumar: That code appears to be reversing `riv64` to adjust for endianness. If so, the loop index should be limited by the size of `riv64`, not `AES_BLOCK_SIZE`. But that is still too small a portion of the code to judge well, and programs should not be debugged by means of Stack Overflow comments. – Eric Postpischil Dec 27 '17 at 12:14
  • I will check and confirm. – Johnney Dec 27 '17 at 12:33