Skip to content

[DRAFT] [inferbo] model std::array::data() and the size of std::array elements - #2208

Draft
VladimirMakaev wants to merge 2 commits into
facebook:mainfrom
VladimirMakaev:inferbo-std-array
Draft

VladimirMakaev wants to merge 2 commits into
facebook:mainfrom
VladimirMakaev:inferbo-std-array

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack: #2193 → #2208 (review only the top commit; lands after #2193)

Inferbo did not model std::array::data() or know the size of std::array elements, so it
missed overruns through data() and reported false positives on memset/memcpy of a whole
std::array, on new/malloc of one, and on std::arrays reached through pointers or
references into C arrays or vectors of them.

#include <array>
#include <cstring>

void f(const char* src) {
  std::array<int, 4> a;
  a.data()[4] = 0; // Infer missed: buffer overrun
  memcpy(a.data(), src, 20); // Infer missed: buffer overrun
  auto* p = new std::array<int, 4>;
  (*p)[3] = 0; // false positive: buffer overrun reported here
  delete p;
}

The std::array type model now records the element size. Pointers to std::array count
std::arrays, using the byte-unit symbols of #2193, and the models of its methods, now including
data(), recount their receiver in elements and keep its offset.

Limitations: std::array fields of local structs are not modelled.

Test plan

New tests in cpp/bufferoverrun/std_array.cpp, with FN_ and FP_ tests for the overruns from
one std::array of a C array into the next, C arrays in a std::array and vectors of
std::arrays. The codetoanalyze tests pass; the example behaves the same with libstdc++.

@meta-cla meta-cla Bot added the CLA Signed label Oct 3, 2026
VladimirMakaev added a commit to VladimirMakaev/infer that referenced this pull request Oct 3, 2026
Inferbo declared no arrays for the array fields of local structs and C++ objects, so their
accesses went unchecked. Array fields passed to functions or models, as in `g(s.arr)` or
`strcpy(p->name, src)`, evaluated to the field's cell instead of its array, also for heap and
parameter structs.

```c
#include <string.h>

struct S {
  int arr[4];
  char name[8];
};

void f(void) {
  struct S s;
  s.arr[4] = 0; // Infer missed: buffer overrun
  strcpy(s.name, "12345678"); // Infer missed: buffer overrun
}
```

Locals now get arrays for their nested array fields, except trailing arrays of length 0 or 1,
which may be flexible. Array fields decay to their arrays in calls and models. String literals
and struct copies are copied into the destination's arrays instead of replacing them, which
removes false positives on initializers such as `struct S s = {{0}, "abc"};` or a C++
`char name[16] = "";`. Part of facebook#1444, with facebook#2192. facebook#2208's `eval_array` replaces `eval_arg` only
in the `StdArray` models.

## Test plan

New tests in `c/bufferoverrun` and `cpp/bufferoverrun`; `FN_call_access_index_4_on_S2_Bad` is
now reported and renamed. The codetoanalyze tests pass. With `--bufferoverrun-only`: lua
(without `lgc.c`) -2 INTEGER_OVERFLOW_L2, civetweb -1 BUFFER_OVERRUN_L3, libuv +1 runtime-safe
BUFFER_OVERRUN_L2 (as for local arrays); zlib, cJSON, libyaml and hiredis unchanged.
@VladimirMakaev
VladimirMakaev force-pushed the inferbo-std-array branch 2 times, most recently from 82171f5 to 069deec Compare October 5, 2026 02:41
Inferbo gave integer pointer parameters a stride in bits and counted the offset and size behind
`void*` parameters in caller elements, missing overruns in helpers taking a pointer and a length.

```c
#include <stdint.h>
#include <string.h>

void zero(uint8_t* p, size_t n) { memset(p, 0, n); }
void set_byte(void* p, int i) { ((char*)p)[i] = 0; }

void f(void) {
  uint8_t b[16];
  zero(b, 17); // Infer missed: buffer overrun
  int w[16];
  set_byte(w, 64); // Infer missed: buffer overrun
}
```

Offsets and sizes behind pointer parameters now count bytes or n-byte units. strncpy reads its
source only up to a known string length, which raw writes and unknown calls forget, also through
reachable pointers. Substituting `c * x` for an unsigned `x` and a negative `c` keeps the term.
The frontend gives `char8_t`, `char16_t`, `char32_t`, `wchar_t` and `__int128` their real integer
types, also fixing their ranges in Pulse.

Limitations:
- A pointer to the first element of a 2D array only knows the first row.
- Byte units expose existing imprecision: 36 new false positives on lua (7 other reports gone,
  zlib loses one), 8 on leveldb, pugixml, tinyxml2 and workflow.
- Analyzing pugixml takes 15% more instructions.

## Test plan
New tests in `c/bufferoverrun/stride.c`, `c/bufferoverrun/void_ptr.c`,
`cpp/bufferoverrun/stride.cpp` and `cpp/frontend-20/char8.cpp`, new strncpy tests in
`c/bufferoverrun/models.c`, and `call_casting_void_ptr_Bad` in `cpp/bufferoverrun/void_ptr.cpp`.
The codetoanalyze tests pass. Counts above: `--bufferoverrun-only` on lua 5.4.6 (without `lgc.c`
and `luac.c`) and zlib 1.3.1, `--bufferoverrun` on 13 C++ projects.
Inferbo did not model `std::array::data()` or know the size of `std::array` elements, so it
missed overruns through `data()` and reported false positives on `memset`/`memcpy` of a whole
`std::array`, on `new`/`malloc` of one, and on `std::array`s reached through pointers or
references into C arrays or vectors of them.

```cpp
#include <array>
#include <cstring>

void f(const char* src) {
  std::array<int, 4> a;
  a.data()[4] = 0; // Infer missed: buffer overrun
  memcpy(a.data(), src, 20); // Infer missed: buffer overrun
  auto* p = new std::array<int, 4>;
  (*p)[3] = 0; // false positive: buffer overrun reported here
  delete p;
}
```

The `std::array` type model now records the element size. Pointers to `std::array` count
`std::array`s, using the byte-unit symbols of facebook#2193, and the models of its methods, now including
`data()`, recount their receiver in elements and keep its offset.

Limitations: `std::array` fields of local structs are not modelled.

## Test plan
New tests in `cpp/bufferoverrun/std_array.cpp`, with `FN_` and `FP_` tests for the overruns from
one `std::array` of a C array into the next, C arrays in a `std::array` and vectors of
`std::array`s. The codetoanalyze tests pass; the example behaves the same with libstdc++.
VladimirMakaev added a commit to VladimirMakaev/infer that referenced this pull request Oct 5, 2026
Inferbo declared no arrays for the array fields of local structs and C++ objects, so their
accesses went unchecked. Array fields passed to functions or models, as in `g(s.arr)` or
`strcpy(p->name, src)`, evaluated to the field's cell instead of its array, also for heap and
parameter structs.

```c
#include <string.h>

struct S {
  int arr[4];
  char name[8];
};

void f(void) {
  struct S s;
  s.arr[4] = 0; // Infer missed: buffer overrun
  strcpy(s.name, "12345678"); // Infer missed: buffer overrun
}
```

Locals now get arrays for their nested array fields, except trailing arrays of length 0 or 1,
which may be flexible. Array fields decay to their arrays in calls and models. String literals
and struct copies are copied into the destination's arrays instead of replacing them, which
removes false positives on initializers such as `struct S s = {{0}, "abc"};` or a C++
`char name[16] = "";`. Part of facebook#1444, with facebook#2192. facebook#2208's `eval_array` replaces `eval_arg` only
in the `StdArray` models.

## Test plan

New tests in `c/bufferoverrun` and `cpp/bufferoverrun`; `FN_call_access_index_4_on_S2_Bad` is
now reported and renamed. The codetoanalyze tests pass. With `--bufferoverrun-only`: lua
(without `lgc.c`) -2 INTEGER_OVERFLOW_L2, civetweb -1 BUFFER_OVERRUN_L3, libuv +1 runtime-safe
BUFFER_OVERRUN_L2 (as for local arrays); zlib, cJSON, libyaml and hiredis unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant