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

Thomas Sepez via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 12 14:11:35 PST 2025


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 <tsepez at chromium.org>
Date: Tue, 11 Feb 2025 23:54:10 +0000
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<char> s, std::span<char> 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 <tsepez at chromium.org>
Date: Wed, 12 Feb 2025 22:08:50 +0000
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<char> s, std::span<char> 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



More information about the cfe-commits mailing list