From 80fc29a8d36386408eb6d27fbe11c507abacbe18 Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 22 May 2026 08:52:59 +0200 Subject: [PATCH] fix(fs): double free and not cleaning temp directory automatically --- pkg/fs/CMakeLists.txt | 2 +- pkg/fs/include/onus/fs.h | 6 ++++-- pkg/fs/src/tmp.c | 17 +++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/fs/CMakeLists.txt b/pkg/fs/CMakeLists.txt index 94d6288..293ea8d 100644 --- a/pkg/fs/CMakeLists.txt +++ b/pkg/fs/CMakeLists.txt @@ -2,4 +2,4 @@ add_library(onusfs) add_library(onus::fs ALIAS onusfs) target_sources(onusfs PRIVATE src/tmp.c) target_include_directories(onusfs PUBLIC include) -target_link_libraries(onusfs PRIVATE onus::common) \ No newline at end of file +target_link_libraries(onusfs PUBLIC onus::common) \ No newline at end of file diff --git a/pkg/fs/include/onus/fs.h b/pkg/fs/include/onus/fs.h index a8d9d33..9d2999f 100644 --- a/pkg/fs/include/onus/fs.h +++ b/pkg/fs/include/onus/fs.h @@ -28,8 +28,10 @@ int onus_fs_copy_file(const char *src, const char *dst); int onus_fs_move_file(const char *src, const char *dst); /** - * Creates a temporary file directory for storing user files. Directory name is - * randomly chosen, but may be suffixed to indicate its source + * Creates a temporary file directory for storing files in the platform + * temporary directory. Directory name is randomly chosen, but may be suffixed + * to indicate its source. If an error occurs during this function, the + * immediately created temporary directory is automatically destroyed * * Not all platforms automatically clean temporary directories, so you should * delete it using onus_fs_del_tmp diff --git a/pkg/fs/src/tmp.c b/pkg/fs/src/tmp.c index 27dfa60..d4d9018 100644 --- a/pkg/fs/src/tmp.c +++ b/pkg/fs/src/tmp.c @@ -10,19 +10,19 @@ int onus_fs_make_tmp(char *path, onus_size_t path_sz, const char *suffix) { char *npath = NULL; - char *tmpname = NULL; int ret; - tmpname = malloc(strlen(suffix) + 1 + 6); - sprintf(tmpname, "%sXXXXXX", suffix); - npath = mkdtemp(tmpname); + if (suffix == NULL) suffix = ""; + npath = malloc(strlen(suffix) + 1 + 6); + sprintf(npath, "%sXXXXXX", suffix); + npath = mkdtemp(npath); if (errno == EACCES) { ret = ONUS_E_PERMISSION_ERROR; - goto clean; + goto end; } else if (errno != 0) { ret = ONUS_E_FS_ERROR; - goto clean; + goto end; } if (onus_nstrcpy(path, npath, path_sz) + 1 > path_sz) { @@ -31,11 +31,12 @@ int onus_fs_make_tmp(char *path, onus_size_t path_sz, const char *suffix) { } ret = ONUS_SUCCESS; + goto end; clean: + onus_fs_del_tmp(npath); +end: if (npath) free(npath); - if (tmpname) - free(tmpname); return ret; }