Skip to content

Commit

Permalink
Avoid invalid read when buffer is full
Browse files Browse the repository at this point in the history
nread is an unsigned value and was overflowing causing
buffer read issues
add null terminator at end only if full msg has been read

Signed-off-by: Tom Flynn <tom.flynn@gmail.com>
  • Loading branch information
tomflynn committed Mar 5, 2021
1 parent 5350f5a commit 8c769f3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 1 addition & 3 deletions libopflex/comms/CommunicationPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,7 @@ void CommunicationPeer::readBufNoNull(char* buffer, size_t nread) {
return;
}

buffer[nread++] = '\0';

while ((--nread > 0) && connected_) {
while ((nread != 0 && --nread > 0) && connected_) {
size_t chunk_size = readChunk(buffer);
if (chunk_size == 0) {
break;
Expand Down
4 changes: 4 additions & 0 deletions libopflex/comms/transport/PlainText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void Cb< PlainText >::on_read(uv_stream_t * h, ssize_t nread, uv_buf_t const * b
(buf->len > static_cast< size_t >(nread))
);
} else {
// add null terminator to indicate end of message
if (nread && buf->len != (size_t)nread) {
buf->base[nread++] = '\0';
}
peer->readBufNoNull(buf->base, nread);
}
}
Expand Down

0 comments on commit 8c769f3

Please sign in to comment.