Skip to content

Commit 520f356

Browse files
committed
unix/main: When preparing sys.path, allocate exact strings on uPy heap.
Due to the way modern compilers work (allocating space for stack vars once at tha start of function, and deallocating once on exit from), using intermediate stack buffer of big size caused blockage of 4K (PATH_MAX) on stack for the entire duration of MicroPython execution.
1 parent 649b69a commit 520f356

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

unix/main.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,12 @@ MP_NOINLINE int main_(int argc, char **argv) {
444444
}
445445
if (p[0] == '~' && p[1] == '/' && home != NULL) {
446446
// Expand standalone ~ to $HOME
447-
CHECKBUF(buf, PATH_MAX);
448-
CHECKBUF_APPEND(buf, home, strlen(home));
449-
CHECKBUF_APPEND(buf, p + 1, (size_t)(p1 - p - 1));
450-
path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
447+
int home_l = strlen(home);
448+
vstr_t vstr;
449+
vstr_init(&vstr, home_l + (p1 - p - 1) + 1);
450+
vstr_add_strn(&vstr, home, home_l);
451+
vstr_add_strn(&vstr, p + 1, p1 - p - 1);
452+
path_items[i] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
451453
} else {
452454
path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
453455
}

0 commit comments

Comments
 (0)