Skip to content

[clang][Sema] Don't report pointer subtraction on a VLA as zero size - #217465

Merged
flash1729 merged 3 commits into
llvm:mainfrom
flash1729:gh28328-vla-pointer-sub
Sep 1, 2026
Merged

flash1729 merged 3 commits into
llvm:mainfrom
flash1729:gh28328-vla-pointer-sub

Conversation

@flash1729

Copy link
Copy Markdown
Member

CheckSubtractionOperands warns when the pointee type has zero size, because the subtraction divides by that size. A variably modified type such as int[n] has no statically known size, and getTypeInfoImpl models it as zero, so the check reported it as an empty type even though its size is only determined at run time.

Exclude variably modified pointee types. Genuinely empty types keep warning: int[0] and zero-sized structs are constant arrays and records, not variably modified.

Fixes #28328

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 121784 tests passed
  • 5079 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 57999 tests passed
  • 2914 tests skipped

✅ The build succeeded and all tests passed.

@flash1729
flash1729 force-pushed the gh28328-vla-pointer-sub branch 2 times, most recently from b0db4c7 to 8327ea5 Compare August 22, 2026 08:00
@flash1729
flash1729 marked this pull request as ready for review August 22, 2026 11:42
@flash1729
flash1729 requested review from Endilll and cor3ntin August 22, 2026 11:42
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:static analyzer labels Aug 22, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Aditya Medhane (flash1729)

Changes

CheckSubtractionOperands warns when the pointee type has zero size, because the subtraction divides by that size. A variably modified type such as int[n] has no statically known size, and getTypeInfoImpl models it as zero, so the check reported it as an empty type even though its size is only determined at run time.

Exclude variably modified pointee types. Genuinely empty types keep warning: int[0] and zero-sized structs are constant arrays and records, not variably modified.

Fixes #28328


Full diff: https://github.com/llvm/llvm-project/pull/217465.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.md (+5)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+4-1)
  • (modified) clang/test/Analysis/pointer-sub.c (+2-4)
  • (modified) clang/test/Sema/empty1.c (+7)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 3c6694f510952..8a5ed09e173ba 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -416,6 +416,11 @@ features cannot lower the translation-unit ABI level;
 - `-Wc++98-compat` now diagnoses explicit conversion functions in C++20 and
   later, matching the behavior in C++11 through C++17. (#GH161689)
 
+- `-Wpointer-arith` no longer reports subtraction of pointers to a variably
+  modified type, such as `int[n]`, as a subtraction of pointers to a type of
+  zero size. The size of such a type is not known statically and is modelled as
+  zero, which is not the same as the type being empty. (#GH28328)
+
 ### Improvements to Clang's time-trace
 
 ### Improvements to Coverage Mapping
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index da76bbf3c35f0..3977dc4932743 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11916,7 +11916,10 @@ QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS,
       // The pointee type may have zero size.  As an extension, a structure or
       // union may have zero size or an array may have zero length.  In this
       // case subtraction does not make sense.
-      if (!rpointee->isVoidType() && !rpointee->isFunctionType()) {
+      // A variably modified type has no statically known size; it is modelled
+      // as zero here, so exclude it rather than report it as empty.
+      if (!rpointee->isVoidType() && !rpointee->isFunctionType() &&
+          !rpointee->isVariablyModifiedType()) {
         CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
         if (ElementSize.isZero()) {
           Diag(Loc,diag::warn_sub_ptr_zero_size_types)
diff --git a/clang/test/Analysis/pointer-sub.c b/clang/test/Analysis/pointer-sub.c
index 25fb7f043d468..d2155e110ba54 100644
--- a/clang/test/Analysis/pointer-sub.c
+++ b/clang/test/Analysis/pointer-sub.c
@@ -65,11 +65,9 @@ void f4(void) {
   int (*p)[m] = a; // p == &a[0]
   p += 1; // p == &a[1]
 
-  // FIXME: This is a known problem with -Wpointer-arith (https://github.com/llvm/llvm-project/issues/28328)
-  int d = p - a; // d == 1 // expected-warning{{subtraction of pointers to type 'int[m]' of zero size has undefined behavior}}
+  int d = p - a; // d == 1
 
-  // FIXME: This is a known problem with -Wpointer-arith (https://github.com/llvm/llvm-project/issues/28328)
-  d = &(a[2]) - &(a[1]); // expected-warning{{subtraction of pointers to type 'int[m]' of zero size has undefined behavior}}
+  d = &(a[2]) - &(a[1]);
 
   d = a[2] - a[1]; // expected-warning{{Subtraction of two pointers that}}
 }
diff --git a/clang/test/Sema/empty1.c b/clang/test/Sema/empty1.c
index 6c5fe76833f3f..0e18023b7d313 100644
--- a/clang/test/Sema/empty1.c
+++ b/clang/test/Sema/empty1.c
@@ -85,3 +85,10 @@ int func_9(struct emp_1 (*x)[], struct emp_1 (*y)[]) {
 int func_10(int (*x)[0], int (*y)[0]) {
   return x - y; // expected-warning {{subtraction of pointers to type 'int[0]' of zero size has undefined behavior}}
 }
+
+// A variably modified type is modelled as having zero size because its size is
+// not known statically. It is not an empty type, so it must not be diagnosed.
+int func_11(int n) {
+  int v[n];
+  return &v + 1 - &v;
+}

@cor3ntin cor3ntin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to be slightly more clever here and try to evaluate the size as an ICE before deciding whether to warn or not?
@AaronBallman

Comment thread clang/docs/ReleaseNotes.md Resolved
@flash1729
flash1729 force-pushed the gh28328-vla-pointer-sub branch from 8327ea5 to 5adb6f1 Compare August 30, 2026 12:18
CheckSubtractionOperands warns when the pointee type has zero size,
because the subtraction divides by that size. A variably modified type
such as int[n] has no statically known size, and getTypeInfoImpl models
it as zero, so the check reported it as an empty type even though its
size is only determined at run time.

Instead of trusting the static size, decide whether the size is provably
zero: walk the array dimensions, folding variable bounds as integer
constant expressions, and warn only when a dimension is provably zero or
the base element type has zero size. This keeps the warning for genuinely
zero-sized cases such as int[0], zero-sized structs, and VLAs of
zero-sized types, while dropping it when the size is simply unknown.

Fixes llvm#28328
@flash1729
flash1729 force-pushed the gh28328-vla-pointer-sub branch from 5adb6f1 to b0501a0 Compare August 30, 2026 12:25
@github-actions

github-actions Bot commented Aug 30, 2026 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this!

if (CAT->isZeroSize())
return true;
} else if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) {
if (const Expr *Bound = VAT->getSizeExpr())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some C++ tests where the size expression is a dependent value, just to be sure the behavior there is reasonable with and without instantiation? e.g.,

template <int N>
int not_instantiated() {
  int array[N];
  return &array - &array; // no diagnostic
}

template <int N>
int instantiated() {
  int array[N];
  return &array - &array; // diagnostic
}

int x = instantiated<0>; // template instantiation note
int y = instantiated<1>; // okay

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@flash1729
flash1729 merged commit 5407e6e into llvm:main Sep 1, 2026
11 checks passed
Iasonaskrpr pushed a commit to Iasonaskrpr/llvm-project that referenced this pull request Sep 4, 2026
…lvm#217465)

CheckSubtractionOperands warns when the pointee type has zero size,
because the subtraction divides by that size. A variably modified type
such as int[n] has no statically known size, and getTypeInfoImpl models
it as zero, so the check reported it as an empty type even though its
size is only determined at run time.

Instead of trusting the static size, decide whether the size is provably
zero: walk the array dimensions, folding each variable bound as an
integer constant expression, and warn only when a dimension is provably
zero or the base element type has zero size. Genuinely empty cases keep
warning, including int[0], zero-sized structs, and VLAs of zero-sized
types such as struct Empty vla[n] or int vla[n][0].

Fixes llvm#28328
asudarsa-qti pushed a commit to asudarsa-qti/llvm-project that referenced this pull request Sep 4, 2026
…lvm#217465)

CheckSubtractionOperands warns when the pointee type has zero size,
because the subtraction divides by that size. A variably modified type
such as int[n] has no statically known size, and getTypeInfoImpl models
it as zero, so the check reported it as an empty type even though its
size is only determined at run time.

Instead of trusting the static size, decide whether the size is provably
zero: walk the array dimensions, folding each variable bound as an
integer constant expression, and warn only when a dimension is provably
zero or the base element type has zero size. Genuinely empty cases keep
warning, including int[0], zero-sized structs, and VLAs of zero-sized
types such as struct Empty vla[n] or int vla[n][0].

Fixes llvm#28328
addmisol pushed a commit to addmisol/llvm-project2 that referenced this pull request Sep 6, 2026
…lvm#217465)

CheckSubtractionOperands warns when the pointee type has zero size,
because the subtraction divides by that size. A variably modified type
such as int[n] has no statically known size, and getTypeInfoImpl models
it as zero, so the check reported it as an empty type even though its
size is only determined at run time.

Instead of trusting the static size, decide whether the size is provably
zero: walk the array dimensions, folding each variable bound as an
integer constant expression, and warn only when a dimension is provably
zero or the base element type has zero size. Genuinely empty cases keep
warning, including int[0], zero-sized structs, and VLAs of zero-sized
types such as struct Empty vla[n] or int vla[n][0].

Fixes llvm#28328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong "warning: subtraction of pointers to type 'int [n]' of zero size has undefined behavior [-Wpointer-arith]"

3 participants