Skip to content

Commit

Permalink
Fixed potential memory leak in the _editor_init_syntax_add_rule function
Browse files Browse the repository at this point in the history
  • Loading branch information
proh14 authored and adsr committed Nov 15, 2024
1 parent 1ea1d85 commit 36d1255
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static int _editor_add_macro_by_str(editor_t *editor, char *str);
static void _editor_init_syntaxes(editor_t *editor);
static void _editor_init_syntax(editor_t *editor, syntax_t **optret_syntax, char *name, char *path_pattern, int tab_width, int tab_to_space, srule_def_t *defs);
static int _editor_init_syntax_by_str(editor_t *editor, syntax_t **ret_syntax, char *str);
static void _editor_init_syntax_add_rule(syntax_t *syntax, srule_def_t *def);
static int _editor_init_syntax_add_rule(syntax_t *syntax, srule_def_t *def);
static int _editor_init_syntax_add_rule_by_str(syntax_t *syntax, char *str);
static void _editor_destroy_syntax_map(syntax_t *map);
static int _editor_init_from_rc_read(editor_t *editor, FILE *rc, char **ret_rc_data, size_t *ret_rc_data_len);
Expand Down Expand Up @@ -2255,15 +2255,20 @@ static int _editor_init_syntax_by_str(editor_t *editor, syntax_t **ret_syntax, c
}

// Add rule to syntax
static void _editor_init_syntax_add_rule(syntax_t *syntax, srule_def_t *def) {
static int _editor_init_syntax_add_rule(syntax_t *syntax, srule_def_t *def) {
srule_node_t *node;
node = calloc(1, sizeof(srule_node_t));
if (def->re_end) {
node->srule = srule_new_multi(def->re, strlen(def->re), def->re_end, strlen(def->re_end), def->fg, def->bg);
} else {
node->srule = srule_new_single(def->re, strlen(def->re), 0, def->fg, def->bg);
}
if (node->srule) DL_APPEND(syntax->srules, node);
if (!node->srule) {
free(node);
return MLE_ERR;
}
DL_APPEND(syntax->srules, node);
return MLE_OK;
}

// Proxy for _editor_init_syntax_add_rule with str in format '<start>,<end>,<fg>,<bg>' or '<regex>,<fg>,<bg>'
Expand All @@ -2275,8 +2280,7 @@ static int _editor_init_syntax_add_rule_by_str(syntax_t *syntax, char *str) {
args[2] = strtok(NULL, ","); if (!args[2]) return MLE_ERR;
args[3] = strtok(NULL, ",");
style_i = args[3] ? 2 : 1;
_editor_init_syntax_add_rule(syntax, &((srule_def_t){ args[0], style_i == 2 ? args[1] : NULL, atoi(args[style_i]), atoi(args[style_i + 1]) }));
return MLE_OK;
return _editor_init_syntax_add_rule(syntax, &((srule_def_t){ args[0], style_i == 2 ? args[1] : NULL, atoi(args[style_i]), atoi(args[style_i + 1]) }));
}

// Destroy a syntax
Expand Down

0 comments on commit 36d1255

Please sign in to comment.