diff --git a/ext/prism/extension.c b/ext/prism/extension.c index db9fe78f349..824f7ad04e6 100644 --- a/ext/prism/extension.c +++ b/ext/prism/extension.c @@ -613,8 +613,7 @@ parse_lex_file(VALUE self, VALUE filepath) { */ static VALUE named_captures(VALUE self, VALUE source) { - pm_string_list_t string_list; - pm_string_list_init(&string_list); + pm_string_list_t string_list = { 0 }; if (!pm_regexp_named_capture_group_names((const uint8_t *) RSTRING_PTR(source), RSTRING_LEN(source), &string_list, false, &pm_encoding_utf_8)) { pm_string_list_free(&string_list); diff --git a/fuzz/regexp.c b/fuzz/regexp.c index fd828f6e8f1..77a456dea6e 100644 --- a/fuzz/regexp.c +++ b/fuzz/regexp.c @@ -2,8 +2,7 @@ void harness(const uint8_t *input, size_t size) { - pm_string_list_t capture_list; - pm_string_list_init(&capture_list); + pm_string_list_t capture_list = { 0 }; pm_regexp_named_capture_group_names(input, size, &capture_list, false, &pm_encoding_utf_8); pm_string_list_free(&capture_list); } diff --git a/include/prism/util/pm_string_list.h b/include/prism/util/pm_string_list.h index 5aa37587758..1f460e5dc9c 100644 --- a/include/prism/util/pm_string_list.h +++ b/include/prism/util/pm_string_list.h @@ -21,13 +21,6 @@ typedef struct { pm_string_t *strings; } pm_string_list_t; -/** - * Initialize a pm_string_list_t with its default values. - * - * @param string_list The string list to initialize. - */ -PRISM_EXPORTED_FUNCTION void pm_string_list_init(pm_string_list_t *string_list); - /** * Append a pm_string_t to the given string list. * diff --git a/src/prism.c b/src/prism.c index 3422aa5894d..0a8c26ea185 100644 --- a/src/prism.c +++ b/src/prism.c @@ -14706,10 +14706,9 @@ parse_call_operator_write(pm_parser_t *parser, pm_call_node_t *call_node, const // match write node. static pm_node_t * parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *content, pm_call_node_t *call) { - pm_string_list_t named_captures; - pm_string_list_init(&named_captures); - + pm_string_list_t named_captures = { 0 }; pm_node_t *result; + if (pm_regexp_named_capture_group_names(pm_string_source(content), pm_string_length(content), &named_captures, parser->encoding_changed, &parser->encoding) && (named_captures.length > 0)) { pm_match_write_node_t *match = pm_match_write_node_create(parser, call); @@ -14718,14 +14717,12 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t * pm_constant_id_t local; if (content->type == PM_STRING_SHARED) { - // If the unescaped string is a slice of the source, - // then we can copy the names directly. The pointers - // will line up. + // If the unescaped string is a slice of the source, then we can + // copy the names directly. The pointers will line up. local = pm_parser_local_add_location(parser, name->source, name->source + name->length); } else { - // Otherwise, the name is a slice of the malloc-ed - // owned string, in which case we need to copy it - // out into a new string. + // Otherwise, the name is a slice of the malloc-ed owned string, + // in which case we need to copy it out into a new string. size_t length = pm_string_length(name); void *memory = malloc(length); diff --git a/src/util/pm_string_list.c b/src/util/pm_string_list.c index fa7f20619f1..d49e4ed734d 100644 --- a/src/util/pm_string_list.c +++ b/src/util/pm_string_list.c @@ -1,26 +1,19 @@ #include "prism/util/pm_string_list.h" -/** - * Initialize a pm_string_list_t with its default values. - */ -void -pm_string_list_init(pm_string_list_t *string_list) { - string_list->length = 0; - string_list->capacity = 1; - string_list->strings = (pm_string_t *) malloc(sizeof(pm_string_t)); -} - /** * Append a pm_string_t to the given string list. */ void pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string) { if (string_list->length + 1 > string_list->capacity) { - pm_string_t *original_string = string_list->strings; - string_list->capacity *= 2; - string_list->strings = (pm_string_t *) malloc(string_list->capacity * sizeof(pm_string_t)); - memcpy(string_list->strings, original_string, (string_list->length) * sizeof(pm_string_t)); - free(original_string); + if (string_list->capacity == 0) { + string_list->capacity = 1; + } else { + string_list->capacity *= 2; + } + + string_list->strings = realloc(string_list->strings, string_list->capacity * sizeof(pm_string_t)); + if (string_list->strings == NULL) abort(); } string_list->strings[string_list->length++] = *string;