On 11/19/23 21:33, Alexandre Oliva wrote:
Unlike C, C++ doesn't mark enums shortened by -fshort-enums as packed.
This makes for undesirable warning differences between C and C++,
e.g. c-c++-common/analyzer/null-deref-pr108251-smp_fetch_ssl_fc_has_early*.c
triggers a warning about a type cast from a pointer to enum that, when
packed, might not be sufficiently aligned.
I think the warning is wrong here. The example given when it was added
in r9-5005-gda77eace90fd99 was
struct pair_t
{
char c;
int i;
} __attribute__ ((packed));
extern struct pair_t p;
int *addr = &p.i;
in this case, we're assigning a pointer to unaligned int to a normal
pointer to int, and that's what the warning is for; conversion from a
misaligned member. In the analyzer testcase, we have a cast from an
enum pointer that we don't know what it points to, and even if it did
point to the obj_type member of struct connection, that wouldn't be a
problem because it's at offset 0.
Also, -fshort-enums has nothing to do with structure packing, it just
affects the underlying type of the enum. We don't warn about conversion
from pointer to char to pointer to struct with char member, and this is
the exact same situation.
And unlike a struct with __attribute__ ((packed)),
enum A { a=1000 };
gets 16-bit alignment with -fshort-enums.
So it seems to me that setting TYPE_PACKED from -fshort-enums is wrong.
Or that the warning's use of TYPE_PACKED is wrong (e.g. it should look
for a COMPONENT_REF of DECL_PACKED instead). Or both.
Either way, I'm opposed to changing things so that the C++ front-end
starts emitting the warning.
Jason