[clang] [clang][-Wunsafe-buffer-usage] Add per-family subgroups of -Wunsafe-buffer-usage-in-libc-call (PR #233604)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 23:21:19 PST 2026


================
@@ -0,0 +1,66 @@
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
+// RUN:            -verify=expected,exam %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
+// RUN:            -Wno-unsafe-buffer-usage-in-libc-call-string-examination \
+// RUN:            -verify=expected %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call-memory \
+// RUN:            -verify=mem %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
+// RUN:            -Wno-unsafe-buffer-usage-in-libc-call \
+// RUN:            -Wunsafe-buffer-usage-in-libc-call-memory -verify=mem %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
+// RUN:            -Wno-unsafe-buffer-usage-in-libc-call -verify=none %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage-in-libc-call \
+// RUN:            %s 2>&1 | FileCheck %s
+
----------------
usx95 wrote:

I would suggest to give each family its own prefix to avoid repeating the diagnostics
```
/ RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage-in-libc-call \
// RUN:            -verify=num,manip,exam,mem,io,printf,scanf,other %s
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
// RUN:            -Wno-unsafe-buffer-usage-in-libc-call-string-examination \
// RUN:            -verify=num,manip,mem,io,printf,scanf,other %s
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage-in-libc-call-memory \
// RUN:            -verify=mem %s
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
// RUN:            -Wno-unsafe-buffer-usage-in-libc-call \
// RUN:            -Wunsafe-buffer-usage-in-libc-call-memory -verify=mem %s
```
test becomes:
```cpp
void calls(char *p, const char *q, const wchar_t *w, size_t n, FILE *f,
           int *x) {
  atoi(q);         // num-warning{{function 'atoi' is unsafe}}
  strcpy(p, q);    // manip-warning{{function 'strcpy' is unsafe}}
  strlen(q);       // exam-warning{{function 'strlen' is unsafe}}
  wcslen(w);       // exam-warning{{function 'wcslen' is unsafe}}
  memcpy(p, q, n); // mem-warning{{function 'memcpy' is unsafe}}
  memset(p, 0, n); // mem-warning{{function 'memset' is unsafe}}
  bzero(p, n);     // mem-warning{{function 'bzero' is unsafe}}
  fgets(p, 4, f);  // io-warning{{function 'fgets' is unsafe}}
  sprintf(p, "%d", 1); // printf-warning{{function 'sprintf' is unsafe}} \
                          printf-note{{change to 'snprintf' for explicit bounds checking}}
  sscanf(q, "%d", x);  // scanf-warning{{function 'sscanf' is unsafe}}
  qsort(p, n, 1, nullptr); // other-warning{{function 'qsort' is unsafe}}
}

void pragma(char *p, const char *q, size_t n) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call-string-examination"
  strlen(q);
  memcpy(p, q, n); // mem-warning{{function 'memcpy' is unsafe}}
#pragma clang diagnostic pop
  strlen(q); // exam-warning{{function 'strlen' is unsafe}}
}
```


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


More information about the cfe-commits mailing list