[clang] [-Wunsafe-buffer-usage] Fix assert when constexpr size passed to snprintf() (#119786) (PR #124022)

2025-01-31 Thread Thomas Sepez via cfe-commits

tsepez wrote:

Ping


https://github.com/llvm/llvm-project/pull/124022
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [-Wunsafe-buffer-usage] Fix assert when constexpr size passed to snprintf() (#119786) (PR #124022)

2025-02-11 Thread Thomas Sepez via cfe-commits

tsepez wrote:

Sure, I'll back-fill the tests shortly.

https://github.com/llvm/llvm-project/pull/124022
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [-Wunsafe-buffer-usage] add test for constexpr size in snprintf (#119786) (PR #126826)

2025-02-12 Thread Thomas Sepez via cfe-commits

tsepez wrote:

Ok, I believe I have updated the patch in the correct place, please take a look.

https://github.com/llvm/llvm-project/pull/126826
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [-Wunsafe-buffer-usage] add test for constexpr size in snprintf (#119786) (PR #126826)

2025-02-12 Thread Thomas Sepez via cfe-commits

https://github.com/tsepez updated 
https://github.com/llvm/llvm-project/pull/126826

>From a1c97178f02dd93e6ae28416273ff7cebfa49d45 Mon Sep 17 00:00:00 2001
From: Tom Sepez 
Date: Tue, 11 Feb 2025 23:54:10 +
Subject: [PATCH 1/2] [-Wunsafe-buffer-usage] add test for constexpr size in
 snprintf (#119786)

---
 .../test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp 
b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
index a7c19bcac1607..40e1e1fbf41a7 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
@@ -97,11 +97,12 @@ void f(char * p, char * q, std::span s, 
std::span s2) {
   wprintf(L"hello %s", p); // expected-warning{{function 'wprintf' is unsafe}} 
expected-note{{string argument is not guaranteed to be null-terminated}}
 
 
-  char a[10], b[11];
+  char a[10];
   int c[10];
+  constexpr unsigned kOneTooBig = 11;
   std::wstring WS;
 
-  snprintf(a, sizeof(b), "%s", __PRETTY_FUNCTION__); // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
+  snprintf(a, kOneTooBig, "%s", __PRETTY_FUNCTION__); // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
   snprintf((char*)c, sizeof(c), "%s", __PRETTY_FUNCTION__);  // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
   fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // 
no warn
   fprintf(fp, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // no warn

>From fea21e92538b4544994bc5356b706946ba003112 Mon Sep 17 00:00:00 2001
From: Tom Sepez 
Date: Wed, 12 Feb 2025 22:08:50 +
Subject: [PATCH 2/2] keep existing tests

---
 clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp 
b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
index 40e1e1fbf41a7..2bb5e9886045c 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp
@@ -97,11 +97,12 @@ void f(char * p, char * q, std::span s, 
std::span s2) {
   wprintf(L"hello %s", p); // expected-warning{{function 'wprintf' is unsafe}} 
expected-note{{string argument is not guaranteed to be null-terminated}}
 
 
-  char a[10];
+  char a[10], b[11];
   int c[10];
   constexpr unsigned kOneTooBig = 11;
   std::wstring WS;
 
+  snprintf(a, sizeof(b), "%s", __PRETTY_FUNCTION__); // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
   snprintf(a, kOneTooBig, "%s", __PRETTY_FUNCTION__); // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
   snprintf((char*)c, sizeof(c), "%s", __PRETTY_FUNCTION__);  // 
expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer 
and size may not match}}
   fprintf((FILE*)p, "%P%d%p%i hello world %32s", *p, *p, p, *p, "hello"); // 
no warn

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Report wider source range for unsafe buffers warnings (PR #147363)

2025-07-07 Thread Thomas Sepez via cfe-commits

https://github.com/tsepez updated 
https://github.com/llvm/llvm-project/pull/147363

>From c389b056250871b802d34de6ba2cb0cf365b751a Mon Sep 17 00:00:00 2001
From: Tom Sepez 
Date: Mon, 7 Jul 2025 17:59:53 +
Subject: [PATCH 1/2] Report wider source range for unsafe buffers warnings

---
 clang/lib/Sema/AnalysisBasedWarnings.cpp | 17 ++---
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 7420ba2d461c6..4129a9537ba40 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2275,28 +2275,23 @@ class UnsafeBufferUsageReporter : public 
UnsafeBufferUsageHandler {
 unsigned MsgParam = 0;
 NamedDecl *D = nullptr;
 if (const auto *ASE = dyn_cast(Operation)) {
-  Loc = ASE->getBase()->getExprLoc();
-  Range = ASE->getBase()->getSourceRange();
+  Loc = ASE->getExprLoc();
+  Range = ASE->getSourceRange();
   MsgParam = 2;
 } else if (const auto *BO = dyn_cast(Operation)) {
   BinaryOperator::Opcode Op = BO->getOpcode();
   if (Op == BO_Add || Op == BO_AddAssign || Op == BO_Sub ||
   Op == BO_SubAssign) {
-if (BO->getRHS()->getType()->isIntegerType()) {
-  Loc = BO->getLHS()->getExprLoc();
-  Range = BO->getLHS()->getSourceRange();
-} else {
-  Loc = BO->getRHS()->getExprLoc();
-  Range = BO->getRHS()->getSourceRange();
-}
+Loc = BO->getExprLoc();
+Range = BO->getSourceRange();
 MsgParam = 1;
   }
 } else if (const auto *UO = dyn_cast(Operation)) {
   UnaryOperator::Opcode Op = UO->getOpcode();
   if (Op == UO_PreInc || Op == UO_PreDec || Op == UO_PostInc ||
   Op == UO_PostDec) {
-Loc = UO->getSubExpr()->getExprLoc();
-Range = UO->getSubExpr()->getSourceRange();
+Loc = UO->getExprLoc();
+Range = UO->getSourceRange();
 MsgParam = 1;
   }
 } else {

>From a4078d7719f6a32521f2c0ad2f777d6b3cffcda4 Mon Sep 17 00:00:00 2001
From: Tom Sepez 
Date: Mon, 7 Jul 2025 18:49:49 +
Subject: [PATCH 2/2] next

---
 ...warn-unsafe-buffer-usage-source-ranges.cpp | 32 +--
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-source-ranges.cpp 
b/clang/test/SemaCXX/warn-unsafe-buffer-usage-source-ranges.cpp
index fe3a952696557..b3036d21a2725 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-source-ranges.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-source-ranges.cpp
@@ -6,45 +6,45 @@ void foo(int i) {
   int * ptr;
 
   ptr++;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:8}
   ptr--;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:8}
   ++ptr;
-  // CHECK: {[[@LINE-1]]:5-[[@LINE-1]]:8}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:8}
   --ptr;
-  // CHECK: {[[@LINE-1]]:5-[[@LINE-1]]:8}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:8}
 
 
   ptr + 1;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
   2 + ptr;
-  // CHECK: {[[@LINE-1]]:7-[[@LINE-1]]:10}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
   ptr + i;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
   i + ptr;
-  // CHECK: {[[@LINE-1]]:7-[[@LINE-1]]:10}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
 
 
   ptr - 3;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
   ptr - i;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:10}
 
 
   ptr += 4;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:11}
   ptr += i;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:11}
 
 
   ptr -= 5;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:11}
   ptr -= i;
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:11}
 
 
   ptr[5];
-  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:6}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:9}
   5[ptr];
-  // CHECK: {[[@LINE-1]]:5-[[@LINE-1]]:8}
+  // CHECK: {[[@LINE-1]]:3-[[@LINE-1]]:9}
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits