Skip to content

-Waddress-of-packed-member misses some formations of const&s #113503

@gburgessiv

Description

@gburgessiv

The following code sample produces no -Waddress-of-packed-member diagnostics on Clang 19:

struct Packed {
    int foo;
} __attribute__((packed));

const Packed *get_packed_struct();
void use_const_int_ref(const int &);

void foo() {
    use_const_int_ref(get_packed_struct()->foo);
    const Packed *p = get_packed_struct();
    use_const_int_ref(p->foo);
}

Clang warns about this for pointers: https://godbolt.org/z/nMdEGcc4s which have the same alignment requirements as references.

A reduced version of the 'real-world' code motivating this bug looked something like:

struct Packed {
    int foo;
} __attribute__((packed));

const Packed *get_packed_struct();
std::optional<int> foo() {
  const Packed *p = get_packed_struct();
  // std::optional's ctor takes this as a `const int&`, which requires alignof(int).
  // If this function didn't have an implicit conversion to optional, this would work
  // fine, since Clang emits IR assuming 1-byte alignment for this load.
  return p->foo; 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerduplicateResolved as duplicatefalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions