[-Wunsafe-buffer-usage] Add Fixable for simple pointer dereference
authorMalavikaSamak <malavika2@apple.com>
Wed, 22 Mar 2023 22:31:00 +0000 (15:31 -0700)
committerMalavikaSamak <malavika2@apple.com>
Wed, 22 Mar 2023 22:32:51 +0000 (15:32 -0700)
commite7596a99fca6d1df14275f5293e447a4d87af06a
treef88cdb33bbda8d620353f4f2cde2fda96e995532
parent201fdef40dd6ec193d18d39638454a3c972f1fec
[-Wunsafe-buffer-usage] Add Fixable for simple pointer dereference

This patch introduces PointerDereferenceGadget, a FixableGadget that emits
fixits to handle cases where a pointer that is identified as unsafe is
dereferenced. The current implementation only handles cases where the strategy
is to change the type of the raw pointer to std::span. The fixit for this
strategy is to fetch the first element from the corresponding span instance.

For example for the code below, the PointerDereferenceGadget emits a fixit for
S3 (S1, S2 are to be handled by other gadgets):

  S1: int *ptr = new int[10];
  S2: int val1 = ptr[k]; // Unsafe operation
  S3: int val2 = *ptr; => Fixit: int val2 = ptr[0];

Differential revision: https://reviews.llvm.org/D143206
clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-deref.cpp [new file with mode: 0644]