On 16/02/2026 22:05, Collin Funk wrote:
Pádraig Brady <[email protected]> writes:
Hi Chris!
I've pushed that now, thank you.
Thanks for the fix!
I'll see if I can trigger it with a test and if it warrants a NEWS entry later.
We initially allocate 2 * MIN (PATH_MAX, 16 * 1024) bytes, so I think it
would be pretty difficult to trigger this bug.
I've never run into it while testing 'pwd' in very deep directories.
Right it is an edge case, but...
Also, I think it would be difficult to add a test case since many
systems do not support directories that deep.
...we do actually have a test for this in tests/pwd/pwd-long.sh
However it doesn't trigger because the getcwd syscall
handles the long path and thus bypasses this fallback code in pwd.c
Note we didn't notice a test failure on any of the many systems we test on
(I re-reviewed all of Bruno's recent test runs for coreutils 9.10 for e.g.)
which suggests this fallback code is not run on any modern system.
I tweaked pwd.c on Solaris 11, FreeBSD, and Linux to not call the system
getcwd()
and the test failed on each with the fallback code without the fix.
I've attached an adjustment to the test to use strace to
avoid the getcwd syscall, which is enough on Linux at least to
trigger our fallback code to run. With that the test fails
without Chris's patch, and passes with it.
cheers,
Padraig
From 8a0d9b889f3db7912234b84a86e6ce68ee8e1da2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Tue, 17 Feb 2026 00:18:48 +0000
Subject: [PATCH] tests: pwd: ensure our getcwd fallback is tested
* tests/pwd/pwd-long.sh: Avoid the getcwd syscall,
to ensure our fallback getcwd code is exercised.
* NEWS: Mention the recent bug fix in pwd.
---
NEWS | 4 ++++
tests/pwd/pwd-long.sh | 12 +++++++++++-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/NEWS b/NEWS
index 6087494f3..179deca90 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,10 @@ GNU coreutils NEWS -*- outline -*-
'kill --help' now has links to valid anchors in the html manual.
[bug introduced in coreutils-9.10]
+ 'pwd' on ancient systems will no longer overflow a buffer
+ when operating in deep paths longer than twice the system PATH_MAX.
+ [bug introduced in coreutils-9.6]
+
** New Features
'date --date' now parses dot delimited dd.mm.yy format common in Europe.
diff --git a/tests/pwd/pwd-long.sh b/tests/pwd/pwd-long.sh
index 734c2db6c..2a010f2f2 100755
--- a/tests/pwd/pwd-long.sh
+++ b/tests/pwd/pwd-long.sh
@@ -19,6 +19,7 @@
. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ pwd
+uses_strace_
require_readable_root_
require_perl_
@@ -26,9 +27,18 @@ require_perl_
ARGV_0=$0
export ARGV_0
+# Disable the getcwd syscall if possible, so more of our code is exercised.
+if strace -f -o /dev/null -e 'getcwd' -e fault=all:error=ENOSYS true; then
+ no_sys_getcwd() {
+ strace -f -o /dev/null -e 'getcwd' -e fault=all:error=ENOSYS "$@"
+ }
+else
+ no_sys_getcwd() { "$@"; }
+fi
+
# Don't use CuTmpdir here, since File::Temp's use of rmtree can't
# remove the deep tree we create.
-$PERL -Tw -I"$abs_srcdir/tests" -MCuSkip -- - <<\EOF
+no_sys_getcwd $PERL -Tw -I"$abs_srcdir/tests" -MCuSkip -- - <<\EOF
# Show that pwd works even when the length of the resulting
# directory name is longer than PATH_MAX.
--
2.52.0