[all-commits] [llvm/llvm-project] 9a1e63: [-Wunsafe-buffer-usage] Ignore constant safe indic...

jkorous-apple via All-commits all-commits at lists.llvm.org
Wed Feb 14 18:16:56 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 9a1e6373ab3edc38486af504154db2d804e72d3d
      https://github.com/llvm/llvm-project/commit/9a1e6373ab3edc38486af504154db2d804e72d3d
  Author: jkorous-apple <32549412+jkorous-apple at users.noreply.github.com>
  Date:   2024-02-14 (Wed, 14 Feb 2024)

  Changed paths:
    M clang/lib/Analysis/UnsafeBufferUsage.cpp
    M clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
    M clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
    M clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-arg-to-func-ptr-call.cpp
    M clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp

  Log Message:
  -----------
  [-Wunsafe-buffer-usage] Ignore constant safe indices in array subscripts (#80504)

[-Wunsafe-buffer-usage] Ignore safe array subscripts
Don't emit warnings for array subscripts on constant size arrays where the index is constant and within bounds.

Example:
int arr[10];
arr[5] = 0; //safe, no warning

This patch recognizes only array indices that are integer literals - it doesn't understand more complex expressions (arithmetic on constants, etc.).

-Warray-bounds implemented in Sema::CheckArrayAccess() already solves a similar
(opposite) problem, handles complex expressions and is battle-tested.

Adding -Wunsafe-buffer-usage diagnostics to Sema is a non-starter as we need to emit
both the warnings and fixits and the performance impact of the fixit machine is
unacceptable for Sema.

CheckArrayAccess() as is doesn't distinguish between "safe" and "unknown" array
accesses. It also mixes the analysis that decides if an index is out of bounds
with crafting the diagnostics.

A refactor of CheckArrayAccess() might serve both the original purpose
and help us avoid false-positive with -Wunsafe-buffer-usage on constant
size arrrays.




More information about the All-commits mailing list