https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87413
Bug ID: 87413
Summary: strlen accepted in array declaration
Product: gcc
Version: 8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bernd.edlinger at hotmail dot de
Target Milestone: ---
$ cat test.c
#include <string.h>
char x[strlen("test")];
int test ()
{
return sizeof(x);
}
$ gcc -Wall -S test.c
test.c:2:6: error: variably modified 'x' at file scope
2 | char x[strlen("test")];
| ^
which is okay.
But:
$ gcc -S -x c++ -Wall test.c
$ cat test.s
.file "test.c"
.text
.globl x
.bss
.type x, @object
.size x, 4
x:
.zero 4
.text
.globl _Z4testv
.type _Z4testv, @function
_Z4testv:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $4, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
This was previously rejected:
$ gcc-4.8 -x c++ -S test.c
test.c:2:22: error: array bound is not an integer constant before ‘]’ token
char x[strlen("test")];
^
test.c: In function ‘int test()’:
test.c:5:17: error: ‘x’ was not declared in this scope
return sizeof(x);
^
I think this should be invalid.
Interestingly, this modified example was accepted as C and rejected as C++
by gcc-4.8
$ cat test1.c
char x[__builtin_strlen("test")];
int test ()
{
return sizeof(x);
}
$ gcc-4.8 -S test.c
$ gcc-4.8 -x c++ -S test.c
test.c:2:22: error: array bound is not an integer constant before ‘]’ token
char x[strlen("test")];
^
test.c: In function ‘int test()’:
test.c:5:17: error: ‘x’ was not declared in this scope
return sizeof(x);
^