Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid memory access in loops due to missing pointer stability when using INJA_DATA_TYPE=nlohmann::ordered_json #290

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ if(BUILD_TESTING AND INJA_BUILD_TESTS)
target_link_libraries(single_inja_test PRIVATE single_inja)

add_test(single_inja_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/single_inja_test)

add_executable(single_inja_ordered_test test/test.cpp)
target_link_libraries(single_inja_ordered_test PRIVATE single_inja)
target_compile_definitions(single_inja_ordered_test PUBLIC INJA_DATA_TYPE=nlohmann::ordered_json)

add_test(single_inja_ordered_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/single_inja_ordered_test)
endif()


Expand Down
45 changes: 30 additions & 15 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class Renderer : public NodeVisitor {
std::ostream* output_stream;

json additional_data;
json* current_loop_data = &additional_data["loop"];

json* get_current_loop_data() {
return &additional_data["loop"];
}

std::vector<std::shared_ptr<json>> data_tmp_stack;
std::stack<const json*> data_eval_stack;
Expand Down Expand Up @@ -485,17 +488,24 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an array", node);
}

if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.value)] = *it;

json* const current_loop_data = get_current_loop_data();

(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -510,11 +520,11 @@ class Renderer : public NodeVisitor {
}

additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
const auto tmp = (*current_loop_data)["parent"];
*current_loop_data = std::move(tmp);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand All @@ -524,17 +534,23 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an object", node);
}

if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.key)] = it.key();
additional_data[static_cast<std::string>(node.value)] = it.value();

json* const current_loop_data = get_current_loop_data();
(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -550,10 +566,10 @@ class Renderer : public NodeVisitor {

additional_data[static_cast<std::string>(node.key)].clear();
additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
*current_loop_data = std::move((*current_loop_data)["parent"]);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand Down Expand Up @@ -618,7 +634,6 @@ class Renderer : public NodeVisitor {
data_input = &data;
if (loop_data) {
additional_data = *loop_data;
current_loop_data = &additional_data["loop"];
}

template_stack.emplace_back(current_template);
Expand Down
45 changes: 30 additions & 15 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,10 @@ class Renderer : public NodeVisitor {
std::ostream* output_stream;

json additional_data;
json* current_loop_data = &additional_data["loop"];

json* get_current_loop_data() {
return &additional_data["loop"];
}

std::vector<std::shared_ptr<json>> data_tmp_stack;
std::stack<const json*> data_eval_stack;
Expand Down Expand Up @@ -2554,17 +2557,24 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an array", node);
}

if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
auto tmp = *current_loop_data; // Because of clang-3
(*current_loop_data)["parent"] = std::move(tmp);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.value)] = *it;

json* const current_loop_data = get_current_loop_data();

(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -2579,11 +2589,11 @@ class Renderer : public NodeVisitor {
}

additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
const auto tmp = (*current_loop_data)["parent"];
*current_loop_data = std::move(tmp);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand All @@ -2593,17 +2603,23 @@ class Renderer : public NodeVisitor {
throw_renderer_error("object must be an object", node);
}

if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
{
json* const current_loop_data = get_current_loop_data();
if (!current_loop_data->empty()) {
(*current_loop_data)["parent"] = std::move(*current_loop_data);
}

(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);
}

size_t index = 0;
(*current_loop_data)["is_first"] = true;
(*current_loop_data)["is_last"] = (result->size() <= 1);

for (auto it = result->begin(); it != result->end(); ++it) {
additional_data[static_cast<std::string>(node.key)] = it.key();
additional_data[static_cast<std::string>(node.value)] = it.value();

json* const current_loop_data = get_current_loop_data();
(*current_loop_data)["index"] = index;
(*current_loop_data)["index1"] = index + 1;
if (index == 1) {
Expand All @@ -2619,10 +2635,10 @@ class Renderer : public NodeVisitor {

additional_data[static_cast<std::string>(node.key)].clear();
additional_data[static_cast<std::string>(node.value)].clear();

json* const current_loop_data = get_current_loop_data();
if (!(*current_loop_data)["parent"].empty()) {
*current_loop_data = std::move((*current_loop_data)["parent"]);
} else {
current_loop_data = &additional_data["loop"];
}
}

Expand Down Expand Up @@ -2687,7 +2703,6 @@ class Renderer : public NodeVisitor {
data_input = &data;
if (loop_data) {
additional_data = *loop_data;
current_loop_data = &additional_data["loop"];
}

template_stack.emplace_back(current_template);
Expand Down
2 changes: 1 addition & 1 deletion test/test-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ TEST_CASE("types") {
data["is_sad"] = false;
data["@name"] = "@name";
data["$name"] = "$name";
data["relatives"]["mother"] = "Maria";
data["relatives"]["brother"] = "Chris";
data["relatives"]["mother"] = "Maria";
data["relatives"]["sister"] = "Jenny";
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
data["max_value"] = 18446744073709551615ull;
Expand Down