git: 60a521a8941c - releng/15.1 - stat: fix use of devname(3)

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Thu, 07 May 2026 20:19:15 UTC
The branch releng/15.1 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=60a521a8941c4b0386dff3a925bae431b3064fcb

commit 60a521a8941c4b0386dff3a925bae431b3064fcb
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-05-01 03:00:26 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-05-07 20:18:48 +0000

    stat: fix use of devname(3)
    
    Besides being a little hard to parse through visually, this had its own
    bug of inspecting st->st_mode to determine what to pass to devname(3),
    which is only correct for st_rdev.
    
    For st_dev, you're likely to be looking at files or directories and
    attempting to assess what device they're located on, so the mode is
    meaningless- we just have to assume that our filesystems are on
    character devices and attempt to resolve st_dev as such.
    
    Approved by:    re (cperciva)
    Reviewed by:    des, kib (previous version)
    Differential Revision:  https://reviews.freebsd.org/D56565
    
    (cherry picked from commit 4d4acdbfc22c84081037f31cff4fb03d18373036)
    
    stat: The devname test case requires root
    
    Fixes:          4d4acdbfc22c ("stat: fix use of devname(3)")
    (cherry picked from commit 72b1aae09bf0bcc01c76df757699e27ad7cf7ecc)
    
    stat: Set the timezone before testing -t flag
    
    The test assumes UTC, which is what I use on my development systems and
    clearly what is used on our CI runners.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D56836
    
    (cherry picked from commit 49e496d2776870fb36ed8ea4c8139b5eb9f7f747)
    
    stat: Expand devname test case
    
    Test what happens when we ask for the rdev of a non-device.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D56838
    
    (cherry picked from commit 2c88636e0e7a0316d5e6d146874bdb2751f75c40)
    (cherry picked from commit 88f5520fa564ef1050bab6e091ead4040eb6d170)
---
 usr.bin/stat/stat.c             | 14 +++++++++++---
 usr.bin/stat/tests/stat_test.sh | 40 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c
index 0ed5d3ae5b53..ad2b3f9fb3d1 100644
--- a/usr.bin/stat/stat.c
+++ b/usr.bin/stat/stat.c
@@ -650,6 +650,7 @@ format1(const struct stat *st,
 	struct timespec ts;
 	struct tm *tm;
 	int l, small, formats;
+	mode_t dtype;
 
 	tsp = NULL;
 	formats = 0;
@@ -665,9 +666,16 @@ format1(const struct stat *st,
 		small = (sizeof(st->st_dev) == 4);
 		data = (what == SHOW_st_dev) ? st->st_dev : st->st_rdev;
 #if HAVE_DEVNAME
-		sdata = devname(what == SHOW_st_dev ? st->st_dev :
-		    st->st_rdev, S_ISCHR(st->st_mode) ? S_IFCHR :
-		    (S_ISBLK(st->st_mode) ? S_IFBLK : 0));
+		switch (what) {
+		case SHOW_st_dev:
+			dtype = S_IFCHR;
+			break;
+		case SHOW_st_rdev:
+			dtype = st->st_mode & (S_IFCHR | S_IFBLK);
+			break;
+		}
+
+		sdata = devname(data, dtype);
 #endif /* HAVE_DEVNAME */
 		if (hilo == HIGH_PIECE) {
 			data = major(data);
diff --git a/usr.bin/stat/tests/stat_test.sh b/usr.bin/stat/tests/stat_test.sh
index 6043686396be..aa8563c62ccc 100755
--- a/usr.bin/stat/tests/stat_test.sh
+++ b/usr.bin/stat/tests/stat_test.sh
@@ -1,7 +1,7 @@
 #
 # Copyright (c) 2017 Dell EMC
 # All rights reserved.
-# Copyright (c) 2025 Klara, Inc.
+# Copyright (c) 2025-2026 Klara, Inc.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -25,6 +25,9 @@
 # SUCH DAMAGE.
 #
 
+: ${CHKPATH:="mnt"}
+: ${NODEV:="#NODEV"}
+
 atf_test_case F_flag
 F_flag_head()
 {
@@ -232,9 +235,9 @@ t_flag_head()
 {
 	atf_set	"descr" "Verify the output format for -t"
 }
-
 t_flag_body()
 {
+	export TZ=UTC
 	atf_check touch foo
 	atf_check touch -d 1970-01-01T00:00:42 foo
 	atf_check -o inline:'42\n' \
@@ -301,6 +304,38 @@ x_flag_body()
 	done
 }
 
+atf_test_case devname cleanup
+devname_head()
+{
+	atf_set	"descr" "Verify that %Sd outputs a device name"
+	atf_set "require.user" "root"
+}
+devname_body()
+{
+	local devname devpath
+
+	atf_check -o save:dev mdconfig -t malloc -s 16M
+	read devname < dev
+	devpath="/dev/$devname"
+	atf_check -o not-empty newfs "$devpath"
+
+	atf_check mkdir "$CHKPATH"
+	atf_check mount "$devpath" "$CHKPATH"
+
+	atf_check -o inline:"$devname\n" stat -f '%Sd' "$CHKPATH"
+	atf_check -o inline:"$devname\n" stat -f '%Sr' "$devpath"
+	atf_check -o inline:"$NODEV\n" stat -f '%Sr' "$CHKPATH"
+}
+devname_cleanup()
+{
+	if [ -d "$CHKPATH" ]; then
+		umount "$CHKPATH" || true
+	fi
+	if [ -f dev ]; then
+		mdconfig -d -u $(cat dev) || true
+	fi
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case F_flag
@@ -315,4 +350,5 @@ atf_init_test_cases()
 	atf_add_test_case s_flag
 	atf_add_test_case t_flag
 	atf_add_test_case x_flag
+	atf_add_test_case devname
 }