diff options
| author | David Yang <mmyangfl@gmail.com> | 2025-03-21 10:25:40 +0800 |
|---|---|---|
| committer | David Yang <mmyangfl@gmail.com> | 2025-04-22 00:20:47 +0800 |
| commit | 6025b78d01a883ed24c2f983c1d7b6a4a05d3774 (patch) | |
| tree | 67877b6a088d83a635848e70d6d757ca564d3301 /ports/unix/main.c | |
| parent | 52ca8268800416538368dea1976d0922ff07543f (diff) | |
unix/main: Remove PATH_MAX from realpath.
POSIX.1-2008 ensures realpath() give a dynamically allocated buffer if
NULL is passed (which is also true for ports/windows/realpath.c),
avoiding an explicit call to malloc() and use of PATH_MAX, which may be
undefined on some systems.
Signed-off-by: David Yang <mmyangfl@gmail.com>
Diffstat (limited to 'ports/unix/main.c')
| -rw-r--r-- | ports/unix/main.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/ports/unix/main.c b/ports/unix/main.c index 0d137fe1b..9f51573fb 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -453,10 +453,13 @@ static void set_sys_argv(char *argv[], int argc, int start_arg) { #if MICROPY_PY_SYS_EXECUTABLE extern mp_obj_str_t mp_sys_executable_obj; -static char executable_path[MICROPY_ALLOC_PATH_MAX]; +static char *executable_path = NULL; static void sys_set_excecutable(char *argv0) { - if (realpath(argv0, executable_path)) { + if (executable_path == NULL) { + executable_path = realpath(argv0, NULL); + } + if (executable_path != NULL) { mp_obj_str_set_data(&mp_sys_executable_obj, (byte *)executable_path, strlen(executable_path)); } } @@ -721,11 +724,9 @@ MP_NOINLINE int main_(int argc, char **argv) { return invalid_args(); } } else { - char *pathbuf = malloc(PATH_MAX); - char *basedir = realpath(argv[a], pathbuf); + char *basedir = realpath(argv[a], NULL); if (basedir == NULL) { mp_printf(&mp_stderr_print, "%s: can't open file '%s': [Errno %d] %s\n", argv[0], argv[a], errno, strerror(errno)); - free(pathbuf); // CPython exits with 2 in such case ret = 2; break; @@ -734,7 +735,7 @@ MP_NOINLINE int main_(int argc, char **argv) { // Set base dir of the script as first entry in sys.path. char *p = strrchr(basedir, '/'); mp_obj_list_store(mp_sys_path, MP_OBJ_NEW_SMALL_INT(0), mp_obj_new_str_via_qstr(basedir, p - basedir)); - free(pathbuf); + free(basedir); set_sys_argv(argv, argc, a); ret = do_file(argv[a]); @@ -800,6 +801,11 @@ MP_NOINLINE int main_(int argc, char **argv) { #endif #endif + #if MICROPY_PY_SYS_EXECUTABLE && !defined(NDEBUG) + // Again, make memory leak detector happy + free(executable_path); + #endif + // printf("total bytes = %d\n", m_get_total_bytes_allocated()); return ret & 0xff; } |
