Repository navigation
Description
Summary
Regex::AsStr() returns an absl::string_view into the compiled pattern owned by the Regex object (regex_.as_str()). It has no ABSL_ATTRIBUTE_LIFETIME_BOUND. Calling it on the StatusOr<Regex> that Compile() returns compiles with no warning, and the view dangles as soon as the statement ends:
absl::string_view pattern = Regex::Compile(p)->AsStr(); // dangles
absl::string_view pattern = (*Regex::Compile(p)).AsStr(); // dangles
absl::string_view pattern = Regex::Compile(p).value().AsStr(); // danglesThis is the same class of bug fixed recently in xml (7f76098), exif (b2d80ab), deflate (4eb75b8) and zip (feed713). Open PR #199 is doing it for elf. regex.h and regex_flag.h have no lifetime annotations yet, and I couldn't find an open PR that touches them.
Affected accessors (at 47b1f4b)
| Accessor | Declared at | Returns | Dangles when |
|---|---|---|---|
Regex::AsStr() |
regex.h:346 | view into the pattern owned by regex_ |
called on any temporary Regex, including every Compile() result used inline |
Match::Inner() |
regex.h:201 | const& to the owned match_ |
called on a temporary Match |
RegexFlagWithOptions::pattern() |
regex_flag.h:126 | re_->AsStr() through a shared_ptr<const Regex> |
called on a temporary that is the last owner, for example RegexFlagWithOptions::OrDie(p).pattern() |
Match::AsStr() is fine: it points into the caller's text, and its comment says so.
Reproduction
I couldn't build the binding locally, because that needs a matched Crubit commit and Rust nightly. So this is a standalone reduction with the same shape as the real class: Compile() returns absl::StatusOr<Regex>, and AsStr() returns a view into an owned pattern. I built it against Abseil 20260107 with clang 21.1.8.
As shipped: -Wall -Wextra -Wdangling gives 0 warnings, and all three call styles are a heap-use-after-free under ASan:
$ ./repro_regex arrow # Regex::Compile(p)->AsStr()
ERROR: AddressSanitizer: heap-use-after-free #0 in main repro_regex.cc:44:25
$ ./repro_regex deref # (*Regex::Compile(p)).AsStr()
ERROR: AddressSanitizer: heap-use-after-free #0 in main repro_regex.cc:47:25
$ ./repro_regex value # Regex::Compile(p).value().AsStr()
ERROR: AddressSanitizer: heap-use-after-free #0 in main repro_regex.cc:50:25
With ABSL_ATTRIBUTE_LIFETIME_BOUND on AsStr() (-DFIXED): clang rejects all three, including the StatusOr::operator-> case:
repro_regex.cc:43:27: error: temporary whose address is used as value of local variable 'v' will be destroyed at the end of the full-expression [-Werror,-Wdangling]
repro_regex.cc:46:29: error: temporary whose address is used as value of local variable 'v' will be destroyed at the end of the full-expression [-Werror,-Wdangling]
repro_regex.cc:49:27: error: temporary whose address is used as value of local variable 'v' will be destroyed at the end of the full-expression [-Werror,-Wdangling]
repro_regex.cc
// Reduction of regex/regex.h at google/safe-bindings 47b1f4b.
// `Regex` owns its compiled pattern and `AsStr()` returns a view into it
// with no lifetime annotation, as at regex.h:346.
//
// clang++ -std=c++20 -fsanitize=address -g repro_regex.cc ... # bug
// clang++ -std=c++20 -DFIXED -Werror=dangling repro_regex.cc ... # fix
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#ifdef FIXED
#define LB ABSL_ATTRIBUTE_LIFETIME_BOUND
#else
#define LB
#endif
class Regex final {
public:
// Same signature as regex.h:337.
static absl::StatusOr<Regex> Compile(absl::string_view pattern) {
return Regex(std::string(pattern));
}
// regex.h:346: `absl::string_view AsStr() const { return regex_.as_str(); }`
absl::string_view AsStr() const LB { return pattern_; }
private:
explicit Regex(std::string p) : pattern_(std::move(p)) {}
std::string pattern_; // stands in for the Rust-owned pattern in `regex_`
};
// Long enough that std::string stores it on the heap.
constexpr char kPattern[] = "^(?P<user>[a-z0-9._%+-]+)@(?P<host>[a-z0-9.-]+)$";
int main(int argc, char** argv) {
const char* which = argc > 1 ? argv[1] : "";
if (!std::strcmp(which, "arrow")) {
absl::string_view v = Regex::Compile(kPattern)->AsStr();
std::printf("%c\n", v[0]);
} else if (!std::strcmp(which, "deref")) {
absl::string_view v = (*Regex::Compile(kPattern)).AsStr();
std::printf("%c\n", v[0]);
} else if (!std::strcmp(which, "value")) {
absl::string_view v = Regex::Compile(kPattern).value().AsStr();
std::printf("%c\n", v[0]);
} else {
std::fprintf(stderr, "usage: %s arrow|deref|value\n", argv[0]);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}clang++ -std=c++20 -fsanitize=address -g repro_regex.cc \
-labsl_statusor -labsl_status -labsl_strings -o repro_regex # bug
clang++ -std=c++20 -DFIXED -Werror=dangling -fsyntax-only repro_regex.cc # fix
Suggested fix
Do the same as the other bindings:
- Add
ABSL_ATTRIBUTE_LIFETIME_BOUNDtoRegex::AsStr(),Match::Inner()andRegexFlagWithOptions::pattern(). - Optionally, as in 7f76098 and [security/ise_memory_safety/elf] Constrain the lifetime of ELF bridge accessors #199, make them
const&-qualified and delete theconst&&overloads, so calling them on an rvalue is a hard error. - Note in the
AsStr()comment that the view is owned by theRegex, and that it may differ from the input pattern because of the RE2 rewrites.
Environment
- safe-bindings 47b1f4b
- Ubuntu, clang 21.1.8, libstdc++ 15, Abseil 20260107
Activity