Skip to content

Commit

Permalink
#2730 Do not overrun put buffer in EventStreamBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyRyabinin committed Nov 11, 2023
1 parent 7ebc08a commit 8a0e861
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/aws-cpp-sdk-core/source/http/crt/CRTHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ namespace Aws
}

// When data is received from the content body of the incoming response, just copy it to the output stream.
assert(response);
response->GetResponseBody().write((const char*)body.ptr, static_cast<long>(body.len));
if (response->GetResponseBody().fail()) {
const auto& ref = response->GetResponseBody();
AWS_LOGSTREAM_ERROR(CRT_HTTP_CLIENT_TAG, "Failed to write " << body.len << " (eof: " << ref.eof() << ", bad: " << ref.bad() << ")");
}

if (request->IsEventStreamRequest() && !response->HasHeader(Aws::Http::X_AMZN_ERROR_TYPE))
{
Expand Down
19 changes: 18 additions & 1 deletion src/aws-cpp-sdk-core/source/utils/event/EventStreamBuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ namespace Aws
{
if (pptr() > pbase())
{
assert(epptr() >= pptr()); // check that we are in a put area
size_t length = static_cast<size_t>(pptr() - pbase());
m_decoder.Pump(m_byteBuffer, length);

if (!m_decoder)
{
m_err.write(reinterpret_cast<char*>(m_byteBuffer.GetUnderlyingData()), length);
if (m_err.fail()) {
AWS_LOGSTREAM_ERROR("EventStreamBuf",
"Failed to write " << length << " (eof: " << m_err.eof() << ", bad: " << m_err.bad() << ")");
}
}
else
{
Expand Down Expand Up @@ -120,13 +125,25 @@ namespace Aws

if (m_decoder)
{
if (pptr() == epptr()) // always the case by C++ standard, but someone may inherit from this class
{
writeToDecoder();
}
// writeToDecoder had to take some data from the put buffer but failed
if (pptr() == epptr())
{
AWS_LOGSTREAM_ERROR("EventStreamBuf", "Failed to decode EventStream event on char with int value: " << ch);
// let's just reset the put buffer and move on.
setp(pbase(), epptr() - 1);
}

// put char to the put area and advance current pptr
if (ch != eof)
{
*pptr() = (char)ch;
pbump(1);
}

writeToDecoder();
return ch;
}

Expand Down

0 comments on commit 8a0e861

Please sign in to comment.