Repository navigation
[DRAFT] [pulse] tie c_str(), string mutators and string_view to the std::string buffer - #2194
Closed
VladimirMakaev wants to merge 4 commits into
Closed
VladimirMakaev wants to merge 4 commits into
VladimirMakaev wants to merge 4 commits into
Conversation
VladimirMakaev
force-pushed
the
pulse-basic-string-buffer
branch
from
October 4, 2026 00:54
37b145f to
875c948
Compare
VladimirMakaev
force-pushed
the
pulse-basic-string-buffer
branch
3 times, most recently
from
October 5, 2026 23:09
f17ebdf to
676f511
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.
VladimirMakaev
force-pushed
the
pulse-basic-string-buffer
branch
from
October 7, 2026 15:49
676f511 to
962c514
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, also on #2169 (review only the top commit; lands after #2209 and #2169)
Pulse invalidates the
data()buffer of a destroyedstd::basic_string, butc_str(),mutators and
std::string_viewwere not tied to it, so dangling pointers were missed:c_str()is now modelled likedata(). Mutators such asappend,erase,clearandoperator=invalidate the buffer (VECTOR_INVALIDATION, even afterreserve()) and return thereceiver when they return
basic_string&; views and string iterators point into the buffer. Asoperator=now records the write, a PULSE_CONST_REFABLE false positive on assigned by-valuestrings goes away.
Limitations: a by-value string whose
c_str()is only used in(void)ploses itsPULSE_CONST_REFABLE, as with
data();swap,getline,cbegin()/end()iterators andbuffer pointer arithmetic are not tracked.
Test plan
New tests in
cpp/pulse/basic_string.cpp,cpp/pulse/const_refable.cpp,cpp/pulse/taint/strings.cppandcpp/pulse-17/string_view.cpp, withFN_tests for thelimitations. Copies of mutator results in
taint/strings.cppare now reported asPULSE_UNNECESSARY_COPY_INTERMEDIATE, like the existing
operator+=one. The codetoanalyze testspass.