libc: handle empty path string in file I/O functions

Fixes #4689
This commit is contained in:
Christian Prochaska 2022-11-20 07:09:57 +01:00 committed by Christian Helmuth
parent 5d62429164
commit 866df56131

View File

@ -218,6 +218,9 @@ extern "C" int access(const char *path, int amode)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks(path, resolved_path);
@ -234,6 +237,9 @@ extern "C" int chdir(const char *path)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
struct stat stat_buf;
if ((stat(path, &stat_buf) == -1) ||
(!S_ISDIR(stat_buf.st_mode))) {
@ -336,6 +342,9 @@ __SYS_(int, fstatat, (int libc_fd, char const *path, struct stat *buf, int flags
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
if (*path == '/') {
if (flags & AT_SYMLINK_NOFOLLOW)
return lstat(path, buf);
@ -395,6 +404,9 @@ extern "C" int lstat(const char *path, struct stat *buf)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(path, resolved_path);
@ -411,6 +423,9 @@ extern "C" int mkdir(const char *path, mode_t mode)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(path, resolved_path);
@ -526,6 +541,9 @@ __SYS_(int, open, (const char *pathname, int flags, ...),
if (!pathname)
return Errno(EFAULT);
if (pathname[0] == '\0')
return Errno(ENOENT);
Absolute_path resolved_path;
Plugin *plugin;
@ -574,6 +592,9 @@ __SYS_(int, openat, (int libc_fd, const char *path, int flags, ...),
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
va_list ap;
va_start(ap, flags);
mode_t mode = va_arg(ap, unsigned);
@ -646,6 +667,9 @@ extern "C" ssize_t readlink(const char *path, char *buf, ::size_t bufsiz)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(path, resolved_path);
@ -661,6 +685,9 @@ extern "C" int rename(const char *oldpath, const char *newpath)
if (!oldpath || !newpath)
return Errno(EFAULT);
if ((oldpath[0] == '\0') || (newpath[0] == '\0'))
return Errno(ENOENT);
try {
Absolute_path resolved_oldpath, resolved_newpath;
resolve_symlinks_except_last_element(oldpath, resolved_oldpath);
@ -681,6 +708,9 @@ extern "C" int rmdir(const char *path)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(path, resolved_path);
@ -708,6 +738,9 @@ extern "C" int stat(const char *path, struct stat *buf)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks(path, resolved_path);
@ -724,6 +757,9 @@ extern "C" int symlink(const char *oldpath, const char *newpath)
if (!oldpath || !newpath)
return Errno(EFAULT);
if ((oldpath[0] == '\0') || (newpath[0] == '\0'))
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(newpath, resolved_path);
@ -739,6 +775,9 @@ extern "C" int unlink(const char *path)
if (!path)
return Errno(EFAULT);
if (path[0] == '\0')
return Errno(ENOENT);
try {
Absolute_path resolved_path;
resolve_symlinks_except_last_element(path, resolved_path);