Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
927b2f6
Update UnsafeBufferUsage.h
shreya-jain Sep 3, 2025
bae5f2e
Update UnsafeBufferUsageGadgets.def
shreya-jain Sep 3, 2025
284c262
Update DiagnosticGroups.td
shreya-jain Sep 3, 2025
980a75e
Update DiagnosticSemaKinds.td
shreya-jain Sep 3, 2025
11fc596
Update UnsafeBufferUsage.cpp
shreya-jain Sep 3, 2025
800d393
Update AnalysisBasedWarnings.cpp
shreya-jain Sep 3, 2025
4c95972
Update warn-unsafe-buffer-usage-debug-unclaimed.cpp
shreya-jain Sep 3, 2025
38a2d44
fix compilation issues and test
shreya-jain Sep 3, 2025
dd1ab78
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Sep 4, 2025
aacc510
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Sep 8, 2025
f074fd0
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Sep 9, 2025
5bc6580
address review comments
shreya-jain Sep 16, 2025
766be17
Merge branch 'add-uniqueptr-to-unsafe-buffer-usage' of github.com:shr…
shreya-jain Sep 16, 2025
eb9afef
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Sep 16, 2025
efb19b6
[-Wunsafe-buffer-usage] Add unique_ptr <T[]> accesses
shreya-jain Sep 18, 2025
7cefe30
[-Wunsafe-buffer-usage] Add unique_ptr <T[]> accesses
shreya-jain Sep 18, 2025
6cf2c9f
[-Wunsafe-buffer-usage] Add unique_ptr <T[]> accesses
shreya-jain Sep 18, 2025
f805faf
[-Wunsafe-buffer-usage] Add unique_ptr <T[]> accesses
shreya-jain Sep 30, 2025
db8a2bc
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Oct 1, 2025
9d597ed
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Oct 1, 2025
2b823cb
Merge branch 'main' into add-uniqueptr-to-unsafe-buffer-usage
shreya-jain Oct 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ static bool isSupportedVariable(const DeclRefExpr &Node) {
return D != nullptr && isa<VarDecl>(D);
}

// Returns true for RecordDecl of type std::unique_ptr<T[]>
static bool isUniquePtrArray(const CXXRecordDecl *RecordDecl) {
if (!RecordDecl || !RecordDecl->isInStdNamespace() ||
RecordDecl->getNameAsString() != "unique_ptr")
Expand All @@ -1343,6 +1344,7 @@ static bool isUniquePtrArray(const CXXRecordDecl *RecordDecl) {
}

class UniquePtrArrayAccessGadget : public WarningGadget {
private:
static constexpr const char *const AccessorTag = "unique_ptr_array_access";
const CXXOperatorCallExpr *AccessorExpr;

Expand Down Expand Up @@ -1374,13 +1376,21 @@ class UniquePtrArrayAccessGadget : public WarningGadget {
if (!Method)
return false;

if (Method->getNameAsString() != "operator[]")
if (Method->getOverloadedOperator() != OO_Subscript)
return false;

const CXXRecordDecl *RecordDecl = Method->getParent();
if (!isUniquePtrArray(RecordDecl))
return false;

const Expr *IndexExpr = OpCall->getArg(1);
llvm::APSInt IndexValue;

// Allow [0]
if (IndexExpr->EvaluateAsInt(IndexValue, Ctx) && IndexValue.isZero()) {
return false;
}

Result.addNode(AccessorTag, DynTypedNode::create(*OpCall));
return true;
}
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/Sema/AnalysisBasedWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2613,10 +2613,8 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
std::string Message;

Loc = Node.get<Stmt>()->getBeginLoc();
Message = "Direct operator[] access on std::unique_ptr<T[]> is unsafe "
"(no bounds check).";
S.Diag(Loc, diag::warn_unsafe_buffer_usage_unique_ptr_array_access)
<< Message << Node.getSourceRange();
<< Node.getSourceRange();
}

bool isSafeBufferOptOut(const SourceLocation &Loc) const override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ template <class T> class unique_ptr {

void basic_unique_ptr() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add more test cases - I imagine at a minimum we should have these:

  • index is zero
  • index is a non-zero integer literal
  • index is a variable
  • index is a simple arithmetic expression like i + 5

Copy link
Contributor Author

@shreya-jain shreya-jain Sep 18, 2025

Choose a reason for hiding this comment

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

Added some more tests.

Added a case where index is 0 fails for when it's not a compile time constant. Not sure if that is the desired behavior

int k = 0; 
buffer[k]; // will flag

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is fine. The warning is not path sensitive.

std::unique_ptr<int[]> p1;
p1[0]; // expected-warning{{direct access using operator[] on std::unique_ptr<T[]> is unsafe due to lack of bounds checking}}
p1[1]; // expected-warning{{direct access using operator[] on std::unique_ptr<T[]> is unsafe due to lack of bounds checking}}
}

// CHECK: Root # 1
Expand Down
43 changes: 43 additions & 0 deletions clang/test/SemaCXX/warn-unsafe-buffer-usage-unique-ptr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %clang_cc1 -Wno-unused-value -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -std=c++20 -verify=expected %s

// This debugging facility is only available in debug builds.
//
// REQUIRES: asserts

namespace std {
inline namespace __1 {
template <class T> class unique_ptr {
public:
T &operator[](long long i) const;
};
} // namespace __1
} // namespace std

void basic_unique_ptr() {
std::unique_ptr<int[]> p1;
int i = 2;

p1[0]; // This is allowed

p1[1]; // expected-warning{{direct access using operator[] on std::unique_ptr<T[]> is unsafe due to lack of bounds checking}}

p1[i]; // expected-warning{{direct access using operator[] on std::unique_ptr<T[]> is unsafe due to lack of bounds checking}}

p1[i + 5]; // expected-warning{{direct access using operator[] on std::unique_ptr<T[]> is unsafe due to lack of bounds checking}}
}

// CHECK: Root # 1
// CHECK: |- DeclRefExpr # 4
// CHECK: |-- UnaryOperator(++) # 1
// CHECK: |--- CompoundStmt # 1
// CHECK: |-- ImplicitCastExpr(LValueToRValue) # 1
// CHECK: |--- BinaryOperator(+) # 1
// CHECK: |---- ParenExpr # 1
// CHECK: |----- BinaryOperator(+) # 1
// CHECK: |------ ParenExpr # 1
// CHECK: |------- UnaryOperator(*) # 1
// CHECK: |-------- CompoundStmt # 1
// CHECK: |-- BinaryOperator(-=) # 1
// CHECK: |--- CompoundStmt # 1
// CHECK: |-- UnaryOperator(--) # 1
// CHECK: |--- CompoundStmt # 1
Loading