https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99847
Bug ID: 99847 Summary: Optimization breaks alignment on CPU32 Product: gcc Version: 10.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: m.frohiky at gmail dot com Target Milestone: --- For CPU32 architecture optimizes two byte accesses into a single word access, without any regard that the original array may be unaligned. So this code: void ntoh(const uint8_t *idata, uint16_t *odata) { *odata = ((uint16_t)idata[0] << 8) | idata[1]; } Compiled with -Os or -O2 produces: move.l 4(%sp),%a1 move.l 8(%sp),%a0 move.w (%a1),(%a0) rts And if idata (address in register a1) is not aligned, the CPU will crash. The compiler I'm using is m68k-linux-gnu-gcc (g++ has the same problem) which comes from Debian repository. The exact version is "(Debian 10.2.1-6) 10.2.1 20210110" Even if it's relying on exception handler to handle the unaligned data it makes no sense because it's sooo much slower.