Skip to content

Commit 19efbd1

Browse files
authored
[BoundsSafety] Do not check for bounds-attr output arguments in dependent contexts (#9733) (#10029)
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 a943c37 commit 19efbd1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/AST/CXXInheritance.h"
2222
#include "clang/AST/DeclObjC.h"
2323
#include "clang/AST/DeclTemplate.h"
24+
#include "clang/AST/DependenceFlags.h"
2425
#include "clang/AST/EvaluatedExprVisitor.h"
2526
#include "clang/AST/Expr.h"
2627
#include "clang/AST/ExprCXX.h"
@@ -8251,7 +8252,7 @@ static bool checkDynamicCountPointerAsParameter(Sema &S, FunctionDecl *FDecl,
82518252
if (!ArgInfo.isCountInParamOrCountPointer() && !ArgInfo.isCountInRet() &&
82528253
!ParmInfo.isCountInParamOrCountPointer() && !ParmInfo.isCountInRet())
82538254
continue;
8254-
assert(!ActualArgExp->isValueDependent());
8255+
82558256
// Disable these checks for attribute-only mode because we don't want
82568257
// non-type-incompatibility errors in that mode.
82578258
if (!S.getLangOpts().isBoundsSafetyAttributeOnlyMode() &&
@@ -8823,7 +8824,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
88238824
!CurContext->isDependentContext()) {
88248825
// FIXME: We need to support function pointers and blocks that don't have
88258826
// function decl.
8826-
if (!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
8827+
8828+
// For C++, we don't want to check for any dependent constructs.
8829+
if (TheCall->getDependence() == ExprDependence::None &&
8830+
!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
88278831
return ExprError();
88288832
if (getLangOpts().BoundsSafety)
88298833
if (!checkDynamicRangePointerAsParameter(*this, FDecl, TheCall))
@@ -26011,4 +26015,4 @@ ExprResult Sema::ActOnUnsafeTerminatedByFromIndexable(
2601126015
PointerToTerminatorExpr, BuiltinLoc,
2601226016
RParenLoc);
2601326017
}
26014-
/* TO_UPSTREAM(BoundsSafety) OFF*/
26018+
/* TO_UPSTREAM(BoundsSafety) OFF*/

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)