From c114e275c9f5a7d90e8536c6f353abdb15d0f1ab Mon Sep 17 00:00:00 2001 From: duerst Date: Thu, 6 Dec 2018 09:16:43 +0000 Subject: [PATCH] make sure all nodes are freed on error in node_extended_grapheme_cluster() regparse.c: In function node_extended_grapheme_cluster(), use function-global array node_common and use it for list and alternate construction. This is done so that in case of error, all nodes that have already been constructed can be correctly freed in a single for loop. Document the layout structure. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66239 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- regparse.c | 73 +++++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/regparse.c b/regparse.c index 138423655e..499e94e751 100644 --- a/regparse.c +++ b/regparse.c @@ -5795,7 +5795,29 @@ create_node_from_array(int kind, Node **np, Node **node_array) } #define R_ERR(call) r=(call);if(r!=0)goto err -#define NODE_ARRAY_SIZE 9 + +/* Memory layout for common node array: + * The main purpose is to be able to easily free all leftover nodes + * after an error. As a side effect, we share some memory. + * + * The layout is as shown below (each line corresponds to one call of + * create_node_from_array()). Because create_node_from_array sets all + * nodes of the source to NULL_NODE, we can overlap the target array + * as long as we do not override the actual target location. + * + * Target Array name Index + * + * node_array 0 1 2 3 4 5 6 7 8 9 A B C D E + * top_alts alts[4] 0 1 2 3* + * alts+1 list[4] 0 1 2 3* + * list+1 core_alts[7] 0 1 2 3 4 5 6* + * core_alts+0 H_list[4] 0 1 2 3* + * H_list+1 H_alt2[4] 0 1 2 3* + * h_alt2+1 H_list2[3] 0 1 2* + * core_alts+4 XP_list[4] 0 1 2 3* + * XP_list+1 Ex_list[4] 0 1 2 3* + */ +#define NODE_COMMON_SIZE 15 static int node_extended_grapheme_cluster(Node** np, ScanEnv* env) @@ -5811,17 +5833,14 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env) OnigOptionType option; /* node_array is function-global so that we can free all nodes * in case of error. Unused slots are set to NULL_NODE at all times. */ - Node *node_array[NODE_ARRAY_SIZE]; + Node *node_common[NODE_COMMON_SIZE]; #ifdef USE_UNICODE_PROPERTIES if (ONIGENC_IS_UNICODE(env->enc)) { /* UTF-8, UTF-16BE/LE, UTF-32BE/LE */ CClassNode* cc; - /* OnigCodePoint sb_out = (ONIGENC_MBC_MINLEN(env->enc) > 1) ? 0x00 : 0x80; */ - /* Node **seq = node_array; * seq[5] */ - /* Node **alts = node_array+5; * alts[4] */ - for (i=0; ibs, 0x0d); } - core_alts[6] = NULL_NODE; R_ERR(create_node_from_array(ALT, list+1, core_alts)); } @@ -5973,7 +5976,6 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env) R_ERR(add_code_range(&(cc->mbuf), env, 0x200D, 0x200D)); R_ERR(quantify_node(list+2, 0, REPEAT_INFINITE)); - list[3] = NULL_NODE; R_ERR(create_node_from_array(LIST, alts+1, list)); } @@ -5990,7 +5992,6 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env) NENCLOSE(tmp)->target = np1; alts[2] = tmp; - alts[3] = NULL_NODE; R_ERR(create_node_from_array(ALT, &top_alt, alts)); } } @@ -6054,8 +6055,8 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env) err: onig_node_free(np1); bbuf_free(pbuf1); - for (i=0; i