wut/libraries/wutdevoptab/devoptab_fs_stat.c
rw-r-r-0644 6ea6625b78 devoptab: Fix double free in __wut_fs_stat
The double free caused apps to crash when attempting to run stat on a directory
2019-02-24 15:48:46 +00:00

50 lines
1.2 KiB
C

#include "devoptab_fs.h"
int
__wut_fs_stat(struct _reent *r,
const char *path,
struct stat *st)
{
int fd;
FSStatus status;
FSCmdBlock cmd;
if (!path || !st) {
r->_errno = EINVAL;
return -1;
}
char *fixedPath = __wut_fs_fixpath(r, path);
if (!fixedPath) {
return -1;
}
FSInitCmdBlock(&cmd);
// First try open as file
status = FSOpenFile(__wut_devoptab_fs_client, &cmd, fixedPath, "r",
(FSFileHandle*)&fd, -1);
if (status >= 0) {
__wut_fs_file_t tmpfd = { .fd = fd };
status = __wut_fs_fstat(r, &tmpfd, st);
FSCloseFile(__wut_devoptab_fs_client, &cmd, fd, -1);
free(fixedPath);
return status;
}
// File failed, so lets try open as directory
status = FSOpenDir(__wut_devoptab_fs_client, &cmd, fixedPath,
(FSDirectoryHandle*)&fd, -1);
free(fixedPath);
if (status < 0) {
r->_errno = __wut_fs_translate_error(status);
return -1;
}
memset(st, 0, sizeof(struct stat));
st->st_nlink = 1;
st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
FSCloseDir(__wut_devoptab_fs_client, &cmd, fd, -1);
return 0;
}