Skip to content

Commit 67440f0

Browse files
authored
[clang][bytecode] Diagnose comparisons of unrelated zero-sized pointers (#140695)
1 parent f62c379 commit 67440f0

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,14 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
11351135
}
11361136
}
11371137

1138+
if (LHS.isUnknownSizeArray() && RHS.isUnknownSizeArray()) {
1139+
const SourceInfo &Loc = S.Current->getSource(OpPC);
1140+
S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_zero_sized)
1141+
<< LHS.toDiagnosticString(S.getASTContext())
1142+
<< RHS.toDiagnosticString(S.getASTContext());
1143+
return false;
1144+
}
1145+
11381146
S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
11391147
return true;
11401148
}

clang/test/AST/ByteCode/cxx11.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,10 @@ namespace Volatile {
243243
// both-note {{in call to 'f(0)'}}
244244
};
245245
}
246+
247+
namespace ZeroSizeCmp {
248+
extern void (*start[])();
249+
extern void (*end[])();
250+
static_assert(&start != &end, ""); // both-error {{constant expression}} \
251+
// both-note {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}}
252+
}

0 commit comments

Comments
 (0)