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
diff --git a/src/google/protobuf/io/coded_stream.cc b/src/google/protobuf/io/coded_stream.cc index b542755..586cff8 100644 --- a/src/google/protobuf/io/coded_stream.cc +++ b/src/google/protobuf/io/coded_stream.cc
@@ -783,7 +783,10 @@ } bool EpsCopyOutputStream::Skip(int count, uint8_t** pp) { - if (count < 0) return false; + if (count < 0) { + *pp = Error(); + return false; + } if (had_error_) { *pp = buffer_; return false; @@ -829,7 +832,10 @@ uint8_t* EpsCopyOutputStream::GetDirectBufferForNBytesAndAdvance(int size, uint8_t** pp) { - if (size < 0) return nullptr; + if (size < 0) { + *pp = Error(); + return nullptr; + } if (had_error_) { *pp = buffer_; return nullptr;
diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index a36a880..693bea5 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h
@@ -1119,7 +1119,8 @@ void Trim() { cur_ = impl_.Trim(cur_); } // Skips a number of bytes, leaving the bytes unmodified in the underlying - // buffer. Returns false if an underlying write error occurs. This is + // buffer. Returns false if an underlying write error occurs or `count` is + // negative; a negative `count` puts the stream in an error state. This is // mainly useful with GetDirectBufferPointer(). // Note of caution, the skipped bytes may contain uninitialized data. The // caller must make sure that the skipped bytes are properly initialized, @@ -1145,9 +1146,10 @@ // returns a pointer directly into the buffer and advances over these bytes. // The caller may then write directly into this buffer (e.g. using the // *ToArray static methods) rather than go through CodedOutputStream. If - // there are not enough bytes available, returns NULL. The return pointer is - // invalidated as soon as any other non-const method of CodedOutputStream - // is called. + // there are not enough bytes available, returns NULL. If `size` is + // negative, returns NULL and puts the stream in an error state. The return + // pointer is invalidated as soon as any other non-const method of + // CodedOutputStream is called. PROTOBUF_FUTURE_ADD_EARLY_NODISCARD uint8_t* GetDirectBufferForNBytesAndAdvance(int size) { return impl_.GetDirectBufferForNBytesAndAdvance(size, &cur_);
diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index 1959f57..327e0fa 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc
@@ -1311,6 +1311,43 @@ EXPECT_EQ(0, size); } +TEST_F(CodedStreamTest, GetDirectBufferForNBytesAndAdvanceNegativeSizeErrors) { + for (int size : {-16, INT_MIN}) { + std::vector<uint8_t> out(32, 0); + ArrayOutputStream output(out.data(), static_cast<int>(out.size())); + CodedOutputStream coded_output(&output); + + EXPECT_EQ(coded_output.GetDirectBufferForNBytesAndAdvance(size), nullptr); + EXPECT_TRUE(coded_output.HadError()); + } +} + +TEST_F(CodedStreamTest, SkipOutput) { + std::vector<uint8_t> out(32, 'x'); + ArrayOutputStream output(out.data(), static_cast<int>(out.size())); + CodedOutputStream coded_output(&output); + + coded_output.WriteRaw("ab", 2); + EXPECT_TRUE(coded_output.Skip(2)); + coded_output.WriteRaw("cd", 2); + coded_output.Trim(); + EXPECT_FALSE(coded_output.HadError()); + EXPECT_EQ(coded_output.ByteCount(), 6); + EXPECT_EQ(absl::string_view(reinterpret_cast<const char*>(out.data()), 6), + "abxxcd"); +} + +TEST_F(CodedStreamTest, SkipOutputNegativeCountErrors) { + for (int count : {-1, INT_MIN}) { + std::vector<uint8_t> out(32, 0); + ArrayOutputStream output(out.data(), static_cast<int>(out.size())); + CodedOutputStream coded_output(&output); + + EXPECT_FALSE(coded_output.Skip(count)); + EXPECT_TRUE(coded_output.HadError()); + } +} + // ------------------------------------------------------------------- // Limits