Skip to content

Commit 1d2fc44

Browse files
committed
cap getdents length argument to INT_MAX
the linux syscall treats this argument as having type int, so passing extremely long buffer sizes would be misinterpreted by the kernel. since "short reads" are always acceptable, just cap it down. patch based on musl commit 3d178a7e2b75066593fbd5705742c5808395d90d
1 parent fc9e28e commit 1d2fc44

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

std/os/linux.zig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,22 @@ pub fn getcwd(buf: [*]u8, size: usize) usize {
106106
return syscall2(SYS_getcwd, @ptrToInt(buf), size);
107107
}
108108

109-
pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
110-
return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
109+
pub fn getdents(fd: i32, dirp: [*]u8, len: usize) usize {
110+
return syscall3(
111+
SYS_getdents,
112+
@bitCast(usize, isize(fd)),
113+
@ptrToInt(dirp),
114+
std.math.min(len, maxInt(c_int)),
115+
);
111116
}
112117

113-
pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
114-
return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
118+
pub fn getdents64(fd: i32, dirp: [*]u8, len: usize) usize {
119+
return syscall3(
120+
SYS_getdents64,
121+
@bitCast(usize, isize(fd)),
122+
@ptrToInt(dirp),
123+
std.math.min(len, maxInt(c_int)),
124+
);
115125
}
116126

117127
pub fn inotify_init1(flags: u32) usize {

0 commit comments

Comments
 (0)