From: Paul Eggert Date: Sun, 20 Jul 2025 16:37:51 +0000 (-0700) Subject: Refactor by coalescing MAX_RW_COUNT definitions X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=26034f34c7bf07a10755973e440fd02ddc966db2;p=emacs.git Refactor by coalescing MAX_RW_COUNT definitions * src/emacs.c (read_full): Simplify by assuming MAX_RW_COUNT is defined. * src/lisp.h (MAX_RW_COUNT): Move here from src/sysdep.c. * src/pdumper.c (dump_read_all): Use MAX_RW_COUNT rather than defining our own equivalent. (cherry picked from commit 46314aef87fbda25e0a7fb5055624dc1c6e90ad9) --- diff --git a/src/emacs.c b/src/emacs.c index cf8f4bd63f7..6e8ef83a81b 100644 --- a/src/emacs.c +++ b/src/emacs.c @@ -1120,16 +1120,7 @@ read_full (int fd, void *buffer, ptrdiff_t size) eassert (0 <= fd); eassert (buffer != NULL); eassert (0 <= size); - enum - { - /* See MAX_RW_COUNT in sysdep.c. */ -#ifdef MAX_RW_COUNT - max_size = MAX_RW_COUNT -#else - max_size = INT_MAX >> 18 << 18 -#endif - }; - if (PTRDIFF_MAX < size || max_size < size) + if (max (PTRDIFF_MAX, MAX_RW_COUNT) < size) { errno = EFBIG; return -1; diff --git a/src/lisp.h b/src/lisp.h index 452265139c5..95dbe2b83c8 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -5297,6 +5297,17 @@ maybe_disable_address_randomization (int argc, char **argv) return argc; } #endif +/* Maximum number of bytes to read or write in a single system call. + This works around a serious bug in Linux kernels before 2.6.16; see + + and see Linux kernel commit e28cc71572da38a5a12c1cfe4d7032017adccf69. + It's likely to work around similar bugs in other operating systems, so do it + on all platforms. Round INT_MAX down to a page size, with the conservative + assumption that page sizes are at most 2**18 bytes (any kernel with a + page size larger than that shouldn't have the bug). */ +#ifndef MAX_RW_COUNT +# define MAX_RW_COUNT (INT_MAX >> 18 << 18) +#endif extern int emacs_exec_file (char const *, char *const *, char *const *); extern void init_standard_fds (void); extern char *emacs_get_current_dir_name (void); diff --git a/src/pdumper.c b/src/pdumper.c index b3de90bfa03..c02808a4e32 100644 --- a/src/pdumper.c +++ b/src/pdumper.c @@ -5346,10 +5346,7 @@ dump_read_all (int fd, void *buf, size_t bytes_to_read) size_t bytes_read = 0; while (bytes_read < bytes_to_read) { - /* Some platforms accept only int-sized values to read. - Round this down to a page size (see MAX_RW_COUNT in sysdep.c). */ - int max_rw_count = INT_MAX >> 18 << 18; - int chunk_to_read = min (bytes_to_read - bytes_read, max_rw_count); + int chunk_to_read = min (bytes_to_read - bytes_read, MAX_RW_COUNT); ssize_t chunk = read (fd, (char *) buf + bytes_read, chunk_to_read); if (chunk < 0) return chunk; diff --git a/src/sysdep.c b/src/sysdep.c index fee068d541f..d01cca1435d 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -2714,17 +2714,6 @@ emacs_fchmodat (int fd, const char *path, mode_t mode, int flags) #endif /* !(defined HAVE_ANDROID && !defined ANDROID_STUBIFY) */ } -/* Maximum number of bytes to read or write in a single system call. - This works around a serious bug in Linux kernels before 2.6.16; see - . - It's likely to work around similar bugs in other operating systems, so do it - on all platforms. Round INT_MAX down to a page size, with the conservative - assumption that page sizes are at most 2**18 bytes (any kernel with a - page size larger than that shouldn't have the bug). */ -#ifndef MAX_RW_COUNT -#define MAX_RW_COUNT (INT_MAX >> 18 << 18) -#endif - /* Verify that MAX_RW_COUNT fits in the relevant standard types. */ #ifndef SSIZE_MAX # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)