[PATCH] hdparm: fix printing of ATA identification strings

Romain Guyard via busybox <[email protected]>
Newsgroups gmane.linux.busybox
Message-ID <[email protected]>
Hello!

In the hdparm applet, print_ascii() is intended to skip leading
spaces before printing ATA IDENTIFY strings, but its condition
is inverted, causing it to skip non-space characters instead.

This causes left-justified serial numbers and firmware revisions to be
printed as empty, and truncates model strings at their first embedded
space.

The attached patch fixes the condition and adds a regression test using
synthetic ATA IDENTIFY data. It was also tested on real hardware.

Thanks,

_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox
0001-hdparm-fix-ATA-identification-string-output.patch (text/x-patch, 4.4 KB)
From 76c7c39e031cf560de89425ae3b4d21018a84e37 Mon Sep 17 00:00:00 2001
From: Romain Guyard <[email protected]>
Date: Tue, 11 Aug 2026 13:06:10 +0900
Subject: [PATCH] hdparm: fix ATA identification string output

print_ascii() is intended to skip leading spaces before printing ATA
IDENTIFY strings, but the condition is inverted and skips non-space
characters instead.

As a result, left-justified strings such as serial numbers and firmware
revisions can be printed as empty, while strings containing embedded
spaces, such as model names, are truncated up to the first space.

Fix the condition so only leading spaces are skipped, and add a regression
test covering left-justified strings, embedded spaces, and leading-space
padding.

function                                             old     new   delta
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/0 up/down: 0/0)                 Total: 0 bytes

Signed-off-by: Romain Guyard <[email protected]>
---
 miscutils/hdparm.c     |  2 +-
 testsuite/hdparm.tests | 45 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100755 testsuite/hdparm.tests

diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c
index 8b844717f..e07ee6398 100644
--- a/miscutils/hdparm.c
+++ b/miscutils/hdparm.c
@@ -552,7 +552,7 @@ static void print_ascii(const char *p, int length)
 
 	length *= 2;
 	/* find first non-space & print it */
-	while (length && p[ofs] != ' ') {
+	while (length && p[ofs] == ' ') {
 		p++;
 		LE_ONLY(ofs = -ofs;)
 		length--;
diff --git a/testsuite/hdparm.tests b/testsuite/hdparm.tests
new file mode 100755
index 000000000..76823d59f
--- /dev/null
+++ b/testsuite/hdparm.tests
@@ -0,0 +1,45 @@
+#!/bin/sh
+# Copyright 2026 by Romain Guyard <[email protected]>
+# Licensed under GPLv2, see file LICENSE in this source tree.
+
+. ./testing.sh
+
+# testing "test name" "commands" "expected result" "file input" "stdin"
+
+# With no device argument and a non-tty stdin, hdparm parses a hex IDENTIFY
+# DEVICE block from stdin (identify_from_stdin) and prints it the way
+# "hdparm -I" does. That makes the ASCII field decoding testable without
+# a disk.
+#
+# The block below carries:
+#	words  10-19  serial number    "SN0123456789ABCDEF"  (no embedded space)
+#	words  23-26  firmware rev     "FW1234"
+#	words  27-46  model number     "EXAMPLE SSD 256GB"   (has one)
+#	words 176-195 media serial     "   MEDIASN00001"     (leading spaces)
+#
+# ATA strings are left-justified and space-padded. print_ascii() must skip
+# leading *spaces* only. It used to skip the leading run of *non-space*
+# characters instead, which emptied every field with no embedded space
+# (serial number, firmware revision) and truncated the model number at its
+# first space.
+
+testing "hdparm -I decodes ASCII fields" \
+	"hdparm | grep -E 'Number:|Revision:|Serial Num:' | sed 's/[[:space:]]*\$//'" \
+"\tModel Number:       EXAMPLE SSD 256GB\n\tSerial Number:      SN0123456789ABCDEF\n\tFirmware Revision:  FW1234\n\tMedia Serial Num:   MEDIASN00001\n" \
+	"" \
+"0040000000000000000000000000000000000000534e3031323334353637383941424344454620200000000000004657\
+3132333420204558414d504c452053534420323536474220202020202020202020202020202020202020202020200000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000002020204d45444941534e30303030312020202020202020202020202020202020\
+202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000000000000000000000000000a500000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000"
+
+exit $FAILCOUNT
-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.