public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112409] New: Structure is not initialized as expected
@ 2023-11-06 16:08 freddy77 at gmail dot com
  2023-11-06 16:11 ` [Bug c/112409] " sjames at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: freddy77 at gmail dot com @ 2023-11-06 16:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112409

            Bug ID: 112409
           Summary: Structure is not initialized as expected
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: freddy77 at gmail dot com
  Target Milestone: ---

I was writing a small network utility till I found a weird behaviour computing
TCP checksums. After some debugging I found that the error disappeared with
either -O1 or -O0. So I reduced the program, trying using multiple GCC versions
and it kept happening.

The final program (stripped down) is:


typedef long unsigned int size_t;

typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;

#define ntohs __builtin_bswap16
#define htonl __builtin_bswap32

static inline unsigned
reduce_cksum(unsigned sum)
{
        return (sum >> 16) + (sum & 0xffffu);
}

static unsigned
cksum(const void *pkt, size_t len, unsigned int start)
{
        const uint16_t *data = (const uint16_t *) pkt;
        unsigned sum = start;

        for (; len >= 2; len -= 2)
                sum += *data++;
        if (len)
                sum += ntohs(*((const uint8_t *)data) << 8);
        sum = reduce_cksum(sum);
        sum = reduce_cksum(sum);
        return sum;
}

static unsigned
tcpdump_flow_new(uint32_t saddr, uint32_t daddr)
{
        struct {
                uint32_t saddr, daddr;
                uint8_t zero, proto;
        } pseudo = { htonl(saddr), htonl(daddr), 0, 6 };
        return cksum(&pseudo, 10, 0);
}

int main(void)
{
        unsigned res = tcpdump_flow_new(0x01020304, 0xa1b2c3d4);
        return res;
}


Using -O2 option and Compiler Explorer (but I tried multiple versions locally)
produced this:


main:
  xor edx, edx
  lea rax, [rsp-12]
  lea rsi, [rsp-2]
.L2:
  movzx ecx, WORD PTR [rax]
  add rax, 2
  add edx, ecx
  cmp rax, rsi
  jne .L2
  mov eax, edx
  movzx edx, dx
  shr eax, 16
  add edx, eax
  mov eax, edx
  movzx edx, dx
  shr eax, 16
  add eax, edx
  ret


It's easy to spot that the constants disappeared from the code and program uses
not initialized memory.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-06 17:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-06 16:08 [Bug c/112409] New: Structure is not initialized as expected freddy77 at gmail dot com
2023-11-06 16:11 ` [Bug c/112409] " sjames at gcc dot gnu.org
2023-11-06 16:13 ` pinskia at gcc dot gnu.org
2023-11-06 16:13 ` redi at gcc dot gnu.org
2023-11-06 16:15 ` redi at gcc dot gnu.org
2023-11-06 16:16 ` freddy77 at gmail dot com
2023-11-06 16:18 ` redi at gcc dot gnu.org
2023-11-06 17:37 ` freddy77 at gmail dot com

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