Skip to contentSkip to conversation

mm, vmpressure: use kstrndup instead of kmalloc+strncpy

PATCH In review Andy Shevchenko 2 participants 2 messages May 3, 2018 – May 4, 2018

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

PATCH 1/1 mm, vmpressure: use kstrndup instead of kmalloc+strncpy

Using kstrndup() simplifies the code.

---
 mm/vmpressure.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/vmpressure.c +1 -2
index 85350ce2d25d..7142207224d3 100644
@@ -390,12 +390,11 @@ int vmpressure_register_event(struct mem_cgroup *memcg,
 	char *token;
 	int ret = 0;
 
-	spec_orig = spec = kzalloc(MAX_VMPRESSURE_ARGS_LEN + 1, GFP_KERNEL);
+	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
 	if (!spec) {
 		ret = -ENOMEM;
 		goto out;
 	}
-	strncpy(spec, args, MAX_VMPRESSURE_ARGS_LEN);
 
 	/* Find required level */
 	token = strsep(&spec, ",");
Signature · 1 line
2.17.0
Acked-by on v1 1/1
#1 lore raw
Quoted 1 line · Andy Shevchenko

Using kstrndup() simplifies the code.

if for nothing else then the len+1 being handled by kstrndup is an improvement.

Quoted 1 line
Quoted 24 lines
---
 mm/vmpressure.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/vmpressure.c +1 -2
index 85350ce2d25d..7142207224d3 100644
@@ -390,12 +390,11 @@ int vmpressure_register_event(struct mem_cgroup *memcg,
 	char *token;
 	int ret = 0;
 
-	spec_orig = spec = kzalloc(MAX_VMPRESSURE_ARGS_LEN + 1, GFP_KERNEL);
+	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
 	if (!spec) {
 		ret = -ENOMEM;
 		goto out;
 	}
-	strncpy(spec, args, MAX_VMPRESSURE_ARGS_LEN);
 
 	/* Find required level */
 	token = strsep(&spec, ",");
Signature · 1 line
2.17.0
Signature · 2 lines
Michal Hocko
SUSE Labs