llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang allows direct list initialization of flexible array member with `{}`

Open Rush10233 opened this issue 2 years ago • 4 comments

The following code is accepted by clang, which contains an invalid non-static list initialization statement of flexible array member a[]:

struct S {
    int bb;
    int a[];
};

void f ()
{
    S s=S{0,{}};
    //rejected
    //S s=S{0,{0}};
}

This only happens when the initialization list of a is empty, otherwise the compiler correctly rejects it.

GCC rejects the code. While ICC performs similarly with clang(accepts the first statement but rejects the second one).

https://godbolt.org/z/66a4qE63a

Rush10233 avatar Jul 14 '24 09:07 Rush10233

@llvm/issue-subscribers-clang-frontend

Author: None (Rush10233)

The following code is accepted by clang, which contains an invalid non-static list initialization statement of flexible array member `a[]`:
struct S {
    int bb;
    int a[];
};

void f ()
{
    S s=S{0,{}};
    //rejected
    //S s=S{0,{0}};
}

This only happens when the initialization list of a is empty, otherwise the compiler correctly rejects it.

GCC rejects the code. While ICC performs similarly with clang(accepts the first statement but rejects the second one).

https://godbolt.org/z/66a4qE63a

llvmbot avatar Jul 14 '24 14:07 llvmbot

CC @AaronBallman

shafik avatar Jul 14 '24 21:07 shafik

It seems that empty flexible array initializers are always permitted as an extension. Should it be changed?

https://github.com/llvm/llvm-project/blob/6ffa995517a9b4c15aba0eb511cba309e21808cf/clang/lib/Sema/SemaInit.cpp#L2199-L2204

a-tarasyuk avatar Jul 14 '24 21:07 a-tarasyuk

I think there are (at least) two bugs here: 1) we should reject {} the same as we reject {0} in C++ mode. 2) Clang accepts VLA initialization as an extension in places GCC does not but we claim it as a GNU extension: https://godbolt.org/z/vYPfqjP7j

AaronBallman avatar Jul 15 '24 13:07 AaronBallman