Skip to content

Commit d4329b6

Browse files
authored
[BoundsSafety] Do not check for bounds-attr output arguments in dependent contexts (#9733)
The bounds-attribute-only mode may be applied to C++ programs, where attributed functions/fields may be used in dependent contexts such as a template. DO NOT type check at those places. Their instantiations will still be checked. (rdar://141708643)
1 parent 9a6c89d commit d4329b6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/AST/Decl.h"
2323
#include "clang/AST/DeclObjC.h"
2424
#include "clang/AST/DeclTemplate.h"
25+
#include "clang/AST/DependenceFlags.h"
2526
#include "clang/AST/DynamicRecursiveASTVisitor.h"
2627
#include "clang/AST/EvaluatedExprVisitor.h"
2728
#include "clang/AST/Expr.h"
@@ -8332,7 +8333,7 @@ static bool checkDynamicCountPointerAsParameter(Sema &S, FunctionDecl *FDecl,
83328333
if (!ArgInfo.isCountInParamOrCountPointer() && !ArgInfo.isCountInRet() &&
83338334
!ParmInfo.isCountInParamOrCountPointer() && !ParmInfo.isCountInRet())
83348335
continue;
8335-
assert(!ActualArgExp->isValueDependent());
8336+
83368337
// Disable these checks for attribute-only mode because we don't want
83378338
// non-type-incompatibility errors in that mode.
83388339
if (!S.getLangOpts().isBoundsSafetyAttributeOnlyMode() &&
@@ -8903,7 +8904,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
89038904
if (getLangOpts().BoundsSafetyAttributes && FDecl) {
89048905
// FIXME: We need to support function pointers and blocks that don't have
89058906
// function decl.
8906-
if (!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
8907+
8908+
// For C++, we don't want to check for any dependent constructs.
8909+
if (TheCall->getDependence() == ExprDependence::None &&
8910+
!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
89078911
return ExprError();
89088912
if (getLangOpts().BoundsSafety)
89098913
if (!checkDynamicRangePointerAsParameter(*this, FDecl, TheCall))

clang/test/BoundsSafety/Sema/unsafe-buffer-usage-interop-crash.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ class MyClass {
1111
namespace value_dependent_assertion_violation {
1212
// Running attribute-only mode on the C++ code below crashes
1313
// previously.
14-
void g(unsigned);
14+
void g(int * __counted_by(n) * p, size_t n);
1515

1616
template<typename T>
1717
struct S {
1818
void f(T p) {
19-
g(sizeof(p));
19+
g(nullptr, sizeof(p));
2020
}
2121
};
22+
23+
// expected-error@-4{{incompatible dynamic count pointer argument to parameter of type 'int * __counted_by(n)*' (aka 'int **')}}
24+
template struct S<int>; // expected-note{{in instantiation of member function 'value_dependent_assertion_violation::S<int>::f' requested here}}
2225
}

0 commit comments

Comments
 (0)