Skip to contentSkip to conversation

platform/x86/intel/uncore-freq: String handling improvements

PATCH In review Kaushlendra Kumar 2 participants 4 messages Dec 30, 2025 – Jan 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover platform/x86/intel/uncore-freq: String handling improvements

This series replaces unsafe/deprecated string formatting functions in the Intel uncore frequency driver with their safer alternatives.

Patch 1 replaces sprintf() with scnprintf() in device name formatting to follow kernel best practices.

Patch 2 replaces sprintf() with sysfs_emit() in sysfs show functions for proper buffer bounds checking.

v2:
- Patch 1/2: Use scnprintf() per reviewer suggestion

Kaushlendra Kumar (2):
platform/x86/intel/uncore-freq: Replace sprintf() with scnprintf() platform/x86/intel/uncore-freq: Replace sprintf() with sysfs_emit()

 .../uncore-frequency/uncore-frequency-common.c      | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 platform/x86/intel/uncore-freq: Replace sprintf() with scnprintf()

Replace unbounded sprintf() calls with scnprintf() to prevent potential buffer overflows when formatting device names. While the current format strings cannot overflow the buffer, using scnprintf() follows kernel best practices for string formatting.

---
 .../x86/intel/uncore-frequency/uncore-frequency-common.c     | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/platform/x86/intel/uncore-frequency/uncore-frequency-common.c +3 -2
index 65897fae17df..e9495ac5ecd0 100644
@@ -269,9 +269,10 @@ int uncore_freq_add_entry(struct uncore_data *data, int cpu)
 			goto uncore_unlock;
 
 		data->instance_id = ret;
-		sprintf(data->name, "uncore%02d", ret);
+		scnprintf(data->name, sizeof(data->name), "uncore%02d", ret);
 	} else {
-		sprintf(data->name, "package_%02d_die_%02d", data->package_id, data->die_id);
+		scnprintf(data->name, sizeof(data->name), "package_%02d_die_%02d",
+			  data->package_id, data->die_id);
 	}
 
 	uncore_read(data, &data->initial_min_freq_khz, UNCORE_INDEX_MIN_FREQ);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 platform/x86/intel/uncore-freq: Replace sprintf() with sysfs_emit()

In reply to Kaushlendra Kumar

Replace sprintf() with sysfs_emit() in sysfs show functions. The sysfs_emit() function is the preferred way to format sysfs output as it ensures proper buffer bounds checking and correct return values.

---
 .../x86/intel/uncore-frequency/uncore-frequency-common.c  | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/platform/x86/intel/uncore-frequency/uncore-frequency-common.c +4 -4
index e9495ac5ecd0..7070c94324e0 100644
@@ -26,21 +26,21 @@ static ssize_t show_domain_id(struct kobject *kobj, struct kobj_attribute *attr,
 {
 	struct uncore_data *data = container_of(attr, struct uncore_data, domain_id_kobj_attr);
 
-	return sprintf(buf, "%u\n", data->domain_id);
+	return sysfs_emit(buf, "%u\n", data->domain_id);
 }
 
 static ssize_t show_fabric_cluster_id(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
 	struct uncore_data *data = container_of(attr, struct uncore_data, fabric_cluster_id_kobj_attr);
 
-	return sprintf(buf, "%u\n", data->cluster_id);
+	return sysfs_emit(buf, "%u\n", data->cluster_id);
 }
 
 static ssize_t show_package_id(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
 	struct uncore_data *data = container_of(attr, struct uncore_data, package_id_kobj_attr);
 
-	return sprintf(buf, "%u\n", data->package_id);
+	return sysfs_emit(buf, "%u\n", data->package_id);
 }
 
 #define MAX_UNCORE_AGENT_TYPES	4
@@ -77,7 +77,7 @@ static ssize_t show_attr(struct uncore_data *data, char *buf, enum uncore_index
 	if (ret)
 		return ret;
 
-	return sprintf(buf, "%u\n", value);
+	return sysfs_emit(buf, "%u\n", value);
 }
 
 static ssize_t store_attr(struct uncore_data *data, const char *buf, ssize_t count,
Signature · 1 line
2.34.1
maintainer
#3 lore raw
In reply to Kaushlendra Kumar
Quoted 10 lines · Kaushlendra Kumar

This series replaces unsafe/deprecated string formatting functions in the Intel uncore frequency driver with their safer alternatives.

Patch 1 replaces sprintf() with scnprintf() in device name formatting to follow kernel best practices.

Patch 2 replaces sprintf() with sysfs_emit() in sysfs show functions for proper buffer bounds checking.

[...]

Thank you for your contribution, it has been applied to my local review-ilpo-next branch. Note it will show up in the public platform-drivers-x86/review-ilpo-next branch only once I've pushed my local branch there, which might take a while.

The list of commits applied:
[1/2] platform/x86/intel/uncore-freq: Replace sprintf() with scnprintf()

      commit: 050a0aab15da9e1d14cd41073046d12d29f443c6
[2/2] platform/x86/intel/uncore-freq: Replace sprintf() with sysfs_emit()
      commit: 7b0a51955b0eadacd44350b6f13bf21ba675aed9

--
i.