gcc and clang assignment compiler bug?
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Wed, 20 Oct 2021 01:04:31 UTC
Weird. I can't figure it out so I am putting it out there. I have sample
code and debug output that shows the issue. I am not sure why the
difference between the two since the buffer variable is a pointer to data
on the heap? Is this a compiler bug? One generates movabs and the other a mov
instruction.
/*
$ cc -g t.c
$ ./a.out
Segmentation fault (core dumped)
$ gdb a.out a.out.core
(gdb) bt
#0 0x00000000002018e6 in test2 () at t.c:50
#1 0x000000000020193e in main () at t.c:60
(gdb) list
46 void test2()
47 {
48 char *buffer= "Example string\r\n";
49
50 *buffer= 0;
51 buffer[0]= 'E';
52 }
(gdb) disassemble
Dump of assembler code for function test2:
0x00000000002018d0 <+0>: push %rbp
0x00000000002018d1 <+1>: mov %rsp,%rbp
0x00000000002018d4 <+4>: movabs $0x200500,%rax
0x00000000002018de <+14>: mov %rax,-0x8(%rbp)
0x00000000002018e2 <+18>: mov -0x8(%rbp),%rax
=> 0x00000000002018e6 <+22>: movb $0x0,(%rax)
0x00000000002018e9 <+25>: mov -0x8(%rbp),%rax
0x00000000002018ed <+29>: movb $0x45,(%rax)
0x00000000002018f0 <+32>: pop %rbp
0x00000000002018f1 <+33>: ret
(gdb) disassemble test1
Dump of assembler code for function test1:
0x0000000000201900 <+0>: push %rbp
0x0000000000201901 <+1>: mov %rsp,%rbp
0x0000000000201904 <+4>: mov 0x200520,%rax
0x000000000020190c <+12>: mov %rax,-0x20(%rbp)
0x0000000000201910 <+16>: mov 0x200528,%rax
0x0000000000201918 <+24>: mov %rax,-0x18(%rbp)
0x000000000020191c <+28>: mov 0x200530,%cl
0x0000000000201923 <+35>: mov %cl,-0x10(%rbp)
0x0000000000201926 <+38>: movb $0x0,-0x20(%rbp)
0x000000000020192a <+42>: movb $0x45,-0x20(%rbp)
0x000000000020192e <+46>: pop %rbp
0x000000000020192f <+47>: ret
0x000000000020192f <+47>: ret
*/
/* bad */
void test2()
{
char *buffer= "Example string\r\n";
*buffer= 0;
buffer[0]= 'E';
}
/* good */
void test1()
{
char buffer[]= {"Example string\r\n"};
*buffer= 0;
buffer[0]= 'E';
}
int main()
{
test1();
test2();
}
$ clang -v
FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git
llvmorg-11.0.1-0-g43ff75f2c3fe)
Target: x86_64-unknown-freebsd13.0
Thread model: posix
InstalledDir: /usr/bin
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc10/gcc/x86_64-portbld-freebsd13.0/10.3.0/lto-wrapper
Target: x86_64-portbld-freebsd13.0
Configured with: /wrkdirs/usr/ports/lang/gcc10/work/gcc-10.3.0/configure
--enable-multilib --with-build-config=bootstrap-debug --disable-nls
--enable-gnu-indirect-function --enable-plugin
--libdir=/usr/local/lib/gcc10 --libexecdir=/usr/local/libexec/gcc10
--program-suffix=10 --with-as=/usr/local/bin/as --with-gmp=/usr/local
--with-gxx-include-dir=/usr/local/lib/gcc10/include/c++/
--with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection'
--with-system-zlib --without-zstd --enable-languages=c,c++,objc,fortran
--prefix=/usr/local --localstatedir=/var --mandir=/usr/local/man
--infodir=/usr/local/share/info/gcc10 --build=x86_64-portbld-freebsd13.0
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.3.0 (FreeBSD Ports Collection)
$ uname -a
FreeBSD tabitha.markjolesen.com 13.0-RELEASE-p3 FreeBSD 13.0-RELEASE-p3 #0:
Tue Jun 29 19:46:20 UTC 2021
root@amd64-builder.daemonology.net:/usr/obj/usr/src/amd64.amd64/sys/GENERIC
amd64