fix(fs): double free and not cleaning temp directory automatically

This commit is contained in:
maelstrom 2026-05-22 08:52:59 +02:00
commit 80fc29a8d3
3 changed files with 14 additions and 11 deletions

View file

@ -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)
target_link_libraries(onusfs PUBLIC onus::common)

View file

@ -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

View file

@ -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;
}