public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "goran dot steen at enea dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/40397]  New: Variable values calculated differently depending on storage class
Date: Wed, 10 Jun 2009 11:11:00 -0000	[thread overview]
Message-ID: <bug-40397-17819@http.gcc.gnu.org/bugzilla/> (raw)

/************* 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.


-- 
           Summary: Variable values calculated differently depending on
                    storage class
           Product: gcc
           Version: 4.2.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: goran dot steen at enea dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40397


             reply	other threads:[~2009-06-10 11:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-10 11:11 goran dot steen at enea dot com [this message]
2009-06-10 12:43 ` [Bug c/40397] " goran dot steen at enea dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-40397-17819@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).