<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW " title="NEW --- - Clang miscompiles assignments to packed struct members" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D23599&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=ykVRlO6FZN7RHNvt7BLOkMImfmePqh5Z9_ATIldYRQE&s=q5opSN9AfdtOOUcW2jrah2gKja-cAsvsx07iEfDUb_c&e=">23599</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang miscompiles assignments to packed struct members
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vonosmas@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu, nlewycky@google.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>$ cat tmp/a.cc
#include <stdio.h>
#include <stdint.h>

#define ETH_ALEN 6

struct ether_addr
{
  uint8_t ether_addr_octet[ETH_ALEN];
} __attribute__ ((__packed__));

struct ether_header
{
  uint8_t ether_dhost[ETH_ALEN];
  uint8_t ether_shost[ETH_ALEN];
} __attribute__ ((__packed__));

void Print(const ether_header &eth) {
  for (int i = 0; i < 6; i++)
    printf("%d ", eth.ether_dhost[i]);
  printf("\n");
  for (int i = 0; i < 6; i++)
    printf("%d ", eth.ether_shost[i]);
  printf("\n");
}

void FooBarBaz(const ether_addr& ether_src, const ether_addr& ether_dst) {
  ether_header eth{};
  *((ether_addr*)eth.ether_shost) = ether_src;
  *((ether_addr*)eth.ether_dhost) = ether_dst;
  Print(eth);
}

int main() {
  FooBarBaz({{1,2,3,4,5,6}}, {{7,8,9,10,11,12}});
  return 0;
}

$ ./bin/clang++ -O2 tmp/a.cc -std=c++11 && ./a.out
7 8 9 10 11 12 
0 0 0 0 0 0

$ ./bin/clang++ -O0 tmp/a.cc -std=c++11 && ./a.out
7 8 9 10 11 12 
1 2 3 4 5 6

I haven't yet figured out which optimization pass is responsible for that. Note
that if I swap the assignments of ether_src and ether_dst, the program works as
expected.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>