Skip to content

Commit 0fffdeb

Browse files
authored
[-Wunsafe-buffer-usage] Warning Libc functions (#101583)
[-Wunsafe-buffer-usage] Add warn on unsafe calls to libc functions Warning about calls to libc functions involving buffer access. Warned functions are hardcoded by names. (rdar://117182250)
1 parent ebf0599 commit 0fffdeb

8 files changed

+716
-4
lines changed

clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H
1616

1717
#include "clang/AST/Decl.h"
18+
#include "clang/AST/Expr.h"
1819
#include "clang/AST/Stmt.h"
1920
#include "clang/Basic/SourceLocation.h"
2021
#include "llvm/Support/Debug.h"
@@ -106,6 +107,20 @@ class UnsafeBufferUsageHandler {
106107
virtual void handleUnsafeOperation(const Stmt *Operation,
107108
bool IsRelatedToDecl, ASTContext &Ctx) = 0;
108109

110+
/// Invoked when a call to an unsafe libc function is found.
111+
/// \param PrintfInfo
112+
/// is 0 if the callee function is not a member of the printf family;
113+
/// is 1 if the callee is `sprintf`;
114+
/// is 2 if arguments of the call have `__size_by` relation but are not in a
115+
/// safe pattern;
116+
/// is 3 if string arguments do not guarantee null-termination
117+
/// is 4 if the callee takes va_list
118+
/// \param UnsafeArg one of the actual arguments that is unsafe, non-null
119+
/// only when `2 <= PrintfInfo <= 3`
120+
virtual void handleUnsafeLibcCall(const CallExpr *Call, unsigned PrintfInfo,
121+
ASTContext &Ctx,
122+
const Expr *UnsafeArg = nullptr) = 0;
123+
109124
/// Invoked when an unsafe operation with a std container is found.
110125
virtual void handleUnsafeOperationInContainer(const Stmt *Operation,
111126
bool IsRelatedToDecl,

clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ WARNING_GADGET(PointerArithmetic)
3838
WARNING_GADGET(UnsafeBufferUsageAttr)
3939
WARNING_GADGET(UnsafeBufferUsageCtorAttr)
4040
WARNING_GADGET(DataInvocation)
41+
WARNING_GADGET(UnsafeLibcFunctionCall)
4142
WARNING_CONTAINER_GADGET(SpanTwoParamConstructor) // Uses of `std::span(arg0, arg1)`
4243
FIXABLE_GADGET(ULCArraySubscript) // `DRE[any]` in an Unspecified Lvalue Context
4344
FIXABLE_GADGET(DerefSimplePtrArithFixable)

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12412,6 +12412,13 @@ def warn_unsafe_buffer_operation : Warning<
1241212412
"unsafe buffer access|function introduces unsafe buffer manipulation|unsafe invocation of span::data|"
1241312413
"field %1 prone to unsafe buffer manipulation}0">,
1241412414
InGroup<UnsafeBufferUsage>, DefaultIgnore;
12415+
def warn_unsafe_buffer_libc_call : Warning<
12416+
"function %0 is unsafe">,
12417+
InGroup<UnsafeBufferUsage>, DefaultIgnore;
12418+
def note_unsafe_buffer_printf_call : Note<
12419+
"%select{|change to 'snprintf' for explicit bounds checking | buffer pointer and size may not match"
12420+
"|string argument is not guaranteed to be null-terminated"
12421+
"|'va_list' is unsafe}0">;
1241512422
def note_unsafe_buffer_operation : Note<
1241612423
"used%select{| in pointer arithmetic| in buffer access}0 here">;
1241712424
def note_unsafe_buffer_variable_fixit_group : Note<

0 commit comments

Comments
 (0)