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

[clang][BoundsSafety] Extend `-Wvla-potential-size-confusion` for struct fields and bounds annotations

Open rapidsna opened this issue 1 year ago • 2 comments

https://github.com/llvm/llvm-project/pull/129772

-Wvla-potential-size-confusion diagnoses when n references the file scope variable and not the parameter.

int n;
void func(int array[n], int n);

We may want to extend it to diagnose on situations mentioned in the PR:

  • Diagnosing a similar situation in structures. e.g.,
int n;
struct S {
  int n;
  int array[sizeof(n)]; // Refers to outer n, not member n
};
  • Diagnosing with constant-size arrays (requires tracking the expression for the constant-size array in the QualType) e.g.,
constexpr int n = 12;
void func(int array[n], int n);
  • Potentially, also diagnosing with any ambiguous situations with bounds annotations like below (with or without the -fexperimental-late-parse-attributes flag:
constexpr int n;
struct foo {
  int * ptr __counted_by(n);
  int n;
};

rapidsna avatar Mar 04 '25 22:03 rapidsna

@llvm/issue-subscribers-clang-frontend

Author: Yeoul Na (rapidsna)

https://github.com/llvm/llvm-project/pull/129772

-Wvla-potential-size-confusion diagnoses when n references the file scope variable and not the parameter.

int n;
void func(int array[n], int n);

We may want to extend it to diagnose on situations mentioned in the PR:

  • Diagnosing a similar situation in structures. e.g.,
int n;
struct S {
  int n;
  int array[sizeof(n)]; // Refers to outer n, not member n
};
  • Diagnosing with constant-size arrays (requires tracking the expression for the constant-size array in the QualType) e.g.,
constexpr int n = 12;
void func(int array[n], int n);
  • Potentially, also diagnosing with any ambiguous situations with bounds annotations like below (with or without the -fexperimental-late-parse-attributes flag:
constexpr int n;
struct foo {
  int * ptr __counted_by(n);
  int n;
};

llvmbot avatar Mar 04 '25 22:03 llvmbot

@AaronBallman

cor3ntin avatar Mar 06 '25 14:03 cor3ntin