* src/tail.c (tail_forever_inotify): Size the buffer based on NAME_MAX
instead of the operands. Reduce the scope of some variables.
* tests/tail/inotify-long-name.sh: New file.
* tests/local.mk (all_tests): Add the new test.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-S9L24ERC.
---
 NEWS                            |  6 ++++
 src/tail.c                      | 12 ++-----
 tests/local.mk                  |  1 +
 tests/tail/inotify-long-name.sh | 61 +++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+), 9 deletions(-)
 create mode 100755 tests/tail/inotify-long-name.sh

diff --git a/NEWS b/NEWS
index 4e2c1d65d..f52f19a89 100644
--- a/NEWS
+++ b/NEWS
@@ -131,6 +131,12 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   if events arrived faster than tail could process them.
   [bug introduced in coreutils-7.5]
 
+  'tail --follow' no longer sizes inotify event buffers based on the length of
+  file operands.  This would cause it to exit with an inotify error when an
+  event occurred on a large file, e.g., creating or modifying a file, in one of
+  the directories containing the operands.
+  [bug introduced in coreutils-7.5]
+
   'timeout --signal=ALRM -k' again honors the -k option.
   Similarly 'timeout --signal=CHLD' no longer reports "No child processes",
   or hangs as it has done since coreutils-8.27, once the command exits.
diff --git a/src/tail.c b/src/tail.c
index 945e129a4..6755d61c1 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -1534,9 +1534,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int 
n_files,
   bool found_unwatchable_dir = false;
   bool no_inotify_resources = false;
   struct File_spec *prev_fspec;
-  idx_t evlen = 0;
-  char *evbuf;
-  idx_t evbuf_off = 0;
 
   *parent_by_namep = NULL;
 
@@ -1569,10 +1566,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int 
n_files,
     {
       if (!f[i].ignore)
         {
-          idx_t fnlen = strlen (f[i].name);
-          if (evlen < fnlen)
-            evlen = fnlen;
-
           f[i].wd = -1;
 
           if (follow_mode == Follow_name)
@@ -1680,8 +1673,9 @@ tail_forever_inotify (int wd, struct File_spec *f, int 
n_files,
   if (debug)
     error (0, 0, "%s", _("using notification mode"));
 
-  evlen += sizeof (struct inotify_event) + 1;
-  evbuf = ximalloc (evlen);
+  idx_t evbuf_off = 0;
+  idx_t evlen = sizeof (struct inotify_event) + NAME_MAX + 1;
+  char *evbuf = ximalloc (evlen);
 
   /* Wait for inotify events and handle them.  Events on directories
      ensure that watched files can be re-added when following by name.
diff --git a/tests/local.mk b/tests/local.mk
index 957cd9446..a66c2cb19 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -206,6 +206,7 @@ all_tests =                                 \
   tests/tail/inotify-rotate.sh                 \
   tests/tail/inotify-rotate-resources.sh       \
   tests/tail/inotify-dir-recreate.sh           \
+  tests/tail/inotify-long-name.sh              \
   tests/tail/inotify-only-regular.sh           \
   tests/tail/inotify-overflow.sh               \
   tests/tail/quote-headers.sh                  \
diff --git a/tests/tail/inotify-long-name.sh b/tests/tail/inotify-long-name.sh
new file mode 100755
index 000000000..39a21416c
--- /dev/null
+++ b/tests/tail/inotify-long-name.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+# Check that the inotify event buffers are allocated with a large enough size.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ tail
+require_inotify_supported_
+
+check_tail_output()
+{
+  local delay="$1"
+  grep "$tail_re" out > /dev/null ||
+    { sleep $delay; return 1; }
+}
+
+cleanup_() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; }
+
+# Make sure we can create a long file name.
+long_name=$(printf '%0255d' | tr '0' 'a') || framework_failure_
+touch "$long_name" || skip_ 'cannot create long file name'
+rm -f "$long_name" || framework_failure_
+
+# Create an empty file to tail.
+touch file || framework_failure_
+
+# Now tail it and make sure we are using notification mode.
+timeout 60 tail --debug -F file >out 2>&1 & pid=$!
+tail_re='tail: using notification mode' retry_delay_ check_tail_output .1 7 ||
+  { cat out; cleanup_; skip_ 'tail did not switch to notification mode'; }
+
+# Create a long file name and then write to the file being tailed.
+# coreutils-9.12 and earlier only allocated enough memory to follow
+# events for its operands.  Creating a file name longer than that would
+# cause it to fail and exit.
+touch "$long_name" &&
+echo abc >file || framework_failure_
+tail_re='^abc$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
+
+echo 1 >"$long_name" &&
+echo def >>file || framework_failure_
+tail_re='^def$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
+
+mv "$long_name" new  &&
+echo ghi >>file || framework_failure_
+tail_re='^ghi$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; }
+
+Exit $fail
-- 
2.56.0


Reply via email to