Skip to content

Commit 7ad95fa

Browse files
authored
Fix Berry Memory leak in import re (#20823)
1 parent 24b5937 commit 7ad95fa

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

CHANGELOG.md

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

3334
### Removed
3435

lib/libesp32/berry/default/be_re_lib.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ int be_re_compile(bvm *vm) {
4646
}
4747

4848
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
49+
if (code == NULL) {
50+
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
51+
}
4952
int ret = re1_5_compilecode(code, regex_str);
5053
if (ret != 0) {
5154
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) {
113116
}
114117

115118
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
119+
if (code == NULL) {
120+
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
121+
}
116122
int ret = re1_5_compilecode(code, regex_str);
117123
if (ret != 0) {
124+
be_os_free(code);
118125
be_raise(vm, "internal_error", "error in regex");
119126
}
120127
be_re_match_search_run(vm, code, hay, is_anchored, size_only);
128+
be_os_free(code);
121129
be_return(vm);
122130
}
123131
be_raise(vm, "type_error", NULL);
@@ -138,8 +146,12 @@ int be_re_match_search_all(bvm *vm, bbool is_anchored) {
138146
}
139147

140148
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
149+
if (code == NULL) {
150+
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
151+
}
141152
int ret = re1_5_compilecode(code, regex_str);
142153
if (ret != 0) {
154+
be_os_free(code);
143155
be_raise(vm, "internal_error", "error in regex");
144156
}
145157

@@ -152,6 +164,7 @@ int be_re_match_search_all(bvm *vm, bbool is_anchored) {
152164
be_pop(vm, 1);
153165
}
154166
be_pop(vm, 1);
167+
be_os_free(code);
155168
be_return(vm);
156169
}
157170
be_raise(vm, "type_error", NULL);
@@ -328,11 +341,17 @@ int be_re_split(bvm *vm) {
328341
}
329342

330343
ByteProg *code = be_os_malloc(sizeof(ByteProg) + sz);
344+
if (code == NULL) {
345+
be_throw(vm, BE_MALLOC_FAIL); /* lack of heap space */
346+
}
331347
int ret = re1_5_compilecode(code, regex_str);
332348
if (ret != 0) {
349+
be_os_free(code);
333350
be_raise(vm, "internal_error", "error in regex");
334351
}
335-
return re_pattern_split_run(vm, code, hay, split_limit);
352+
ret = re_pattern_split_run(vm, code, hay, split_limit);
353+
be_os_free(code);
354+
return ret;
336355
}
337356
be_raise(vm, "type_error", NULL);
338357
}

0 commit comments

Comments
 (0)