diff --git a/include/nlohmann/detail/view/builder.hpp b/include/nlohmann/detail/view/builder.hpp index 1722dcc34..ce0336549 100644 --- a/include/nlohmann/detail/view/builder.hpp +++ b/include/nlohmann/detail/view/builder.hpp @@ -836,7 +836,10 @@ indent_done: NLOHMANN_VIEW_ALWAYS_INLINE void open(value_t k) { - const auto idx = static_cast(emit(k, 0, 0, static_cast(p - b), 0) - base); + // (base is read after emit(), which moves the node array when it + // grows; in one expression the order of the two is unspecified) + const node* const n = emit(k, 0, 0, static_cast(p - b), 0); + const auto idx = static_cast(n - base); if (depth != 0) { if (NLOHMANN_VIEW_LIKELY(depth <= 64)) diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index cdae60903..9338f691c 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -1981,7 +1981,10 @@ indent_done: NLOHMANN_VIEW_ALWAYS_INLINE void open(value_t k) { - const auto idx = static_cast(emit(k, 0, 0, static_cast(p - b), 0) - base); + // (base is read after emit(), which moves the node array when it + // grows; in one expression the order of the two is unspecified) + const node* const n = emit(k, 0, 0, static_cast(p - b), 0); + const auto idx = static_cast(n - base); if (depth != 0) { if (NLOHMANN_VIEW_LIKELY(depth <= 64))