Bug 87413 - strlen accepted in array declaration
Summary: strlen accepted in array declaration
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: constexpr strlen
  Show dependency treegraph
 
Reported: 2018-09-24 14:48 UTC by Bernd Edlinger
Modified: 2023-12-30 20:48 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-09-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bernd Edlinger 2018-09-24 14:48:24 UTC
$ 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);
                 ^
Comment 1 Bernd Edlinger 2018-09-24 14:58:32 UTC
so current trunk accepts the variant test1.c with __builtin_strlen
both C and C++:

$ gcc -Wall -S test1.c
$ gcc -x c++ -Wall -S test1.c

Don't know it that is valid.
Comment 2 Jonathan Wakely 2018-09-24 15:17:58 UTC
It's a GNU extension in C++, so we can decide whether it's valid or not.
Comment 3 Martin Sebor 2018-09-24 15:50:07 UTC
As Jonathan already mentioned, treating __builtin_strlen("string") (among other string function calls) as a constant integer expression is a useful extension accepted by several C++ compilers, including Clang and ICC 18 (see also pr80265).

G++ accepts the test case in C++ 11 mode and later, but rejects it with an error in C++ 98 mode.  Since C++ requires array bounds to be constant expressions and calls to functions are not such expressions, it might be worth issuing a pedantic warning on the strlen case in C++ 11 mode and later.
Comment 4 Martin Sebor 2018-09-24 16:16:15 UTC
Let me confirm this for the missing pedantic warning.