Repository navigation
[DRAFT] [pulse] model std::deque and the node-based containers of libc++ - #2180
Closed
VladimirMakaev wants to merge 5 commits into
Closed
VladimirMakaev wants to merge 5 commits into
VladimirMakaev wants to merge 5 commits into
Conversation
VladimirMakaev
force-pushed
the
pulse-std-node-containers
branch
4 times, most recently
from
October 5, 2026 18:13
9f512c5 to
428bcec
Compare
VladimirMakaev
force-pushed
the
pulse-std-node-containers
branch
from
October 5, 2026 21:55
428bcec to
663531c
Compare
VladimirMakaev
force-pushed
the
pulse-std-node-containers
branch
from
October 5, 2026 23:09
663531c to
f6be025
Compare
Pulse tied only `operator[]` and `at()` to the backing array of a `std::vector`, did not model
`erase()`, `resize()` or range `assign`/`insert`, and did not match `name<>` calls such as
`emplace_back()`, so uses of invalidated elements went unreported.
```cpp
int front_then_push_back(std::vector<int>& vec) {
int& first = vec.front();
vec.push_back(42);
return first; // Infer missed: vector invalidation
}
int erase_then_deref(std::vector<int>& vec) {
int* elt = &vec[1];
vec.erase(vec.begin());
return *elt; // Infer missed: vector invalidation
}
```
Model `front()`, `back()`, `data()` and the `emplace_back()` result; make `erase()`, growing
`resize()` and range `assign`/`insert` invalidate and update the size; model iterator `operator=`
and make `++`/`--` move only the stepped iterator, so `it = v.erase(it)` works and `--it; ++it`
from `end()` is still caught. Matching `name<>` in `ProcnameDispatcher` affects the models of
every checker; no other name newly matches.
Limitations: `erase()` invalidates every element, even before the erased position, and `resize()`
invalidates unless the vector provably does not grow (constructors such as `std::vector<int> v(n)`
leave the size unknown).
## Test plan
New tests in `cpp/pulse/vector.cpp`, `cpp/pulse/vector_iterator.cpp`,
`cpp/pulse/reference_stability.cpp` and `cpp/pulse-17/std_vector.cpp`, with `FP_`/`FN_` tests for
the limitations. All codetoanalyze tests pass; the new tests give the same results with libstdc++.
Pulse treated `std::find`, `std::lower_bound`, `std::next` and similar functions as unknown calls,
so the iterators they return were not tied to their vector and later reallocations went
unreported. Iterator arithmetic also kept the `end()` marking.
```cpp
int find_then_push_back(std::vector<int>& vec) {
auto it = std::find(vec.begin(), vec.end(), 3);
if (it == vec.end()) {
return 0;
}
vec.push_back(1);
return *it; // Infer missed: vector invalidation
}
int last(std::vector<int>& vec) {
return vec.empty() ? 0 : *(vec.end() - 1); // false positive: end() dereference
}
```
Model the algorithms that return a position without reordering the range as returning an unknown
position in the range of their first argument, propagating taint as unknown calls did. Model `+`,
`-`, `+=`, `-=`, `std::next`, `std::prev` and `std::advance` as shifting the position,
`operator->` like `operator*`, and `cbegin()`/`cend()`.
Limitations:
- these models have no "not found" case, so an unchecked `*std::find(...)` is not reported
- only `std::vector` and `std::string` iterators are matched, not reverse iterators, raw pointers
or other containers
- summaries of loops reading `it->x` now keep the elements read, as with `(*it).x`, which made
some of yaml-cpp's iterator summaries 4-7x larger
## Test plan
New tests in `cpp/pulse/std_algorithm.cpp`, `cpp/pulse/vector_iterator.cpp` and
`cpp/pulse/taint/strings.cpp`. The C, C++, Java, Kotlin and SIL codetoanalyze tests pass.
Since e1f88f2 (August 2024), callers passing null to a function that checks `p == NULL` and then dereferences `p` anyway get no NULLPTR_DEREFERENCE: the access fails on `ComparedToNullInThisProcedure` before `MustBeValid` is abduced. That commit deleted the caller tests showing this (`call_test_after_dereference_bad`, `no_early_return_no_zero_manifest_bad`). Java is hit hardest: bytecode spells every null check as `== null`. ```c #include <stddef.h> struct node { int data; }; void log_message(const char* msg); int get_data(struct node* n) { if (n == NULL) { log_message("null node"); } return n->data; } int caller(void) { return get_data(NULL); // Infer missed: null dereference } ``` When an access fails on `ComparedToNullInThisProcedure` or `ConstantDereference` and the pointer's history shows it was read from a pre-condition cell holding it, abduce `MustBeValid` as successful accesses do. All null values of a path share one canonical value: the check keeps a null local, or the result of an unknown call given a null argument, from blaming the caller. Limitations: with the opt-in COMPARED_TO_NULL_AND_DEREFERENCED, one bug yields two reports (callee and caller). ## Test plan New `c/pulse/compared_to_null.c`, with callers not blamed for a null local or unknown-call result (direct or through a wrapper), and an `FP_` test for an unknown noreturn function in the null branch. Callers restored in `cpp/pulse/nullptr.cpp` and `objc/pulse/null_deref/return_npe_test.m`, added in `java/pulse/NullPointerExceptions.java`. All suites pass; the ObjC line comes from that file alone.
…ng buffer
Pulse invalidates the `data()` buffer of a destroyed `std::basic_string`, but `c_str()`,
mutators and `std::string_view` were not tied to it, so dangling pointers were missed:
```cpp
std::string make_string();
char c_str_then_append_bad(std::string& s) {
const char* p = s.c_str();
s.append(1000, 'x');
return *p; // Infer missed: use of an invalidated buffer
}
char view_of_temporary_string_bad() {
std::string_view sv = make_string();
return *sv.data(); // Infer missed: use after delete
}
```
`c_str()` is now modelled like `data()`. Mutators such as `append`, `erase`, `clear` and
`operator=` invalidate the buffer (VECTOR_INVALIDATION, even after `reserve()`) and return the
receiver when they return `basic_string&`; views and string iterators point into the buffer. As
`operator=` now records the write, a PULSE_CONST_REFABLE false positive on assigned by-value
strings goes away.
Limitations: a by-value string whose `c_str()` is only used in `(void)p` loses its
PULSE_CONST_REFABLE, as with `data()`; `swap`, `getline`, `cbegin()`/`end()` iterators and
buffer pointer arithmetic are not tracked.
## Test plan
New tests in `cpp/pulse/basic_string.cpp`, `cpp/pulse/const_refable.cpp`,
`cpp/pulse/taint/strings.cpp` and `cpp/pulse-17/string_view.cpp`, with `FN_` tests for the
limitations. Copies of mutator results in `taint/strings.cpp` are now reported as
PULSE_UNNECESSARY_COPY_INTERMEDIATE, like the existing `operator+=` one. The codetoanalyze tests
pass.
Pulse models `std::vector` iterators but not those of `std::deque`, `std::list`, `std::map`, `std::set`, `std::unordered_map` and other node-based containers. It misses their misuses and, since an unknown `operator*` keeps returning the same element, reports deletion loops (meta#1884). ```cpp void delete_all(std::list<Node*>& nodes) { for (Node* node : nodes) delete node; // Infer reported a false USE_AFTER_DELETE } int find_in_empty_map() { std::map<int, int> map; return map.find(1)->second; // Infer missed: dereference of end() } ``` Model these containers and their libc++ iterators like `std::vector`: dereferencing `end()`, an iterator or reference invalidated by `erase()` or `clear()`, or a deque iterator used after an insertion is reported as VECTOR_INVALIDATION. The models that need no iterator, such as `size()`, `clear()`, `at()` and `front()`, also apply to libstdc++. Unknown calls havoc insert iterators such as `std::back_inserter`. Limitations: - libstdc++ iterators are not modelled, so meta#1884 remains there. - Not modelled: `std::forward_list`, `erase(key)`, range `erase()`, `extract()`, splicing, rehashing, deque pops, resizes and middle erasures. - False positives on containers known to be empty but filled where Pulse cannot see them. ## Test plan New tests in `cpp/pulse/std_containers.cpp` (libc++), including meta#1884 and unrelated bugs after loops, which stay manifest, and in `std_containers_libstdcxx.cpp` (libstdc++ declarations). `cpp/impurity/std_containers.cpp` checks that writes through elements are impure and reads are not. The codetoanalyze tests pass.
VladimirMakaev
force-pushed
the
pulse-std-node-containers
branch
from
October 7, 2026 15:49
f6be025 to
6c39a67
Compare
Contributor
Author
|
Will reopen once current PR queue is cleared |
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: #2181 → #2209 → #2194 → #2180, also on #2169 (review only the top commit; lands after #2194)
Pulse models
std::vectoriterators but not those ofstd::deque,std::list,std::map,std::set,std::unordered_mapand other node-based containers. It misses their misuses and,since an unknown
operator*keeps returning the same element, reports deletion loops (#1884).Model these containers and their libc++ iterators like
std::vector: dereferencingend(), aniterator or reference invalidated by
erase()orclear(), or a deque iterator used after aninsertion is reported as VECTOR_INVALIDATION. The models that need no iterator, such as
size(),clear(),at()andfront(), also apply to libstdc++. Unknown calls havoc insert iterators suchas
std::back_inserter.Limitations:
std::forward_list,erase(key), rangeerase(),extract(), splicing,rehashing, deque pops, resizes and middle erasures.
Test plan
New tests in
cpp/pulse/std_containers.cpp(libc++), including #1884 and unrelated bugs afterloops, which stay manifest, and in
std_containers_libstdcxx.cpp(libstdc++ declarations).cpp/impurity/std_containers.cppchecks that writes through elements are impure and reads are not.The codetoanalyze tests pass.