Skip to content

Commit cce5781

Browse files
committed
[-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 80525df commit cce5781

8 files changed

+586
-4
lines changed

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

Lines changed: 12 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,17 @@ 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+
virtual void handleUnsafeLibcCall(const CallExpr *Call, unsigned PrintfInfo,
119+
ASTContext &Ctx) = 0;
120+
109121
/// Invoked when an unsafe operation with a std container is found.
110122
virtual void handleUnsafeOperationInContainer(const Stmt *Operation,
111123
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
@@ -12383,6 +12383,13 @@ def warn_unsafe_buffer_operation : Warning<
1238312383
"%select{unsafe pointer operation|unsafe pointer arithmetic|"
1238412384
"unsafe buffer access|function introduces unsafe buffer manipulation|unsafe invocation of span::data}0">,
1238512385
InGroup<UnsafeBufferUsage>, DefaultIgnore;
12386+
def warn_unsafe_buffer_libc_call : Warning<
12387+
"function %0 introduces unsafe buffer access">,
12388+
InGroup<UnsafeBufferUsage>, DefaultIgnore;
12389+
def note_unsafe_buffer_printf_call : Note<
12390+
"%select{| change to 'snprintf' for explicit bounds checking | buffer pointer and size may not match"
12391+
"| use 'std::string::c_str' or string literal as string pointer to guarantee null-termination"
12392+
"| do not use va_list that cannot be checked at compile-time for bounds safety}0">;
1238612393
def note_unsafe_buffer_operation : Note<
1238712394
"used%select{| in pointer arithmetic| in buffer access}0 here">;
1238812395
def note_unsafe_buffer_variable_fixit_group : Note<

0 commit comments

Comments
 (0)