From 1f6157cd6e139da3e8b38c0bea62922ce3db2635 Mon Sep 17 00:00:00 2001 From: "K.Kosako" Date: Tue, 6 Jun 2017 14:57:33 +0900 Subject: [PATCH] rename and change type from scanenv->mem_nodes to mem_env --- src/regcomp.c | 40 ++++++++++++++++++++-------------------- src/regparse.c | 45 ++++++++++++++++++++++----------------------- src/regparse.h | 18 ++++++++++++------ 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/regcomp.c b/src/regcomp.c index 9a35dc2..88a6a92 100644 --- a/src/regcomp.c +++ b/src/regcomp.c @@ -1940,7 +1940,7 @@ disable_noname_group_capture(Node** root, regex_t* reg, ScanEnv* env) for (i = 1, pos = 1; i <= env->num_mem; i++) { if (map[i].new_val > 0) { - SCANENV_MEM_NODES(env)[pos] = SCANENV_MEM_NODES(env)[i]; + SCANENV_MEMENV(env)[pos] = SCANENV_MEMENV(env)[i]; pos++; } } @@ -2439,17 +2439,17 @@ get_min_len(Node* node, OnigLen *min, ScanEnv* env) { int i; int* backs; - Node** nodes = SCANENV_MEM_NODES(env); + MemEnv* mem_env = SCANENV_MEMENV(env); BRefNode* br = NBREF(node); if (br->state & NST_RECURSION) break; backs = BACKREFS_P(br); if (backs[0] > env->num_mem) return ONIGERR_INVALID_BACKREF; - r = get_min_len(nodes[backs[0]], min, env); + r = get_min_len(mem_env[backs[0]].node, min, env); if (r != 0) break; for (i = 1; i < br->back_num; i++) { if (backs[i] > env->num_mem) return ONIGERR_INVALID_BACKREF; - r = get_min_len(nodes[backs[i]], &tmin, env); + r = get_min_len(mem_env[backs[i]].node, &tmin, env); if (r != 0) break; if (*min > tmin) *min = tmin; } @@ -2598,7 +2598,7 @@ get_max_len(Node* node, OnigLen *max, ScanEnv* env) { int i; int* backs; - Node** nodes = SCANENV_MEM_NODES(env); + MemEnv* mem_env = SCANENV_MEMENV(env); BRefNode* br = NBREF(node); if (br->state & NST_RECURSION) { *max = ONIG_INFINITE_DISTANCE; @@ -2607,7 +2607,7 @@ get_max_len(Node* node, OnigLen *max, ScanEnv* env) backs = BACKREFS_P(br); for (i = 0; i < br->back_num; i++) { if (backs[i] > env->num_mem) return ONIGERR_INVALID_BACKREF; - r = get_max_len(nodes[backs[i]], &tmax, env); + r = get_max_len(mem_env[backs[i]].node, &tmax, env); if (r != 0) break; if (*max < tmax) *max = tmax; } @@ -2977,7 +2977,7 @@ setup_subexp_call(Node* node, ScanEnv* env) case NT_CALL: { CallNode* cn = NCALL(node); - Node** nodes = SCANENV_MEM_NODES(env); + MemEnv* mem_env = SCANENV_MEMENV(env); if (cn->group_num != 0) { int gnum = cn->group_num; @@ -2998,7 +2998,7 @@ setup_subexp_call(Node* node, ScanEnv* env) #ifdef USE_NAMED_GROUP set_call_attr: #endif - cn->target = nodes[cn->group_num]; + cn->target = mem_env[cn->group_num].node; if (IS_NULL(cn->target)) { onig_scan_env_set_error_string(env, ONIGERR_UNDEFINED_NAME_REFERENCE, cn->name, cn->name_end); @@ -3751,7 +3751,7 @@ setup_tree(Node* node, regex_t* reg, int state, ScanEnv* env) { int i; int* p; - Node** nodes = SCANENV_MEM_NODES(env); + MemEnv* mem_env = SCANENV_MEMENV(env); BRefNode* br = NBREF(node); p = BACKREFS_P(br); for (i = 0; i < br->back_num; i++) { @@ -3763,7 +3763,7 @@ setup_tree(Node* node, regex_t* reg, int state, ScanEnv* env) BIT_STATUS_ON_AT(env->bt_mem_end, p[i]); } #endif - SET_ENCLOSE_STATUS(nodes[p[i]], NST_MEM_BACKREFED); + SET_ENCLOSE_STATUS(mem_env[p[i]].node, NST_MEM_BACKREFED); } } break; @@ -4793,7 +4793,7 @@ optimize_node_left(Node* node, NodeOptInfo* opt, OptEnv* env) int i; int* backs; OnigLen min, max, tmin, tmax; - Node** nodes = SCANENV_MEM_NODES(env->scan_env); + MemEnv* mem_env = SCANENV_MEMENV(env->scan_env); BRefNode* br = NBREF(node); if (br->state & NST_RECURSION) { @@ -4801,14 +4801,14 @@ optimize_node_left(Node* node, NodeOptInfo* opt, OptEnv* env) break; } backs = BACKREFS_P(br); - r = get_min_len(nodes[backs[0]], &min, env->scan_env); + r = get_min_len(mem_env[backs[0]].node, &min, env->scan_env); if (r != 0) break; - r = get_max_len(nodes[backs[0]], &max, env->scan_env); + r = get_max_len(mem_env[backs[0]].node, &max, env->scan_env); if (r != 0) break; for (i = 1; i < br->back_num; i++) { - r = get_min_len(nodes[backs[i]], &tmin, env->scan_env); + r = get_min_len(mem_env[backs[i]].node, &tmin, env->scan_env); if (r != 0) break; - r = get_max_len(nodes[backs[i]], &tmax, env->scan_env); + r = get_max_len(mem_env[backs[i]].node, &tmax, env->scan_env); if (r != 0) break; if (min > tmin) min = tmin; if (max < tmax) max = tmax; @@ -5414,9 +5414,9 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, if (r != 0) goto err_unset; #endif - if (IS_NOT_NULL(scan_env.mem_nodes_dynamic)) { - xfree(scan_env.mem_nodes_dynamic); - scan_env.mem_nodes_dynamic = (Node** )NULL; + if (IS_NOT_NULL(scan_env.mem_env_dynamic)) { + xfree(scan_env.mem_env_dynamic); + scan_env.mem_env_dynamic = (MemEnv* )NULL; } r = compile_tree(root, reg); @@ -5472,8 +5472,8 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, } onig_node_free(root); - if (IS_NOT_NULL(scan_env.mem_nodes_dynamic)) - xfree(scan_env.mem_nodes_dynamic); + if (IS_NOT_NULL(scan_env.mem_env_dynamic)) + xfree(scan_env.mem_env_dynamic); return r; } diff --git a/src/regparse.c b/src/regparse.c index 8153513..e2cb9fc 100644 --- a/src/regparse.c +++ b/src/regparse.c @@ -962,13 +962,11 @@ onig_noname_group_capture_is_active(regex_t* reg) } -#define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16 +#define INIT_SCANENV_MEMENV_ALLOC_SIZE 16 static void scan_env_clear(ScanEnv* env) { - int i; - BIT_STATUS_CLEAR(env->capture_history); BIT_STATUS_CLEAR(env->bt_mem_start); BIT_STATUS_CLEAR(env->bt_mem_end); @@ -980,11 +978,10 @@ scan_env_clear(ScanEnv* env) #ifdef USE_NAMED_GROUP env->num_named = 0; #endif - env->mem_alloc = 0; - env->mem_nodes_dynamic = (Node** )NULL; + env->mem_alloc = 0; + env->mem_env_dynamic = (MemEnv* )NULL; - for (i = 0; i < SCANENV_MEMNODES_SIZE; i++) - env->mem_nodes_static[i] = NULL_NODE; + xmemset(env->mem_env_static, 0, sizeof(env->mem_env_static)); #ifdef USE_COMBINATION_EXPLOSION_CHECK env->num_comb_exp_check = 0; @@ -999,30 +996,32 @@ static int scan_env_add_mem_entry(ScanEnv* env) { int i, need, alloc; - Node** p; + MemEnv* p; need = env->num_mem + 1; if (need > MaxCaptureNum && MaxCaptureNum != 0) return ONIGERR_TOO_MANY_CAPTURES; - if (need >= SCANENV_MEMNODES_SIZE) { + if (need >= SCANENV_MEMENV_SIZE) { if (env->mem_alloc <= need) { - if (IS_NULL(env->mem_nodes_dynamic)) { - alloc = INIT_SCANENV_MEMNODES_ALLOC_SIZE; - p = (Node** )xmalloc(sizeof(Node*) * alloc); - xmemcpy(p, env->mem_nodes_static, - sizeof(Node*) * SCANENV_MEMNODES_SIZE); + if (IS_NULL(env->mem_env_dynamic)) { + alloc = INIT_SCANENV_MEMENV_ALLOC_SIZE; + p = (MemEnv* )xmalloc(sizeof(MemEnv) * alloc); + xmemcpy(p, env->mem_env_static, sizeof(env->mem_env_static)); } else { alloc = env->mem_alloc * 2; - p = (Node** )xrealloc(env->mem_nodes_dynamic, sizeof(Node*) * alloc); + p = (MemEnv* )xrealloc(env->mem_env_dynamic, sizeof(MemEnv) * alloc); } CHECK_NULL_RETURN_MEMERR(p); - for (i = env->num_mem + 1; i < alloc; i++) - p[i] = NULL_NODE; + for (i = env->num_mem + 1; i < alloc; i++) { + p[i].node = NULL_NODE; + p[i].in = 0; + p[i].recursion = 0; + } - env->mem_nodes_dynamic = p; + env->mem_env_dynamic = p; env->mem_alloc = alloc; } } @@ -1035,7 +1034,7 @@ static int scan_env_set_mem_node(ScanEnv* env, int num, Node* node) { if (env->num_mem >= num) - SCANENV_MEM_NODES(env)[num] = node; + SCANENV_MEMENV(env)[num].node = node; else return ONIGERR_PARSER_BUG; return 0; @@ -1250,7 +1249,7 @@ node_new_backref(int back_num, int* backrefs, int by_name, for (i = 0; i < back_num; i++) { if (backrefs[i] <= env->num_mem && - IS_NULL(SCANENV_MEM_NODES(env)[backrefs[i]])) { + IS_NULL(SCANENV_MEMENV(env)[backrefs[i]].node)) { NBREF(node)->state |= NST_RECURSION; /* /...(\1).../ */ break; } @@ -3418,7 +3417,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env) if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) && (num <= env->num_mem || num <= 9)) { /* This spec. from GNU regex */ if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) { - if (num > env->num_mem || IS_NULL(SCANENV_MEM_NODES(env)[num])) + if (num > env->num_mem || IS_NULL(SCANENV_MEMENV(env)[num].node)) return ONIGERR_INVALID_BACKREF; } @@ -3489,7 +3488,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env) if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) { if (back_num > env->num_mem || - IS_NULL(SCANENV_MEM_NODES(env)[back_num])) + IS_NULL(SCANENV_MEMENV(env)[back_num].node)) return ONIGERR_INVALID_BACKREF; } tok->type = TK_BACKREF; @@ -3508,7 +3507,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env) int i; for (i = 0; i < num; i++) { if (backs[i] > env->num_mem || - IS_NULL(SCANENV_MEM_NODES(env)[backs[i]])) + IS_NULL(SCANENV_MEMENV(env)[backs[i]].node)) return ONIGERR_INVALID_BACKREF; } } diff --git a/src/regparse.h b/src/regparse.h index 2c7f72c..7937f05 100644 --- a/src/regparse.h +++ b/src/regparse.h @@ -271,10 +271,16 @@ typedef struct _Node { #define NULL_NODE ((Node* )0) -#define SCANENV_MEMNODES_SIZE 8 -#define SCANENV_MEM_NODES(senv) \ - (IS_NOT_NULL((senv)->mem_nodes_dynamic) ? \ - (senv)->mem_nodes_dynamic : (senv)->mem_nodes_static) +#define SCANENV_MEMENV_SIZE 8 +#define SCANENV_MEMENV(senv) \ + (IS_NOT_NULL((senv)->mem_env_dynamic) ? \ + (senv)->mem_env_dynamic : (senv)->mem_env_static) + +typedef struct { + Node* node; + int in; + int recursion; +} MemEnv; typedef struct { OnigOptionType option; @@ -299,8 +305,8 @@ typedef struct { int num_named; #endif int mem_alloc; - Node* mem_nodes_static[SCANENV_MEMNODES_SIZE]; - Node** mem_nodes_dynamic; + MemEnv mem_env_static[SCANENV_MEMENV_SIZE]; + MemEnv* mem_env_dynamic; #ifdef USE_COMBINATION_EXPLOSION_CHECK int num_comb_exp_check; int comb_exp_max_regnum; -- 2.40.0