Fixed one of those "Cs convenient ways to show error" (sigsegv caused by ivalid pointer). It were 2 hours of pure frustration

This commit is contained in:
Андреев Григорий 2024-08-14 16:12:31 +03:00
parent 28dec5384c
commit 0d479fcd0c
8 changed files with 30 additions and 15 deletions

View File

@ -28,7 +28,7 @@ namespace nytl {
LocalVarValue rendering_core_execute_expression(const global_elem_set_t& global_elems, LocalVarValue rendering_core_execute_expression(const global_elem_set_t& global_elems,
const std::vector<LocalVarValue>& local_vars, const json::JSON& expr); const std::vector<LocalVarValue>& local_vars, const json::JSON& expr);
std::string rendering_core(const std::string& entry_func, const std::vector<json::JSON>& entry_arguments, std::string rendering_core(const std::string& entry_func, const std::vector<const json::JSON*>& entry_arguments,
const global_elem_set_t& elem_ns, const std::function<std::string(std::string)>& escape); const global_elem_set_t& elem_ns, const std::function<std::string(std::string)>& escape);
} }

View File

@ -15,6 +15,15 @@ namespace nytl {
result(result) { result(result) {
} }
// todo: remove this debug function
// void print_result() {
// if (result.is_json) {
// printf("print_result %%p: %p\n", result.JSON_subval);
// printf("%s\n", json::generate_str(*result.JSON_subval, json::print_compact).c_str());
// } else
// printf("element %s\n", result.EL_name.c_str());
// }
void descend(const json::JSON& what) { void descend(const json::JSON& what) {
if (result.is_json) { if (result.is_json) {
const json::JSON& P = *result.JSON_subval; const json::JSON& P = *result.JSON_subval;
@ -24,7 +33,7 @@ namespace nytl {
ASSERT(ind_w > 0 && ind_w < arr_p.size(), "Expression \"array[integer]\" caused out-of-bound situation"); ASSERT(ind_w > 0 && ind_w < arr_p.size(), "Expression \"array[integer]\" caused out-of-bound situation");
result = LocalVarValue{true, "", &arr_p[ind_w]}; result = LocalVarValue{true, "", &arr_p[ind_w]};
} else if (P.isDictionary() && what.isString()) { } else if (P.isDictionary() && what.isString()) {
const std::map<std::string, json::JSON> dict_p = P.asDictionary(); const std::map<std::string, json::JSON>& dict_p = P.asDictionary();
const std::string& key_w = what.asString(); const std::string& key_w = what.asString();
ASSERT(dict_p.count(key_w) == 1, "No such key exception"); ASSERT(dict_p.count(key_w) == 1, "No such key exception");
result = LocalVarValue{true, "", &dict_p.at(key_w)}; result = LocalVarValue{true, "", &dict_p.at(key_w)};

View File

@ -437,6 +437,7 @@ namespace nytl {
P.called_element["C"] = json::JSON(json::array); P.called_element["C"] = json::JSON(json::array);
skipWhitespace(ctx); skipWhitespace(ctx);
P.passed_arguments = {parse_expression(ctx, local_var_names)}; P.passed_arguments = {parse_expression(ctx, local_var_names)};
skip_magic_block_end(ctx, syntax);
}; };
if (op == "WRITE") { if (op == "WRITE") {
mediocre_operator("str2text"); mediocre_operator("str2text");

View File

@ -230,18 +230,17 @@ namespace nytl {
return std::make_unique<RFrame_OverParts>(part.internal_element, saved_args_plus_iter, wsp_before_newlines); return std::make_unique<RFrame_OverParts>(part.internal_element, saved_args_plus_iter, wsp_before_newlines);
} }
std::string rendering_core(const std::string& entry_func, const std::vector<json::JSON>& entry_arguments, std::string rendering_core(const std::string& entry_func, const std::vector<const json::JSON*>& entry_arguments,
const global_elem_set_t& elem_ns, const std::function<std::string(std::string)>& escape) const global_elem_set_t& elem_ns, const std::function<std::string(std::string)>& escape)
{ {
Ditch result; Ditch result;
std::vector<uptr<RFrame>> stack; std::vector<uptr<RFrame>> stack;
{ {
size_t AN = entry_arguments.size(); size_t AN = entry_arguments.size();
std::vector<LocalVarValue> entry_arguments_conv(AN); std::vector<LocalVarValue> entry_arguments_conv(AN);
for (size_t i = 0; i < AN; i++) for (size_t i = 0; i < AN; i++)
entry_arguments_conv[i] = {true, "", &entry_arguments[i]}; entry_arguments_conv[i] = {true, "", entry_arguments[i]};
stack.push_back(std::make_unique<RFrame_OverParts>(entry_func, entry_arguments_conv, 0)); stack.push_back(std::make_unique<RFrame_OverParts>(entry_func, entry_arguments_conv, 0));
} }
bool returned = false; bool returned = false;

View File

@ -127,7 +127,7 @@ namespace nytl {
} }
/* Still can throw some stuff derived from std::exception (like bad alloc) */ /* Still can throw some stuff derived from std::exception (like bad alloc) */
std::string Templater::render(const std::string& element, const std::vector<json::JSON> &arguments) const { std::string Templater::render(const std::string& element, const std::vector<const json::JSON*> &arguments) const {
ASSERT(is_uname_dotted_sequence(element), "Incorrect entry element name"); ASSERT(is_uname_dotted_sequence(element), "Incorrect entry element name");
return rendering_core(element, arguments, elements, settings.escape); return rendering_core(element, arguments, elements, settings.escape);
} }

View File

@ -74,7 +74,7 @@ namespace nytl {
void update(); void update();
/* Throws exception, derived from std::exception */ /* Throws exception, derived from std::exception */
std::string render(const std::string& element, const std::vector<json::JSON>& arguments) const; std::string render(const std::string& element, const std::vector<const json::JSON*>& arguments) const;
}; };
} }

View File

@ -1,8 +1,9 @@
{% ELDEF main JSON abc %} {% ELDEF foo JSON cba %}
{% PUT jesc abc %} BBBB
{% ENDELDEF %}
AAAAAAAA
BBB
{% PUT jesc abc %}
{% ELDEF main JSON cba %}
AAAA
{% PUT test.foo cba %}
{% PUT test.foo cba %}
{% ENDELDEF %} {% ENDELDEF %}

View File

@ -1,3 +1,4 @@
#include <jsonincpp/string_representation.h>
#include <new_york_transit_line/templater.h> #include <new_york_transit_line/templater.h>
#include <new_york_transit_line/core.h> #include <new_york_transit_line/core.h>
@ -11,8 +12,12 @@ int main(int argc, char** argv) {
nytl::Templater templater(nytl::TemplaterSettings{nytl::TemplaterDetourRules{dir_path}}); nytl::Templater templater(nytl::TemplaterSettings{nytl::TemplaterDetourRules{dir_path}});
templater.update(); templater.update();
nytl::debug_print_templater(templater); nytl::debug_print_templater(templater);
json::JSON cba;
std::string answer2 = templater.render("test", {json::JSON()}); cba["boba"] = json::JSON("<>");
// printf("DEBUG WAS: %p\n", &cba["boba"].g());
// printf("%s\n", json::generate_str(cba["boba"].g(), json::print_compact).c_str());
// return 0;
std::string answer2 = templater.render("test", {&cba});
printf("%s\n<a><f><t><e><r><><l><f>\n", answer2.c_str()); printf("%s\n<a><f><t><e><r><><l><f>\n", answer2.c_str());
return 0; return 0;