[PATCH] D144136: Add a "remark" to report on array accesses

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 21 13:18:21 PST 2023


void added a comment.

In D144136#4137143 <https://reviews.llvm.org/D144136#4137143>, @kees wrote:

> Here's a test-case. I'd expect 6 remarks from building this:
>
>   /* Build with -Wall -O2 -fstrict-flex-arrays=3 -fsanitize=bounds -Rarray-bounds */
>   #include <stdint.h>
>   #include <stdio.h>
>   #include <string.h>
>   #include <malloc.h>
>   
>   #define report_size(p, index)      do {    \
>       const size_t bdos = __builtin_dynamic_object_size(p, 1); \
>       \
>       if (__builtin_constant_p(bdos)) { \
>           if (bdos == SIZE_MAX) { \
>               printf(#p " has unknowable size\n"); \
>           } else { \
>               printf(#p " has a fixed size: %zu\n", bdos); \
>           } \
>       } else { \
>           printf(#p " has a dynamic size: %zu\n", bdos); \
>       } \
>       printf(#p "[" #index "] assignment: %d\n", (p)[index] = 15); \
>   } while (0)
>   
>   struct fixed {
>       unsigned long flags;
>       size_t foo;
>       int array[16];
>   };
>   
>   /* should emit "fixed" */
>   void do_fixed(struct fixed *p, int index)
>   {
>       report_size(p->array, 0);
>       report_size(p->array, index);
>   }
>   
>   struct flex {
>       unsigned long flags;
>       size_t foo;
>       int array[];
>   };
>   
>   /* should emit "dynamic" */
>   void do_dynamic(unsigned char count, int index)
>   {
>       /* malloc() is marked with __attribute__((alloc_size(1))) */
>       struct flex *p = malloc(sizeof(*p) + count * sizeof(*p->array));
>       report_size(p->array, 0);
>       report_size(p->array, index);
>       free(p);
>   }
>   
>   /* should emit "unknowable" */
>   void do_unknown(struct flex *p, int index)
>   {
>       report_size(p->array, 0);
>       report_size(p->array, index);
>   }
>
> Currently, it only emits once for the compile-time known index with a compile-time known array size:
>
>   array.c:31:17: remark: accessing fixed sized array 'int[16]' by 0 [-Rarray-bounds]                  
>       report_size(p->array, 0);                                                                                       ^                                 

Right. I'll be working on the rest of these soon. Probably the FAM's will be next followed by the "dynamic" size, as that's trickier due to lack of support in Clang for some of the required features.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144136/new/

https://reviews.llvm.org/D144136



More information about the cfe-commits mailing list