Skip to contentSkip to conversation

drivers/hwtracing/intel_th: Replace kzalloc(... strlen()...) with kzalloc_flex()

PATCH New [email protected] 1 participant 1 message Jun 8

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

PATCH 1/1 drivers/hwtracing/intel_th: Replace kzalloc(... strlen()...) with kzalloc_flex()

From: David Laight <[email protected]>

Save the result of the strlen() and use memcpy() instead of strcpy() to copy the string.

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

 drivers/hwtracing/intel_th/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hwtracing/intel_th/core.c +3 -2
index 3924e63e2eee..711a54e22e87 100644
@@ -374,20 +374,21 @@ intel_th_device_alloc(struct intel_th *th, unsigned int type, const char *name,
 {
 	struct device *parent;
 	struct intel_th_device *thdev;
+	size_t name_len = strlen(name) + 1;
 
 	if (type == INTEL_TH_OUTPUT)
 		parent = &th->hub->dev;
 	else
 		parent = th->dev;
 
-	thdev = kzalloc(sizeof(*thdev) + strlen(name) + 1, GFP_KERNEL);
+	thdev = kzalloc_flex(*thdev, name, name_len);
 	if (!thdev)
 		return NULL;
 
 	thdev->id = id;
 	thdev->type = type;
 
-	strcpy(thdev->name, name);
+	memcpy(thdev->name, name, name_len);
 	device_initialize(&thdev->dev);
 	thdev->dev.bus = &intel_th_bus;
 	thdev->dev.type = intel_th_device_type[type];
Signature · 1 line
2.39.5