llvm / llvm/llvm-project

`-Wunsafe-buffer-usage-in-container` in main

Open
#217,911 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`-Wunsafe-buffer-usage-in-container` correctly complains about the following snippet:

```c++
#include

int main( int argc, char* const argv[] )
{
auto data = std::span( argv, argc );
// do something with data
}
```

Example on godbolt: https://compiler-explorer.com/z/Yv5d6bKWb

On the other hand, since the function signature of `main` is that one and cannot be changed, it would be nice if the warning could special-case it, and not trigger any warning if the parameters are passed as-is.

There is (to my knowledge) no safe buffer API to prefer as alternative to the two-parameter constructor.

Same warning appears when writing

```c++
#include

int main( int argc, char* const argv[] )
{
auto data = std::span( argv, argv + argc );
// do something with data
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the warning with the two `main` examples in Compiler Explorer, linked in the issue. Trace the compiler warning implementation and its tests to determine how the fixed `main` signature is recognized; done means the shown `std::span` constructions no longer warn while the diagnostic remains effective elsewhere.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.