From c43a0beff0ab4e1d5c5fe5044dfb30f24974eaff Mon Sep 17 00:00:00 2001 From: joligarson <33656824+joligarson@users.noreply.github.com> Date: Fri, 18 May 2018 18:15:20 +0200 Subject: [PATCH] Fix undertermined execution behavior (#4751) Fix for the issue #4744: Ambiguous side-effect execution on vector_downward::make_space() method. C++ does not impose evaluation order on the two expressions on the right side of the assignment, so compiler can freely decide. As ensure_space() method can change the value of "cur_" variable, the result of the subtraction may be different depending on the evaluation order, which is ambiguous in C++. In order to make this code deterministic and correct, cur_ must be evaluated after ensure_space() is called. --- 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 e513e7938..da1c2c9c2 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -601,7 +601,8 @@ class vector_downward { } inline uint8_t *make_space(size_t len) { - cur_ -= ensure_space(len); + size_t space = ensure_space(len); + cur_ -= space; return cur_; }