this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Don't read more dirents from Linux than we can handle

This was causing some directory entries to be skipped because we were reading more directory entries from Linux than we had space for in the user provided buffer

+33 -4
+33 -4
src/kernel/emulation/linux/dirent/getdirentries.c
··· 3 3 #include "../errno.h" 4 4 #include <linux-syscalls/linux.h> 5 5 #include <sys/dirent.h> 6 + //#include "../simple.h" 7 + 8 + #define LINUX_SEEK_SET 0 9 + #define LINUX_SEEK_CUR 1 6 10 7 11 extern __SIZE_TYPE__ strlen(const char* s); 8 12 extern char* strcpy(char* dest, const char* src); ··· 12 16 # define min(a,b) ((a) < (b)) ? (a) : (b) 13 17 #endif 14 18 19 + #define ALIGN(x, alignment) (((x) + (alignment - 1)) & ~(alignment - 1)) 20 + #define BSD_DIRENT_MIN_SIZE ALIGN(sizeof(struct bsd_dirent) + 2, 4) 21 + #define BSD_DIRENT64_MIN_SIZE ALIGN(sizeof(struct bsd_dirent64) + 2, 4) 22 + #define LINUX_DIRENT64_MIN_SIZE ALIGN(sizeof(struct linux_dirent64) + 2, 8) 23 + 15 24 static inline void round_to_4(unsigned short* reclen) 16 25 { 17 26 if (*reclen & 3) ··· 25 34 long sys_getdirentries(int fd, char* ibuf, unsigned int len, long* basep) 26 35 { 27 36 int ret, bpos = 0, opos = 0; 28 - char buf[1024]; 37 + 38 + unsigned int max_entry_count = len / BSD_DIRENT_MIN_SIZE; 39 + 40 + // don't want to blow up the stack 41 + if (max_entry_count > 20) 42 + max_entry_count = 20; 43 + 44 + char buf[max_entry_count * LINUX_DIRENT64_MIN_SIZE]; 29 45 30 46 if (basep) 31 47 *basep = 0; 32 48 33 - ret = LINUX_SYSCALL(__NR_getdents64, fd, buf, min(len, sizeof(buf))); 49 + ret = LINUX_SYSCALL(__NR_getdents64, fd, buf, sizeof(buf)); 34 50 if (ret < 0) 35 51 return errno_linux_to_bsd(ret); 36 52 ··· 68 84 long sys_getdirentries64(int fd, char* ibuf, unsigned int len, long* basep) 69 85 { 70 86 int ret, bpos = 0, opos = 0; 71 - char buf[1024]; 87 + 88 + unsigned int max_entry_count = len / BSD_DIRENT64_MIN_SIZE; 89 + 90 + // don't want to blow up the stack 91 + if (max_entry_count > 20) 92 + max_entry_count = 20; 93 + 94 + char buf[max_entry_count * LINUX_DIRENT64_MIN_SIZE]; 72 95 73 96 if (basep) 74 97 *basep = 0; 75 98 76 - ret = LINUX_SYSCALL(__NR_getdents64, fd, buf, min(len, sizeof(buf))); 99 + ret = LINUX_SYSCALL(__NR_getdents64, fd, buf, sizeof(buf)); 77 100 if (ret < 0) 78 101 return errno_linux_to_bsd(ret); 79 102 ··· 103 126 opos += bsd->d_reclen; 104 127 bpos += l64->d_reclen; 105 128 } 129 + 130 + /* 131 + if (bpos < ret) { 132 + __simple_printf("bpos < ret; %d < %d", bpos, ret); 133 + } 134 + */ 106 135 107 136 return opos; 108 137 }