diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-10-02 07:07:20 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-10-02 07:07:20 +0900 |
| commit | c33100c4f03b5f0e8d9034a7b82fabb0975e9a5a (patch) | |
| tree | f0e649f18e0d0abb7c7b8d9d969e37ddb491b170 | |
| parent | 97433c02630c5a983e205371cbd4fbe4eff8fcc0 (diff) | |
| parent | e2f54ff59483e886042712560599d987cd9fc4ac (diff) | |
| download | mruby-c33100c4f03b5f0e8d9034a7b82fabb0975e9a5a.tar.gz mruby-c33100c4f03b5f0e8d9034a7b82fabb0975e9a5a.zip | |
Merge pull request #4736 from dearblue/ast-strdump
Escape the AST string
| -rw-r--r-- | mrbgems/mruby-compiler/core/parse.y | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index 9ab20610a..b2c31ed17 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -21,6 +21,7 @@ #include <mruby/proc.h> #include <mruby/error.h> #include <mruby/throw.h> +#include <mruby/string.h> #include "node.h" #define YYLEX_PARAM p @@ -6420,6 +6421,28 @@ dump_args(mrb_state *mrb, node *n, int offset) } } +/* + * This function restores the GC arena on return. + * For this reason, if a process that further generates an object is + * performed at the caller, the string pointer returned as the return + * value may become invalid. + */ +static const char* +str_dump(mrb_state *mrb, const char *str, int len) +{ + mrb_int ai = mrb_gc_arena_save(mrb); + mrb_value s; +# if INT_MAX > MRB_INT_MAX / 4 + /* check maximum length with "\xNN" charactor */ + if (len > MRB_INT_MAX / 4) { + len = MRB_INT_MAX / 4; + } +# endif + s = mrb_str_new(mrb, str, (mrb_int)len); + s = mrb_str_dump(mrb, s); + mrb_gc_arena_restore(mrb, ai); + return RSTRING_PTR(s); +} #endif void @@ -6894,7 +6917,7 @@ mrb_parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_STR: - printf("NODE_STR \"%s\" len %d\n", (char*)tree->car, intn(tree->cdr)); + printf("NODE_STR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); break; case NODE_DSTR: @@ -6903,7 +6926,7 @@ mrb_parser_dump(mrb_state *mrb, node *tree, int offset) break; case NODE_XSTR: - printf("NODE_XSTR \"%s\" len %d\n", (char*)tree->car, intn(tree->cdr)); + printf("NODE_XSTR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); break; case NODE_DXSTR: |
