Skip to contentSkip to conversation

locking/lockdep: Fix string truncation and length accounting in seq_stats()

PATCH New Boudewijn van der Heide 1 participant 1 message Dec 19, 2025 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/lockdep: Fix string truncation and length accounting in seq_stats()

GCC 14 reports a -Wformat-truncation warning when appending "#%d" and "/%d" to the lock class name in seq_stats(), as the buffer size was insufficient and the resulting length was tracked incorrectly.

Use scnprintf() with remaining-buffer accounting to safely append the suffixes and update namelen based on the actual number of characters written.

---
 kernel/locking/lockdep_proc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/locking/lockdep_proc.c +6 -4
index 1916db9aa46b..8fe977c32a3d 100644
@@ -496,12 +496,14 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 
 	namelen = strlen(name);
 	if (class->name_version > 1) {
-		snprintf(name+namelen, 3, "#%d", class->name_version);
-		namelen += 2;
+		namelen += scnprintf(name + namelen,
+				sizeof(name) - namelen, "#%d",
+				class->name_version);
 	}
 	if (class->subclass) {
-		snprintf(name+namelen, 3, "/%d", class->subclass);
-		namelen += 2;
+		namelen += scnprintf(name + namelen,
+				sizeof(name) - namelen, "/%d",
+				class->subclass);
 	}
 
 	if (stats->write_holdtime.nr) {
Signature · 1 line
2.47.3