From 7d7d796cd09dfaed3aed5cd1f973b86f8c2d79bb Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 27 Jun 2019 14:11:31 -0700 Subject: [PATCH] Fix undefined behavior. Closes #5422 (#5423) * Fix undefined behavior. Closes #5422 * Move check into callers of make_space --- include/flatbuffers/flatbuffers.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index a831163af..02a191047 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -880,7 +880,7 @@ class vector_downward { uint8_t *data_at(size_t offset) const { return buf_ + reserved_ - offset; } void push(const uint8_t *bytes, size_t num) { - memcpy(make_space(num), bytes, num); + if (num > 0) { memcpy(make_space(num), bytes, num); } } // Specialized version of push() that avoids memcpy call for small data. @@ -903,6 +903,7 @@ class vector_downward { } // Version for when we know the size is larger. + // Precondition: zero_pad_bytes > 0 void fill_big(size_t zero_pad_bytes) { memset(make_space(zero_pad_bytes), 0, zero_pad_bytes); }