Skip to content

[-Wunsafe-buffer-usage] Introduce std::array fixits #80084

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
463a990
[-Wunsafe-buffer-usage] Move Strategy class to the header
jkorous-apple Jan 24, 2024
38255d1
[-Wunsafe-buffer-usage] Implement fixits from const size array to std…
jkorous-apple Jan 24, 2024
83e31ad
[-Wunsafe-buffer-usage] Don't assert Array strategy is unimplemented
jkorous-apple Jan 24, 2024
da47129
[-Wunsafe-buffer-usage] Preserve spelling of array size in std::array…
jkorous-apple Jan 27, 2024
e3c2483
[-Wunsafe-buffer-usage] Preserve array element type spelling in std::…
jkorous-apple Jan 27, 2024
d19a2c3
[-Wunsafe-buffer-usage] Fix whitespace handling in array types
jkorous-apple Jan 29, 2024
849f643
[-Wnsafe-buffer-usage][NFC] Fix and refactor tests
jkorous-apple Jan 29, 2024
b6688fb
[-Wunsafe-buffer-usage] Bail on fixits for arrays whose size is auto-…
jkorous-apple Jan 29, 2024
fd791cc
[-Wunsafe-buffer-usage][NFC] Add testcases
jkorous-apple Jan 29, 2024
9183a63
[-Wunsafe-buffer-usage] Clang-format std::array fixits work
jkorous-apple Jan 31, 2024
04abb73
[-Wunsafe-buffer-usage] Fix note message suggesting to use std::array
jkorous-apple Feb 1, 2024
5540d7b
[-Wunsafe-buffer-usage] Enable fixits for const ptr array declarations
jkorous-apple Feb 1, 2024
7694a6d
[-Wunsafe-buffer-usage] Remove unused TargetType enum
jkorous-apple Feb 1, 2024
437a4f5
[-Wunsafe-buffer-usage] Explicitly unsupport fixits for multidimensio…
jkorous-apple Feb 6, 2024
d49d5fd
[-Wunsafe-buffer-usage] Avoid type sugar in array fixits
jkorous-apple Feb 8, 2024
17f7b9e
[-Wunsafe-buffer-usage] Avoid multi-dimentional arrays properly
jkorous-apple Feb 8, 2024
7cb98ff
[-Wunsafe-buffer-usage] Improve diagnostic note message
jkorous-apple Feb 12, 2024
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
45 changes: 42 additions & 3 deletions clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ class VariableGroupsManager {
virtual VarGrpRef getGroupOfParms() const =0;
};

// FixitStrategy is a map from variables to the way we plan to emit fixes for
// these variables. It is figured out gradually by trying different fixes
// for different variables depending on gadgets in which these variables
// participate.
class FixitStrategy {
public:
enum class Kind {
Wontfix, // We don't plan to emit a fixit for this variable.
Span, // We recommend replacing the variable with std::span.
Iterator, // We recommend replacing the variable with std::span::iterator.
Array, // We recommend replacing the variable with std::array.
Vector // We recommend replacing the variable with std::vector.
};

private:
using MapTy = llvm::DenseMap<const VarDecl *, Kind>;

MapTy Map;

public:
FixitStrategy() = default;
FixitStrategy(const FixitStrategy &) = delete; // Let's avoid copies.
FixitStrategy &operator=(const FixitStrategy &) = delete;
FixitStrategy(FixitStrategy &&) = default;
FixitStrategy &operator=(FixitStrategy &&) = default;

void set(const VarDecl *VD, Kind K) { Map[VD] = K; }

Kind lookup(const VarDecl *VD) const {
auto I = Map.find(VD);
if (I == Map.end())
return Kind::Wontfix;

return I->second;
}
};

/// The interface that lets the caller handle unsafe buffer usage analysis
/// results by overriding this class's handle... methods.
class UnsafeBufferUsageHandler {
Expand Down Expand Up @@ -75,9 +112,11 @@ class UnsafeBufferUsageHandler {
///
/// `D` is the declaration of the callable under analysis that owns `Variable`
/// and all of its group mates.
virtual void handleUnsafeVariableGroup(const VarDecl *Variable,
const VariableGroupsManager &VarGrpMgr,
FixItList &&Fixes, const Decl *D) = 0;
virtual void
handleUnsafeVariableGroup(const VarDecl *Variable,
const VariableGroupsManager &VarGrpMgr,
FixItList &&Fixes, const Decl *D,
const FixitStrategy &VarTargetTypes) = 0;

#ifndef NDEBUG
public:
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12113,9 +12113,9 @@ def warn_unsafe_buffer_operation : Warning<
def note_unsafe_buffer_operation : Note<
"used%select{| in pointer arithmetic| in buffer access}0 here">;
def note_unsafe_buffer_variable_fixit_group : Note<
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information%select{|, and change %2 to '%select{std::span|std::array|std::span::iterator}1' to propagate bounds information between them}3">;
"change type of %0 to '%select{std::span' to preserve bounds information|std::array' to label it for hardening|std::span::iterator' to preserve bounds information}1%select{|, and change %2 to '%select{std::span|std::array|std::span::iterator}1' to propagate bounds information between them}3">;
def note_unsafe_buffer_variable_fixit_together : Note<
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information"
"change type of %0 to '%select{std::span' to preserve bounds information|std::array' to label it for hardening|std::span::iterator' to preserve bounds information}1"
"%select{|, and change %2 to safe types to make function %4 bounds-safe}3">;
def note_safe_buffer_usage_suggestions_disabled : Note<
"pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions">;
Expand Down
Loading