Repository navigation
[DRAFT] [inferbo] give global and static arrays their declared sizes - #2192
Draft
VladimirMakaev wants to merge 1 commit into
Draft
VladimirMakaev wants to merge 1 commit into
VladimirMakaev wants to merge 1 commit into
Conversation
VladimirMakaev
force-pushed
the
inferbo-global-arrays
branch
from
October 3, 2026 21:52
90607cd to
4bdda32
Compare
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-global-arrays
branch
from
October 5, 2026 02:40
4bdda32 to
87ab95b
Compare
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-global-arrays
branch
from
October 5, 2026 06:54
87ab95b to
b2bb3cb
Compare
VladimirMakaev
added a commit
to VladimirMakaev/infer
that referenced
this pull request
Oct 5, 2026
Inferbo knew a global array's size only from its initializer, so it missed overruns of uninitialized global arrays (facebook#949), of static local arrays and of array fields of global structs. A store `g[i] = v` also overwrote the whole array value of `g`. Upstream-PR: facebook#2192 PR-Head: b2bb3cb PR-Base: 9cc2641
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 global,
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] = "";`. With facebook#2192, this fixes facebook#1444.
## Test plan
New tests in `c/bufferoverrun` and `cpp/bufferoverrun`; `FN_call_access_index_4_on_S2_Bad` and
`FN_pass_global_struct_array_field_Bad` are now reported and renamed. The codetoanalyze tests
pass. With `--bufferoverrun` on 13 open-source C/C++ projects, the reports are unchanged.
Inferbo knew a global array's size only from its initializer, so it missed overruns of uninitialized global arrays (facebook#949), of static local arrays and of array fields of global structs. A store `g[i] = v` also overwrote the whole array value of `g`. ```c int arr[50]; void write_all() { for (int i = 0; i <= 50; i++) { arr[i] = 1; // Infer missed: buffer overrun } } ``` The clang frontend records the declared types of the globals each procedure references. Inferbo gives these arrays symbolic contents of that size, stores update their elements, a row `g[i]` used as `int*` points to its first element, and only `const` arrays take their contents from their initializer. Limitations: - array fields of global structs passed to calls or `strcpy` need facebook#2185 too (together: facebook#1444); - elements are weak (`if (g[1] > 3)` bounds no later read) and conditions on them are reported in each caller, as for other globals; - pointers to rows (`int (*q)[4] = &g[1]; (*q)[3]`) keep Inferbo's imprecision for local arrays. ## Test plan New tests in `c/bufferoverrun/global.c`, `cpp/bufferoverrun/global.cpp`, `cpp/bufferoverrun/std_array.cpp` and `c/performance/loops.c`; `issue949_bad_FN` becomes `issue949_bad`. The addresses of two mutable global arrays are no longer known to differ, which adds 1 to the cost of `loop_use_global_vars` (C and ObjC). The C/C++ codetoanalyze tests pass.
VladimirMakaev
force-pushed
the
inferbo-global-arrays
branch
from
October 7, 2026 15:48
b2bb3cb to
484de98
Compare
This was referenced Oct 7, 2026
Draft
VladimirMakaev
added a commit
to VladimirMakaev/infer
that referenced
this pull request
Oct 7, 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 global,
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] = "";`. With facebook#2192, this fixes facebook#1444.
## Test plan
New tests in `c/bufferoverrun` and `cpp/bufferoverrun`; `FN_call_access_index_4_on_S2_Bad` and
`FN_pass_global_struct_array_field_Bad` are now reported and renamed. The codetoanalyze tests
pass. With `--bufferoverrun` on 13 open-source C/C++ projects, the reports are unchanged.
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.
Inferbo knew a global array's size only from its initializer, so it missed overruns of
uninitialized global arrays (#949), of static local arrays and of array fields of global structs.
A store
g[i] = valso overwrote the whole array value ofg.The clang frontend records the declared types of the globals each procedure references.
Inferbo gives these arrays symbolic contents of that size, stores update their elements, a row
g[i]used asint*points to its first element, and onlyconstarrays take their contentsfrom their initializer.
Limitations:
strcpyneed [DRAFT] [inferbo] bounds-check array fields of local structs and objects #2185 too (together: Array size in struct or global scope not captured? #1444);if (g[1] > 3)bounds no later read) and conditions on them are reportedin each caller, as for other globals;
int (*q)[4] = &g[1]; (*q)[3]) keep Inferbo's imprecision for local arrays.Test plan
New tests in
c/bufferoverrun/global.c,cpp/bufferoverrun/global.cpp,cpp/bufferoverrun/std_array.cppandc/performance/loops.c;issue949_bad_FNbecomesissue949_bad. The addresses of two mutable global arrays are no longer known to differ, whichadds 1 to the cost of
loop_use_global_vars(C and ObjC). The C/C++ codetoanalyze tests pass.