[DRAFT] [inferbo] model std::array::data() and the size of std::array elements - #2208
Draft
VladimirMakaev wants to merge 2 commits into
Draft
VladimirMakaev wants to merge 2 commits into
VladimirMakaev wants to merge 2 commits into
Conversation
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
force-pushed
the
inferbo-std-array
branch
2 times, most recently
from
October 5, 2026 02:41
82171f5 to
069deec
Compare
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.
VladimirMakaev
force-pushed
the
inferbo-std-array
branch
from
October 5, 2026 06:55
069deec to
22868a4
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack: #2193 → #2208 (review only the top commit; lands after #2193)
Inferbo did not model
std::array::data()or know the size ofstd::arrayelements, so itmissed overruns through
data()and reported false positives onmemset/memcpyof a wholestd::array, onnew/mallocof one, and onstd::arrays reached through pointers orreferences into C arrays or vectors of them.
The
std::arraytype model now records the element size. Pointers tostd::arraycountstd::arrays, using the byte-unit symbols of #2193, and the models of its methods, now includingdata(), recount their receiver in elements and keep its offset.Limitations:
std::arrayfields of local structs are not modelled.Test plan
New tests in
cpp/bufferoverrun/std_array.cpp, withFN_andFP_tests for the overruns fromone
std::arrayof a C array into the next, C arrays in astd::arrayand vectors ofstd::arrays. The codetoanalyze tests pass; the example behaves the same with libstdc++.