summaryrefslogtreecommitdiff
path: root/lib/libc/dirent
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/dirent')
-rw-r--r--lib/libc/dirent/closedir.c10
-rw-r--r--lib/libc/dirent/fdopendir.c14
-rw-r--r--lib/libc/dirent/readdir_r.c57
3 files changed, 35 insertions, 46 deletions
diff --git a/lib/libc/dirent/closedir.c b/lib/libc/dirent/closedir.c
index 873364c2..bc2efb08 100644
--- a/lib/libc/dirent/closedir.c
+++ b/lib/libc/dirent/closedir.c
@@ -1,8 +1,8 @@
-#include <__dirent.h> // for __DIR
-#include <dirent.h> // for DIR, closedir
-#include <errno.h> // for EBADF, errno
-#include <stdlib.h> // for free
-#include <unistd.h> // for close
+#include <dirent.h> // for DIR, closedir
+#include <errno.h> // for EBADF, errno
+#include <libc/dirent.h> // for __DIR
+#include <stdlib.h> // for free
+#include <unistd.h> // for close
int closedir(DIR *dirp)
{
diff --git a/lib/libc/dirent/fdopendir.c b/lib/libc/dirent/fdopendir.c
index c0e3492b..145d7404 100644
--- a/lib/libc/dirent/fdopendir.c
+++ b/lib/libc/dirent/fdopendir.c
@@ -1,20 +1,22 @@
#include "stddef.h" // for NULL
-#include <__dirent.h> // for __DIR
-#include <dirent.h> // for DIR, fdopendir
-#include <errno.h> // for EBADF, errno
-#include <stdlib.h> // for calloc
+#include <dirent.h>
+#include <errno.h>
+#include <libc/dirent.h>
+#include <stdlib.h>
+#include <sys/cdefs.h>
DIR *fdopendir(int fildes)
{
struct __DIR *dir;
- if (fildes < 0) {
+ if (__predict_false(fildes < 0)) {
errno = EBADF;
return NULL;
}
- if ((dir = calloc(1, sizeof(struct __DIR))) == NULL) {
+ dir = calloc(1, sizeof(struct __DIR));
+ if (__predict_false(dir == NULL)) {
return NULL;
}
diff --git a/lib/libc/dirent/readdir_r.c b/lib/libc/dirent/readdir_r.c
index c7bffdea..049865f2 100644
--- a/lib/libc/dirent/readdir_r.c
+++ b/lib/libc/dirent/readdir_r.c
@@ -2,21 +2,20 @@
#include "sys/types.h" // for off_t
-#include <__dirent.h> // for linux_dirent64
-#include <dirent.h> // for dirent, ssize_t, DIR, readdir_r
-#include <errno.h> // for EINVAL, errno
-#include <stddef.h> // for NULL, offsetof
-#include <string.h> // for memcpy, size_t, memset
-#include <syscall.h> // for __syscall_3, syscall
-
-int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
- struct dirent **restrict result)
+#include <dirent.h> // for dirent, ssize_t, DIR, readdir_r
+#include <errno.h> // for EINVAL, errno
+#include <libc/dirent.h> // for linux_dirent64
+#include <stddef.h> // for NULL, offsetof
+#include <string.h> // for memcpy, size_t, memset
+#include <syscall.h> // for __syscall_3, syscall
+
+int readdir_r(DIR *restrict dirp, struct dirent *restrict entry, struct dirent **restrict result)
{
struct linux_dirent64 *ldir = (void *)dirp->buffer;
ssize_t nread;
int ret;
- if (dirp == NULL || entry == NULL || result == NULL) {
+ if (dirp == NULL || entry == NULL || (void *)result == NULL) {
return EINVAL;
}
@@ -28,19 +27,15 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
/* Validate buffer bounds */
if (dirp->offset >= (off_t)sizeof(dirp->buffer) ||
- dirp->offset + (off_t)sizeof(struct linux_dirent64) >
- (off_t)sizeof(dirp->buffer)) {
+ dirp->offset + (off_t)sizeof(struct linux_dirent64) > (off_t)sizeof(dirp->buffer)) {
dirp->cached = 0;
*result = NULL;
return 0;
}
/* Validate record length */
- if (ldir->d_reclen <
- offsetof(struct linux_dirent64, d_name) + 1 ||
- dirp->offset + (off_t)ldir->d_reclen >
- (off_t)sizeof(dirp->buffer) ||
- ldir->d_reclen == 0) {
+ if (ldir->d_reclen < offsetof(struct linux_dirent64, d_name) + 1 ||
+ dirp->offset + (off_t)ldir->d_reclen > (off_t)sizeof(dirp->buffer) || ldir->d_reclen == 0) {
dirp->cached = 0;
*result = NULL;
return 0;
@@ -49,16 +44,14 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
entry->d_ino = ldir->d_ino;
/* Calculate available space for name */
- size_t max_name_len = ldir->d_reclen -
- offsetof(struct linux_dirent64, d_name);
+ size_t max_name_len = ldir->d_reclen - offsetof(struct linux_dirent64, d_name);
if (max_name_len > sizeof(entry->d_name) - 1) {
max_name_len = sizeof(entry->d_name) - 1;
}
/* Find actual string length, bounded by available space */
size_t name_len = 0;
- while (name_len < max_name_len &&
- ldir->d_name[name_len] != '\0') {
+ while (name_len < max_name_len && ldir->d_name[name_len] != '\0') {
name_len++;
}
@@ -75,8 +68,7 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
dirp->cached = 0;
dirp->offset = 0;
- ret = syscall(getdents64, dirp->fildes, dirp->buffer,
- sizeof(dirp->buffer));
+ ret = syscall(getdents64, dirp->fildes, dirp->buffer, sizeof(dirp->buffer));
if (ret < 0)
return errno;
@@ -90,8 +82,8 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
/* Validate first entry bounds */
if (nread < (ssize_t)sizeof(struct linux_dirent64) ||
- ldir->d_reclen < offsetof(struct linux_dirent64, d_name) + 1 ||
- ldir->d_reclen > nread || ldir->d_reclen == 0) {
+ ldir->d_reclen < offsetof(struct linux_dirent64, d_name) + 1 || ldir->d_reclen > nread ||
+ ldir->d_reclen == 0) {
*result = NULL;
return EINVAL;
}
@@ -100,8 +92,7 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
entry->d_ino = ldir->d_ino;
/* Calculate available space for name */
- size_t max_name_len =
- ldir->d_reclen - offsetof(struct linux_dirent64, d_name);
+ size_t max_name_len = ldir->d_reclen - offsetof(struct linux_dirent64, d_name);
if (max_name_len > sizeof(entry->d_name) - 1) {
max_name_len = sizeof(entry->d_name) - 1;
}
@@ -119,16 +110,12 @@ int readdir_r(DIR *restrict dirp, struct dirent *restrict entry,
/* Count the amount of remaining entries we have cached from getdents.
*/
for (ssize_t buffer_offset = ldir->d_reclen; buffer_offset < nread;) {
- struct linux_dirent64 *next_ldir =
- (void *)(dirp->buffer + buffer_offset);
+ struct linux_dirent64 *next_ldir = (void *)(dirp->buffer + buffer_offset);
/* Validate entry bounds to prevent infinite loops */
- if (buffer_offset + (ssize_t)sizeof(struct linux_dirent64) >
- nread ||
- next_ldir->d_reclen <
- offsetof(struct linux_dirent64, d_name) + 1 ||
- buffer_offset + next_ldir->d_reclen > nread ||
- next_ldir->d_reclen == 0) {
+ if (buffer_offset + (ssize_t)sizeof(struct linux_dirent64) > nread ||
+ next_ldir->d_reclen < offsetof(struct linux_dirent64, d_name) + 1 ||
+ buffer_offset + next_ldir->d_reclen > nread || next_ldir->d_reclen == 0) {
break;
}