Subject: bug#77621: coreutils-9.6.53-14af8 on OpenBSD 7.6

From: Bruno Haible <[email-masked]>
Date: Tue, 08 Apr 2025 12:22:15 +0200

Paul Eggert wrote:
> I don't see the problem on cfarm220.cfarm.net with the current coreutils

That's because you are not on the /dev/wd0a disk on that machine.

What I see by single-stepping through "ls -Z ." in gdb is:
1. f->scontext gets set to "?".
2. Upon the first entry to function file_has_aclinfo_cache,
   all 4 variables
     static int unsupported_return;
     static char *unsupported_scontext;
     static int unsupported_scontext_err;
     static dev_t unsupported_device;
   are 0 or NULL, respectively. (As expected, since it's the first call
   to this function.)
   f->stat.st_dev is 0 (since "." is on the /dev/wd0a disk and this device
   has major and minor number both 0).
   Thus the condition (f->stat_ok && f->stat.st_dev == unsupported_device)
   evaluates to true, and four lines later, in
     ai->scontext = unsupported_scontext;
   an scontext gets set to NULL.

The fix is obviously to ignore these 4 static variables if they have not
been initialized. Done through the attached patch, which fixes the crash.

>From 6cab0a182e3034b4533bcfe73fef74af37cc723d Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email-masked]>
Date: Tue, 8 Apr 2025 12:14:29 +0200
Subject: [PATCH] ls: Fix crash of "ls -Z ." on OpenBSD's /dev/wd0a disk

* src/ls.c (file_has_aclinfo_cache): Add new static variable
'unsupported_cached'. Don't assume that device 0 never occurs.
---
 src/ls.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/ls.c b/src/ls.c
index 6690f7747..de5a1ae2b 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -3309,12 +3309,13 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f,
 {
   /* st_dev and associated info for the most recently processed device
      for which file_has_aclinfo failed indicating lack of support.  */
+  static bool unsupported_cached /* = false */;
   static int unsupported_return;
   static char *unsupported_scontext;
   static int unsupported_scontext_err;
   static dev_t unsupported_device;
 
-  if (f->stat_ok && f->stat.st_dev == unsupported_device)
+  if (f->stat_ok && unsupported_cached && f->stat.st_dev == unsupported_device)
     {
       ai->buf = ai->u.__gl_acl_ch;
       ai->size = 0;
@@ -3330,6 +3331,7 @@ file_has_aclinfo_cache (char const *file, struct fileinfo *f,
   if (f->stat_ok && n <= 0 && !acl_errno_valid (err)
       && (!(flags & ACL_GET_SCONTEXT) || !acl_errno_valid (ai->scontext_err)))
     {
+      unsupported_cached = true;
       unsupported_return = n;
       unsupported_scontext = ai->scontext;
       unsupported_scontext_err = ai->scontext_err;
-- 
2.43.0