Ensure io requests are always capped at 0x7ffff000

This gives us the Linux behavior across platforms.

Fixes #1189
This commit is contained in:
Justine Tunney 2024-05-26 16:53:13 -07:00
parent 6cf9b9e0fc
commit af3f62a71a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
10 changed files with 268 additions and 81 deletions

View file

@ -27,6 +27,7 @@
#include "libc/intrin/weaken.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/sock/sock.h"
#include "libc/stdio/sysparam.h"
#include "libc/sysv/errfuns.h"
/**
@ -39,6 +40,7 @@
*
* @param fd is open file descriptor
* @param buf is copied from, cf. copy_file_range(), sendfile(), etc.
* @param size is always saturated to 0x7ffff000 automatically
* @return [1..size] bytes on success, or -1 w/ errno; noting zero is
* impossible unless size was passed as zero to do an error check
* @raise EBADF if `fd` is negative or not an open file descriptor
@ -68,6 +70,10 @@ ssize_t write(int fd, const void *buf, size_t size) {
ssize_t rc;
BEGIN_CANCELATION_POINT;
// XNU and BSDs will EINVAL if requested bytes exceeds INT_MAX
// this is inconsistent with Linux which ignores huge requests
size = MIN(size, 0x7ffff000);
if (fd < 0) {
rc = ebadf();
} else if (IsAsan() && !__asan_is_valid(buf, size)) {