From d1ae844dc2cc58bc762135d9500464c61d50f4f9 Mon Sep 17 00:00:00 2001 From: Rashmi Mudduluru Date: Thu, 15 Jun 2023 16:45:28 -0700 Subject: [PATCH] [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage Differential Revision: https://reviews.llvm.org/D153064 --- clang/lib/Analysis/UnsafeBufferUsage.cpp | 12 +++++++++++ .../SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp | 24 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp index 999b94a..49229a6 100644 --- a/clang/lib/Analysis/UnsafeBufferUsage.cpp +++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp @@ -2139,6 +2139,18 @@ void clang::checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler, bool EmitSuggestions) { assert(D && D->getBody()); + + // Do not emit fixit suggestions for functions declared in an + // extern "C" block. + if (const auto *FD = dyn_cast(D)) { + for (FunctionDecl *FReDecl : FD->redecls()) { + if (FReDecl->isExternC()) { + EmitSuggestions = false; + break; + } + } + } + WarningGadgetSets UnsafeOps; FixableGadgetSets FixablesForAllVars; diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp new file mode 100644 index 0000000..6c13409 --- /dev/null +++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s + +extern "C" { +void foo(int *ptr) { + ptr[5] = 10; // expected-warning{{unsafe buffer access}} +} + +void bar(int *ptr); + +struct c_struct { + char *name; +}; +} + +void bar(int *ptr) { + ptr[5] = 10; // expected-warning{{unsafe buffer access}} +} + +void call_foo(int *p) { + foo(p); + struct c_struct str; + str.name[7] = 9; // expected-warning{{unsafe buffer access}} + bar(p); +} -- 2.7.4