Skip to content

[DRAFT] [pulse] tie c_str(), string mutators and string_view to the std::string buffer - #2194

Closed
VladimirMakaev wants to merge 4 commits into
meta:mainfrom
VladimirMakaev:pulse-basic-string-buffer
Closed

VladimirMakaev wants to merge 4 commits into
meta:mainfrom
VladimirMakaev:pulse-basic-string-buffer

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Stack: #2181 → #2209 → #2194, also on #2169 (review only the top commit; lands after #2209 and #2169)

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:

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 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
VladimirMakaev force-pushed the pulse-basic-string-buffer branch from 676f511 to 962c514 Compare October 7, 2026 15:49
@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

Will reopen once current PR queue is cleared

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