Skip to content

Commit fd8a0e5

Browse files
ClaytonKnittelcopybara-github
authored andcommitted
Set an error on negative sizes in EpsCopyOutputStream::Skip and GetDirectBufferForNBytesAndAdvance
Previously these returned failure but left the stream usable, so callers that ignored the result could keep writing. Now the stream enters the error state, like other write failures. PiperOrigin-RevId: 1029507842
1 parent ccec463 commit fd8a0e5

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎src/google/protobuf/io/coded_stream.cc‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,10 @@ uint8_t* EpsCopyOutputStream::FlushAndResetBuffer(uint8_t* ptr) {
783783
}
784784

785785
bool EpsCopyOutputStream::Skip(int count, uint8_t** pp) {
786-
if (count < 0) return false;
786+
if (count < 0) {
787+
*pp = Error();
788+
return false;
789+
}
787790
if (had_error_) {
788791
*pp = buffer_;
789792
return false;
@@ -829,7 +832,10 @@ bool EpsCopyOutputStream::GetDirectBufferPointer(void** data, int* size,
829832

830833
uint8_t* EpsCopyOutputStream::GetDirectBufferForNBytesAndAdvance(int size,
831834
uint8_t** pp) {
832-
if (size < 0) return nullptr;
835+
if (size < 0) {
836+
*pp = Error();
837+
return nullptr;
838+
}
833839
if (had_error_) {
834840
*pp = buffer_;
835841
return nullptr;

‎src/google/protobuf/io/coded_stream.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,8 @@ class PROTOBUF_EXPORT PROTOBUF_FUTURE_ADD_EARLY_WARN_UNUSED CodedOutputStream {
11191119
void Trim() { cur_ = impl_.Trim(cur_); }
11201120

11211121
// Skips a number of bytes, leaving the bytes unmodified in the underlying
1122-
// buffer. Returns false if an underlying write error occurs. This is
1122+
// buffer. Returns false if an underlying write error occurs or `count` is
1123+
// negative; a negative `count` puts the stream in an error state. This is
11231124
// mainly useful with GetDirectBufferPointer().
11241125
// Note of caution, the skipped bytes may contain uninitialized data. The
11251126
// caller must make sure that the skipped bytes are properly initialized,
@@ -1145,9 +1146,10 @@ class PROTOBUF_EXPORT PROTOBUF_FUTURE_ADD_EARLY_WARN_UNUSED CodedOutputStream {
11451146
// returns a pointer directly into the buffer and advances over these bytes.
11461147
// The caller may then write directly into this buffer (e.g. using the
11471148
// *ToArray static methods) rather than go through CodedOutputStream. If
1148-
// there are not enough bytes available, returns NULL. The return pointer is
1149-
// invalidated as soon as any other non-const method of CodedOutputStream
1150-
// is called.
1149+
// there are not enough bytes available, returns NULL. If `size` is
1150+
// negative, returns NULL and puts the stream in an error state. The return
1151+
// pointer is invalidated as soon as any other non-const method of
1152+
// CodedOutputStream is called.
11511153
PROTOBUF_FUTURE_ADD_EARLY_NODISCARD uint8_t*
11521154
GetDirectBufferForNBytesAndAdvance(int size) {
11531155
return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_);

‎src/google/protobuf/io/coded_stream_unittest.cc‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,43 @@ TEST_F(CodedStreamTest, GetDirectBufferPointerInlineInput) {
13111311
EXPECT_EQ(0, size);
13121312
}
13131313

1314+
TEST_F(CodedStreamTest, GetDirectBufferForNBytesAndAdvanceNegativeSizeErrors) {
1315+
for (int size : {-16, INT_MIN}) {
1316+
std::vector<uint8_t> out(32, 0);
1317+
ArrayOutputStream output(out.data(), static_cast<int>(out.size()));
1318+
CodedOutputStream coded_output(&output);
1319+
1320+
EXPECT_EQ(coded_output.GetDirectBufferForNBytesAndAdvance(size), nullptr);
1321+
EXPECT_TRUE(coded_output.HadError());
1322+
}
1323+
}
1324+
1325+
TEST_F(CodedStreamTest, SkipOutput) {
1326+
std::vector<uint8_t> out(32, 'x');
1327+
ArrayOutputStream output(out.data(), static_cast<int>(out.size()));
1328+
CodedOutputStream coded_output(&output);
1329+
1330+
coded_output.WriteRaw("ab", 2);
1331+
EXPECT_TRUE(coded_output.Skip(2));
1332+
coded_output.WriteRaw("cd", 2);
1333+
coded_output.Trim();
1334+
EXPECT_FALSE(coded_output.HadError());
1335+
EXPECT_EQ(coded_output.ByteCount(), 6);
1336+
EXPECT_EQ(absl::string_view(reinterpret_cast<const char*>(out.data()), 6),
1337+
"abxxcd");
1338+
}
1339+
1340+
TEST_F(CodedStreamTest, SkipOutputNegativeCountErrors) {
1341+
for (int count : {-1, INT_MIN}) {
1342+
std::vector<uint8_t> out(32, 0);
1343+
ArrayOutputStream output(out.data(), static_cast<int>(out.size()));
1344+
CodedOutputStream coded_output(&output);
1345+
1346+
EXPECT_FALSE(coded_output.Skip(count));
1347+
EXPECT_TRUE(coded_output.HadError());
1348+
}
1349+
}
1350+
13141351
// -------------------------------------------------------------------
13151352
// Limits
13161353

0 commit comments

Comments
 (0)