From 37b085e567c9c16473ec0d9285ed3a47e861dc24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Yhuel?= Date: Sun, 9 Mar 2025 19:12:14 +0000 Subject: [PATCH] dd: fix error detection with "nocache" flag * NEWS: Mention the bug fix. * src/dd.c (invalidate_cache): Adjust to the unusual error propagation sematics of posix_fadvise(). --- NEWS | 4 ++++ src/dd.c | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 8d82bc3525..902bdaf0e3 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,10 @@ GNU coreutils NEWS -*- outline -*- 'cksum -a crc' misbehaved on aarch64 with 32-bit uint_fast32_t. [bug introduced in coreutils-9.6] + dd with the 'nocache' flag will now detect all failures to drop the + cache for the whole file. Previously it may have erroneously succeeded. + [bug introduced with the "nocache" feature in coreutils-8.11] + 'ls -Z dir' would crash. [bug introduced in coreutils-9.6] diff --git a/src/dd.c b/src/dd.c index 4e914336b9..d549105169 100644 --- a/src/dd.c +++ b/src/dd.c @@ -1027,7 +1027,8 @@ cache_round (int fd, off_t len) /* Discard the cache from the current offset of either STDIN_FILENO or STDOUT_FILENO. - Return true on success. */ + Return true on success. + Return false on failure, with errno set. */ static bool invalidate_cache (int fd, off_t len) @@ -1087,12 +1088,13 @@ invalidate_cache (int fd, off_t len) if (clen == 0) offset -= offset % page_size; adv_ret = posix_fadvise (fd, offset, clen, POSIX_FADV_DONTNEED); + errno = adv_ret; #else errno = ENOTSUP; #endif } - return adv_ret != -1 ? true : false; + return adv_ret == 0; } /* Read from FD into the buffer BUF of size SIZE, processing any -- 2.47.3