Skip to content

Cherrypick [-Wunsafe-buffer-usage] Emit a warning if pointer returned by vector::data and array::data is cast to larger type #9584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12475,7 +12475,7 @@ def warn_unsafe_buffer_variable : Warning<
InGroup<UnsafeBufferUsage>, DefaultIgnore;
def warn_unsafe_buffer_operation : Warning<
"%select{unsafe pointer operation|unsafe pointer arithmetic|"
"unsafe buffer access|function introduces unsafe buffer manipulation|unsafe invocation of span::data|"
"unsafe buffer access|function introduces unsafe buffer manipulation|unsafe invocation of %1|"
"field %1 prone to unsafe buffer manipulation}0">,
InGroup<UnsafeBufferUsage>, DefaultIgnore;
def warn_unsafe_buffer_libc_call : Warning<
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/Analysis/UnsafeBufferUsage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,11 @@ class DataInvocationGadget : public WarningGadget {
}

static Matcher matcher() {
Matcher callExpr = cxxMemberCallExpr(
callee(cxxMethodDecl(hasName("data"), ofClass(hasName("std::span")))));

Matcher callExpr = cxxMemberCallExpr(callee(
cxxMethodDecl(hasName("data"),
ofClass(anyOf(hasName("std::span"), hasName("std::array"),
hasName("std::vector"))))));
return stmt(
explicitCastExpr(anyOf(has(callExpr), has(parenExpr(has(callExpr)))))
.bind(OpTag));
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Sema/AnalysisBasedWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2279,9 +2279,18 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
QualType srcType = ECE->getSubExpr()->getType();
const uint64_t sSize =
Ctx.getTypeSize(srcType.getTypePtr()->getPointeeType());

if (sSize >= dSize)
return;

if (const auto *CE = dyn_cast<CXXMemberCallExpr>(
ECE->getSubExpr()->IgnoreParens())) {
D = CE->getMethodDecl();
}

if (!D)
return;

MsgParam = 4;
}
Loc = Operation->getBeginLoc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,68 @@ void foo(int *p){}
namespace std{
template <typename T> class span {

T *elements;
T *elements;

span(T *, unsigned){}
span(T *, unsigned){}

public:
public:

constexpr span<T> subspan(size_t offset, size_t count) const {
return span<T> (elements+offset, count); // expected-warning{{unsafe pointer arithmetic}}
}
constexpr span<T> subspan(size_t offset, size_t count) const {
return span<T> (elements+offset, count); // expected-warning{{unsafe pointer arithmetic}}
}

constexpr T* data() const noexcept {
return elements;
}
constexpr T* data() const noexcept {
return elements;
}

constexpr T* hello() const noexcept {
return elements;
}
};

template <typename T> class vector {

T *elements;

public:

vector(size_t n) {
elements = new T[n];
}

constexpr T* data() const noexcept {
return elements;
}

~vector() {
delete[] elements;
}
};

template <class T, size_t N>
class array {
T elements[N];

public:

constexpr const T* data() const noexcept {
return elements;
}

};


constexpr T* hello() const noexcept {
return elements;
}
};

template <typename T> class span_duplicate {
span_duplicate(T *, unsigned){}
span_duplicate(T *, unsigned){}

T array[10];
T array[10];

public:
public:

T* data() {
return array;
}
T* data() {
return array;
}

};
};
}

using namespace std;
Expand All @@ -89,21 +119,28 @@ void cast_without_data(int *ptr) {
float *p = (float*) ptr;
}

void warned_patterns(std::span<int> span_ptr, std::span<Base> base_span, span<int> span_without_qual) {
A *a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of span::data}}
a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of span::data}}
void warned_patterns_span(std::span<int> span_ptr, std::span<Base> base_span, span<int> span_without_qual) {
A *a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of 'data'}}
a1 = (A*)span_ptr.data(); // expected-warning{{unsafe invocation of 'data'}}

a1 = (A*)(span_ptr.data()); // expected-warning{{unsafe invocation of span::data}}
A *a2 = (A*) (span_without_qual.data()); // expected-warning{{unsafe invocation of span::data}}
a1 = (A*)(span_ptr.data()); // expected-warning{{unsafe invocation of 'data'}}
A *a2 = (A*) (span_without_qual.data()); // expected-warning{{unsafe invocation of 'data'}}

a2 = (A*) span_without_qual.data(); // expected-warning{{unsafe invocation of span::data}}
a2 = (A*) span_without_qual.data(); // expected-warning{{unsafe invocation of 'data'}}

// TODO:: Should we warn when we cast from base to derived type?
Derived *b = dynamic_cast<Derived*> (base_span.data());// expected-warning{{unsafe invocation of span::data}}
Derived *b = dynamic_cast<Derived*> (base_span.data());// expected-warning{{unsafe invocation of 'data'}}

// TODO:: This pattern is safe. We can add special handling for it, if we decide this
// is the recommended fixit for the unsafe invocations.
A *a3 = (A*)span_ptr.subspan(0, sizeof(A)).data(); // expected-warning{{unsafe invocation of span::data}}
A *a3 = (A*)span_ptr.subspan(0, sizeof(A)).data(); // expected-warning{{unsafe invocation of 'data'}}
}

void warned_patterns_array(std::array<int, 5> array_ptr, std::array<Base, 10> base_span, span<int> span_without_qual) {
const A *a1 = (A*)array_ptr.data(); // expected-warning{{unsafe invocation of 'data'}}
a1 = (A*)array_ptr.data(); // expected-warning{{unsafe invocation of 'data'}}

a1 = (A*)(array_ptr.data()); // expected-warning{{unsafe invocation of 'data'}}
}

void not_warned_patterns(std::span<A> span_ptr, std::span<Base> base_span) {
Expand Down