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

            Bug ID: 129277
           Summary: wrong code: u"..." initializer of a big-endian
                    scalar_storage_order array is stored in native order
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

Tested with gcc version 17.0.0 20261203 (experimental), r17-7143-g592f605117e,
x86_64-pc-linux-gnu.

int printf(const char *, ...);
#pragma scalar_storage_order big-endian
struct S { unsigned short u[2]; };
#pragma scalar_storage_order default
struct S g = { u"E" };
int main(void) {
  printf("%x\n", g.u[0]);
}

$ gcc -O0 t.c && ./a.out
4500
$ gcc -O2 t.c && ./a.out
4500

Expected 45. No flag fixes it (static initializer). `struct S g = { { 0x45 }
};` prints 45.
Bisected to r6-4623-gee45a32dae2 ("Merge of the scalar-storage-order branch.");
its parent has no scalar_storage_order, so the fault is as old as the feature.
output_constant (varasm.cc), case STRING_CST, writes the raw string bytes with
assemble_string and ignores `reverse`; only INTEGER_CST and REAL_CST are
byte-swapped. So the u"E" element is emitted as bytes 45 00 instead of 00 45.
L"..." and U"..." fail the same way.

---
Note(:
- r17-7143: -O0 4500, -O2 4500. All releases 7.5..16.2: 4500 at -O0 and -O2.
- Sanitizers: gcc ASan+UBSan -O0/-O2 clean. clang 24 ignores the pragma (prints
45); ASan+UBSan, MSan clean. TySan reports "READ of size 2 ... accesses an
existing object of type S" also for the brace-initialized control (no string):
TySan false positive on array members, not this test.
- Local-object form (BATCH5 ยง23 second test) is a -O0 vs -O1 difference on
8.5..14.3; since r16-1752 (Botcazou, "Fix misoptimization of CONSTRUCTOR with
reverse SSO") -O1 is wrong too. More forms on request.

Reply via email to