Skip to content

Commit 8ee7bea

Browse files
juergwcopybara-github
authored andcommitted
Add additional checks to tink/python/cc's streaming AEAD output buffers.
These checks are not really needed, but I think it's better to add them. PiperOrigin-RevId: 766178948 Change-Id: I4c24a1aea531d08d16bb2b6bb3ad72756cf240ea
1 parent 954a543 commit 8ee7bea

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

tink/cc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ cc_library(
107107
hdrs = ["output_stream_adapter.h"],
108108
include_prefix = "tink/cc",
109109
deps = [
110+
"@com_google_absl//absl/log:check",
110111
"@com_google_absl//absl/status",
111112
"@com_google_absl//absl/status:statusor",
112113
"@com_google_absl//absl/strings",

tink/cc/output_stream_adapter.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdint>
2121
#include <cstring>
2222

23+
#include "absl/log/check.h"
2324
#include "absl/status/status.h"
2425
#include "absl/status/statusor.h"
2526
#include "absl/strings/string_view.h"
@@ -40,6 +41,7 @@ absl::StatusOr<int64_t> OutputStreamAdapter::Write(absl::string_view data) {
4041
if (write_count < available) stream_->BackUp(available - write_count);
4142
written += write_count;
4243
}
44+
CHECK(written == data.size());
4345
return written;
4446
}
4547

tink/cc/python_output_stream.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ absl::StatusOr<int> PythonOutputStream::Next(void** data) {
7373

7474
// Some data was written, so we can return some portion of buffer_.
7575
int written = write_result.value();
76+
77+
// This should not happen, because the only implementation of
78+
// PythonFileObjectAdapter we have is FileObjectAdapter in
79+
// _file_object_adapter.py, which never returns a value larger than the buffer
80+
// size.
81+
if (written > buffer_.size()) {
82+
return status_ = absl::Status(absl::StatusCode::kInternal,
83+
"Invalid value returned by Write");
84+
}
85+
7686
position_ += written;
7787
count_in_buffer_ = buffer_.size();
7888
buffer_offset_ = buffer_.size() - written;

0 commit comments

Comments
 (0)