Skip to content

Commit

Permalink
Use string_view::substr when making string_views out of other strings
Browse files Browse the repository at this point in the history
substr is both more convenient and also slightly safer when C++ hardening is enabled.

PiperOrigin-RevId: 697644082
  • Loading branch information
davidben authored and copybara-github committed Nov 18, 2024
1 parent ff243e7 commit 8cab599
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
3 changes: 1 addition & 2 deletions quiche/http2/adapter/oghttp2_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,7 @@ OgHttp2Session::SendResult OgHttp2Session::SendQueuedFrames() {
}
if (static_cast<size_t>(result) < frame.size()) {
// The frame was partially written, so the rest must be buffered.
buffered_data_.Append(
absl::string_view(frame.data() + result, frame.size() - result));
buffered_data_.Append(absl::string_view(frame).substr(result));
return SendResult::SEND_BLOCKED;
}
}
Expand Down
12 changes: 5 additions & 7 deletions quiche/quic/core/crypto/crypto_secret_boxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,12 @@ bool CryptoSecretBoxer::Unbox(absl::string_view in_ciphertext,
absl::ReaderMutexLock l(&lock_);
for (const bssl::UniquePtr<EVP_AEAD_CTX>& ctx : state_->ctxs) {
size_t bytes_written;
if (EVP_AEAD_CTX_open(ctx.get(),
reinterpret_cast<uint8_t*>(
const_cast<char*>(out_storage->data())),
&bytes_written, ciphertext_len, nonce,
kSIVNonceSize, ciphertext, ciphertext_len, nullptr,
0)) {
if (EVP_AEAD_CTX_open(
ctx.get(), reinterpret_cast<uint8_t*>(out_storage->data()),
&bytes_written, ciphertext_len, nonce, kSIVNonceSize, ciphertext,
ciphertext_len, nullptr, 0)) {
ok = true;
*out = absl::string_view(out_storage->data(), bytes_written);
*out = absl::string_view(*out_storage).substr(0, bytes_written);
break;
}

Expand Down
10 changes: 4 additions & 6 deletions quiche/quic/tools/quic_memory_cache_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void QuicMemoryCacheBackend::ResourceFile::Read() {
<< file_name_;
return;
}
file_contents_ = *maybe_file_contents;
file_contents_ = *std::move(maybe_file_contents);

// First read the headers.
for (size_t start = 0; start < file_contents_.length();) {
Expand All @@ -54,12 +54,11 @@ void QuicMemoryCacheBackend::ResourceFile::Read() {
if (file_contents_[pos - 1] == '\r') {
len -= 1;
}
absl::string_view line(file_contents_.data() + start, len);
auto line = absl::string_view(file_contents_).substr(start, len);
start = pos + 1;
// Headers end with an empty line.
if (line.empty()) {
body_ = absl::string_view(file_contents_.data() + start,
file_contents_.size() - start);
body_ = absl::string_view(file_contents_).substr(start);
break;
}
// Extract the status from the HTTP first line.
Expand Down Expand Up @@ -137,8 +136,7 @@ const QuicBackendResponse* QuicMemoryCacheBackend::GetResponse(
if (it == responses_.end()) {
uint64_t ignored = 0;
if (generate_bytes_response_) {
if (absl::SimpleAtoi(absl::string_view(path.data() + 1, path.size() - 1),
&ignored)) {
if (absl::SimpleAtoi(path.substr(1), &ignored)) {
// The actual parsed length is ignored here and will be recomputed
// by the caller.
return generate_bytes_response_.get();
Expand Down

0 comments on commit 8cab599

Please sign in to comment.