[clang] [Wunsafe-buffer-usage] Fix false positives in handling string literals. (PR #115552)

Malavika Samak via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 11 10:32:58 PST 2024


================
@@ -38,3 +38,17 @@ void constant_idx_unsafe(unsigned idx) {
                         // expected-note at -1{{change type of 'buffer' to 'std::array' to label it for hardening}}
   buffer[10] = 0;       // expected-note{{used in buffer access here}}
 }
+
+void constant_id_string(unsigned idx) {
+  char safe_char = "abc"[1]; // no-warning
+  safe_char = ""[0];
+  safe_char = "\0"[0];
+ 
+  char abcd[5] = "abc";
+  abcd[2]; // no-warning
+
+  char unsafe_char = "abc"[3]; //expected-warning{{unsafe buffer access}}
+  unsafe_char = "abc"[-1]; //expected-warning{{unsafe buffer access}}
+  unsafe_char = ""[1]; //expected-warning{{unsafe buffer access}} 
----------------
malavikasamak wrote:

This I think should warn, as the length here including the null terminator is 1.

https://github.com/llvm/llvm-project/pull/115552


More information about the cfe-commits mailing list