First Last Prev Next    No search results available      Search page      Enter new bug
Bug#: 40394
Product:  
Component:  
Status: RESOLVED
Resolution: INVALID
Assigned To: Not yet assigned to anyone <unassigned@gcc.gnu.org>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: Göran Steen <goran.steen@enea.com>
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 40394 depends on: Show dependency tree
Show dependency graph
Bug 40394 blocks:

Additional Comments:






View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: Opened: 2009-06-10 10:38
/************* CODE EXAMPLE ***************/ 
#include <stdio.h> 
#include <string.h> 

typedef unsigned int U32; 
typedef unsigned short U16; 
typedef unsigned char U8; 

#ifndef __GNUC__ 
#define __attribute__(a) 
#endif /* __GNUC__ */ 
#define APP6DR_PACK_STRUCT __attribute__((packed)) 

#define APP6DR_INLINE_FUNC(func) \ 
   __inline__ func __attribute__((always_inline)); \ 
   extern __inline__ __attribute__((gnu_inline)) func 

struct app6drIpHdr 
{ 
   unsigned int ip_v :4; 
   unsigned int ip_hl :4; 
   U8 ip_tos; 
   U16 ip_len; 
   U16 ip_id; 
   U16 ip_off; 
   U8 ip_ttl; 
   U8 ip_p; 
   U16 ip_sum; 
   struct 
   { 
      U32 ip_sa; 
      U32 ip_da; 
   } ip_addr_r; 
} APP6DR_PACK_STRUCT; 
typedef struct app6drIpHdr App6drIpHdr; 

#define APP6DR_CALC_CSUM(hdr_p,len,cs) App6dr_calcCs16((const char*)(hdr_p),
(len), &(cs)) 
APP6DR_INLINE_FUNC(void App6dr_calcCs16(const char* hdr_p, 
                                        U32 len, 
                                        volatile U16* cs_p)) 
{ 
   U32 i, sum; 
   for(i=0, sum=0; i<(len)/2; i++) 
   { 
      U32 val = ((U32*)hdr_p)[i]; 
      sum += ((val&0xffff) + (val>>16)); 
   } 
   if ((len) & 1) sum += ((U16*)hdr_p)[(len)-1]; 
   while(sum>>16) sum = (sum&0xffff)+(sum>>16); 
   *cs_p = (U16)(~sum); 
} 

#define APP6DR_CALC_IPV4_CSUM(ipv4_p) \ 
   (ipv4_p)->ip_sum = 0x0000; \ 
   APP6DR_CALC_CSUM((char*)(ipv4_p),sizeof(App6drIpHdr)/2,(ipv4_p)->ip_sum) 


#define DOIT() \ 
  memset((void*)&iph, 0xfe, sizeof(App6drIpHdr)); \ 
  iph.ip_v = 4; \ 
  iph.ip_hl = 5; \ 
  iph.ip_tos = 0; \ 
  iph.ip_len = 0; \ 
  iph.ip_id = 0; \ 
  iph.ip_off = 0; \ 
  iph.ip_ttl = 64; /* XXX */ \ 
  iph.ip_p = 50; /* ESP */ \ 
  iph.ip_addr_r.ip_sa = (U32) sa; \ 
  iph.ip_addr_r.ip_da = (U32) da; \ 
  APP6DR_CALC_IPV4_CSUM(&iph);\ 
  fprintf(stderr, "checksum = 0x%04x\n", iph.ip_sum); 


void testchksum2(U32 sa, U32 da) 
{ 
  static App6drIpHdr iph; 
  DOIT(); 
} 

void testchksum1(U32 sa, U32 da) 
{ 
  App6drIpHdr iph; 
  DOIT(); 
} 

int main() 
{ 
testchksum1(0x0a0a0a14, 0x0a0a0a01); 
testchksum2(0x0a0a0a14, 0x0a0a0a01); 
return 0; 
} 
/************ END OF CODE EXAMPLE **************/ 


Compiled with 
gcc -Os -Wall -pedantic main.c -o runme

Output:
checksum = 0xa89c
checksum = 0xa542

The only difference between outputs are that the variables made calculations on
have different storage classes, ie automatic and static.

------- Comment #1 From Göran Steen 2009-06-10 12:42 -------
*** Bug 40395 has been marked as a duplicate of this bug. ***

------- Comment #2 From Göran Steen 2009-06-10 12:42 -------
*** Bug 40396 has been marked as a duplicate of this bug. ***

------- Comment #3 From Göran Steen 2009-06-10 12:42 -------
*** Bug 40397 has been marked as a duplicate of this bug. ***

------- Comment #4 From Göran Steen 2009-06-10 12:43 -------
*** Bug 40398 has been marked as a duplicate of this bug. ***

------- Comment #5 From Göran Steen 2009-06-10 12:43 -------
*** Bug 40399 has been marked as a duplicate of this bug. ***

------- Comment #6 From Göran Steen 2009-06-10 12:43 -------
*** Bug 40400 has been marked as a duplicate of this bug. ***

------- Comment #7 From Richard Guenther 2009-06-10 14:02 -------
You are violating C aliasing rules.  Use -fno-strict-aliasing.

------- Comment #8 From Göran Steen 2009-06-12 06:20 -------
Since -pedantic was given as compiler option, gcc should have warned about
violating C aliasing rules.

------- Comment #9 From Andrew Pinski 2009-06-12 06:57 -------
Not all aliasing violations can be detected by the compiler :).

First Last Prev Next    No search results available      Search page      Enter new bug