Fix Berry Memory leak in import re (#20823)

This commit is contained in:
s-hadinger 2024-02-27 21:56:51 +01:00 committed by GitHub
parent 24b59376bb
commit 7ad95faad2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -29,6 +29,7 @@ All notable changes to this project will be documented in this file.
- ESP32 PWM activity on unconfigured PWM GPIOs (#20732)
- Shutter inverted using internal commands (#20752)
- HASPmota PSRAM memory leak (#20818)
- Berry Memory leak in `import re`
### Removed

View File

@ -46,6 +46,9 @@ int be_re_compile(bvm *vm) {
}
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
if (code == NULL) {
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
}
int ret = re1_5_compilecode(code, regex_str);
if (ret != 0) {
be_raise(vm, "internal_error", "error in regex");
@ -113,11 +116,16 @@ int be_re_match_search(bvm *vm, bbool is_anchored, bbool size_only) {
}
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
if (code == NULL) {
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
}
int ret = re1_5_compilecode(code, regex_str);
if (ret != 0) {
be_os_free(code);
be_raise(vm, "internal_error", "error in regex");
}
be_re_match_search_run(vm, code, hay, is_anchored, size_only);
be_os_free(code);
be_return(vm);
}
be_raise(vm, "type_error", NULL);
@ -138,8 +146,12 @@ int be_re_match_search_all(bvm *vm, bbool is_anchored) {
}
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
if (code == NULL) {
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
}
int ret = re1_5_compilecode(code, regex_str);
if (ret != 0) {
be_os_free(code);
be_raise(vm, "internal_error", "error in regex");
}
@ -152,6 +164,7 @@ int be_re_match_search_all(bvm *vm, bbool is_anchored) {
be_pop(vm, 1);
}
be_pop(vm, 1);
be_os_free(code);
be_return(vm);
}
be_raise(vm, "type_error", NULL);
@ -328,11 +341,17 @@ int be_re_split(bvm *vm) {
}
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
if (code == NULL) {
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
}
int ret = re1_5_compilecode(code, regex_str);
if (ret != 0) {
be_os_free(code);
be_raise(vm, "internal_error", "error in regex");
}
return re_pattern_split_run(vm, code, hay, split_limit);
ret = re_pattern_split_run(vm, code, hay, split_limit);
be_os_free(code);
return ret;
}
be_raise(vm, "type_error", NULL);
}