Skip to content

Conversation

@tsepez
Copy link
Contributor

@tsepez tsepez commented Feb 12, 2025

Add a test for passing a constexpr size argument to snprintf()
giving an unsafe buffers warning.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Feb 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 12, 2025

@llvm/pr-subscribers-clang

Author: Thomas Sepez (tsepez)

Changes

Add a test for passing a constexpr size argument to snprintf()
giving an unsafe buffers warning.


Full diff: https://github.com/llvm/llvm-project/pull/126826.diff

1 Files Affected:

  • (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp (+3-2)
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

@tsepez
Copy link
Contributor Author

tsepez commented Feb 12, 2025

@shafik @ziqingluo-90 Please take a look.

Copy link
Contributor

@ziqingluo-90 ziqingluo-90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I think we can keep the test of sizeof(b) and add yours at a new line.
Thanks.

@tsepez
Copy link
Contributor Author

tsepez commented Feb 12, 2025

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

@ziqingluo-90
Copy link
Contributor

Thank you @tsepez

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants