https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67360
Bug ID: 67360 Summary: runtime crash for swprintf function compiled with gcc(4.8.2/4.9) and "-O" option under Ubuntu 4.8.2-19ubuntu1 Product: gcc Version: 4.8.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: zhijian at ca dot ibm.com Target Milestone: --- I write a simple source code P73246.c as #include <wchar.h> #include <stddef.h> /*--------------------------------------------------------------------*/ size_t n = 90; int main(void) { int ret, prec; wchar_t s[76]; if ((ret = swprintf(s,n,L"%.1a\n",-1.000e-27)) < 4) { } /*--------------------------------------------------------------------*/ return(0); } And compile and link it with gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) The compile command as /usr/bin/gcc -std=c99 -m64 -D__64BIT__=1 -O -mcpu=power8 -fabi-version=4 -std=gnu99 -c P73246.c Link command as /usr/bin/gcc -std=c99 -lm -o P73246 P73246.o Last I run the ./P73246 and a crash happened *** buffer overflow detected ***: ./P73246 terminated ======= Backtrace: ========= /lib/powerpc64le-linux-gnu/libc.so.6(+0x8123c)[0x3fff804a123c] /lib/powerpc64le-linux-gnu/libc.so.6(__fortify_fail+0x74)[0x3fff80555614] /lib/powerpc64le-linux-gnu/libc.so.6(__chk_fail+0x20)[0x3fff80553a30] /lib/powerpc64le-linux-gnu/libc.so.6(__vswprintf_chk+0x130)[0x3fff80557240] /lib/powerpc64le-linux-gnu/libc.so.6(__swprintf_chk+0x28)[0x3fff805570e8] ./P73246[0x10000724] /lib/powerpc64le-linux-gnu/libc.so.6(+0x24d00)[0x3fff80444d00] /lib/powerpc64le-linux-gnu/libc.so.6(__libc_start_main+0xc8)[0x3fff80444ef8] ======= Memory map: ======== 10000000-10010000 r-xp 00000000 00:1e 10809300 /gsa/tlbgsa-h1/00/zhijian/res/200230/P73246.scenario/P73246 10010000-10020000 rw-p 00000000 00:1e 10809300 /gsa/tlbgsa-h1/00/zhijian/res/200230/P73246.scenario/P73246 3fff80420000-3fff805d0000 r-xp 00000000 fc:02 15076932 /lib/powerpc64le-linux-gnu/libc-2.19.so 3fff805d0000-3fff805e0000 rw-p 001a0000 fc:02 15076932 /lib/powerpc64le-linux-gnu/libc-2.19.so 3fff805e0000-3fff80600000 r-xp 00000000 fc:02 15073346 /lib/powerpc64le-linux-gnu/libgcc_s.so.1 3fff80600000-3fff80610000 rw-p 00010000 fc:02 15073346 /lib/powerpc64le-linux-gnu/libgcc_s.so.1 3fff80610000-3fff806e0000 r-xp 00000000 fc:02 15076921 /lib/powerpc64le-linux-gnu/libm-2.19.so 3fff806e0000-3fff806f0000 rw-p 000c0000 fc:02 15076921 /lib/powerpc64le-linux-gnu/libm-2.19.so 3fff80700000-3fff80720000 rw-p 00000000 00:00 0 3fff80720000-3fff80740000 r-xp 00000000 00:00 0 [vdso] 3fff80740000-3fff80770000 r-xp 00000000 fc:02 15076942 /lib/powerpc64le-linux-gnu/ld-2.19.so 3fff80770000-3fff80780000 rw-p 00020000 fc:02 15076942 /lib/powerpc64le-linux-gnu/ld-2.19.so 3fffc3ad0000-3fffc3b00000 rw-p 00000000 00:00 0 [stack] ./test: line 41: 160167: Abort If I do not use option "-O" in the compile stage , There is no crash ,The problem look like to related to compile with option "-O" . I try to use the same source code and compile option in the Redhat linux(no matter gcc version 4.8 and 4.9), there is no problem.