Commit 417d7131 authored by Igor Sugak's avatar Igor Sugak Committed by Facebook Github Bot

fix multiple invalid-null-argument UBSAN errors in io/Cursor.h

Summary:
Exposed by UBSAN:
{P59121025}
{P59121310}
{P59121558}

Add appropriate checks to avoid the UB.

Reviewed By: yfeldblum

Differential Revision: D7112776

fbshipit-source-id: 82bd9f85ed4c82aa1b030a38d6ad9358e8f2d38a
parent 2806dda4
...@@ -413,6 +413,9 @@ class CursorBase { ...@@ -413,6 +413,9 @@ class CursorBase {
} }
void pull(void* buf, size_t len) { void pull(void* buf, size_t len) {
if (UNLIKELY(len == 0)) {
return;
}
dcheckIntegrity(); dcheckIntegrity();
if (LIKELY(crtPos_ + len <= crtEnd_)) { if (LIKELY(crtPos_ + len <= crtEnd_)) {
memcpy(buf, data(), len); memcpy(buf, data(), len);
...@@ -639,6 +642,12 @@ class CursorBase { ...@@ -639,6 +642,12 @@ class CursorBase {
} }
size_t pullAtMostSlow(void* buf, size_t len) { size_t pullAtMostSlow(void* buf, size_t len) {
// If the length of this buffer is 0 try advancing it.
// Otherwise on the first iteration of the following loop memcpy is called
// with a null source pointer.
if (UNLIKELY(length() == 0 && !tryAdvanceBuffer())) {
return 0;
}
uint8_t* p = reinterpret_cast<uint8_t*>(buf); uint8_t* p = reinterpret_cast<uint8_t*>(buf);
size_t copied = 0; size_t copied = 0;
for (size_t available; (available = length()) < len; ) { for (size_t available; (available = length()) < len; ) {
...@@ -1014,6 +1023,13 @@ class Appender : public detail::Writable<Appender> { ...@@ -1014,6 +1023,13 @@ class Appender : public detail::Writable<Appender> {
return 0; return 0;
} }
// If the length of this buffer is 0 try growing it.
// Otherwise on the first iteration of the following loop memcpy is called
// with a null source pointer.
if (UNLIKELY(length() == 0 && !tryGrowChain())) {
return 0;
}
size_t copied = 0; size_t copied = 0;
for (;;) { for (;;) {
// Fast path: it all fits in one buffer. // Fast path: it all fits in one buffer.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment