Skip to contentSkip to conversation

lib/parser.c: switch match_strdup() over to use kmemdup_nul()

PATCH In review Eric Biggers 2 participants 2 messages Aug 30, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/parser.c: switch match_strdup() over to use kmemdup_nul()

From: Eric Biggers <[email protected]>

This simplifies the code. No change in behavior.

---
 lib/parser.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
lib/parser.c +1 -5
index 3278958b472a..0142ef28f0eb 100644
@@ -327,10 +327,6 @@ EXPORT_SYMBOL(match_strlcpy);
  */
 char *match_strdup(const substring_t *s)
 {
-	size_t sz = s->to - s->from + 1;
-	char *p = kmalloc(sz, GFP_KERNEL);
-	if (p)
-		match_strlcpy(p, s, sz);
-	return p;
+	return kmemdup_nul(s->from, s->to - s->from, GFP_KERNEL);
 }
 EXPORT_SYMBOL(match_strdup);
Signature · 1 line
2.19.0.rc0.228.g281dcd1b4d0-goog
maintainer
#1 lore raw
Quoted 10 lines · On Thu, 30 Aug 2018 12:44:36 -0700 Eric Biggers <[email protected]> wrote:

From: Eric Biggers <[email protected]>

This simplifies the code. No change in behavior.

...

lib/parser.c +0 -0
@@ -327,10 +327,6 @@ EXPORT_SYMBOL(match_strlcpy);
  */

lib/parser.c EXPORT_SYMBOL(match_strlcpy); · line 332

Quoted 10 lines
lib/parser.c +1 -5
 char *match_strdup(const substring_t *s)
 {
-	size_t sz = s->to - s->from + 1;
-	char *p = kmalloc(sz, GFP_KERNEL);
-	if (p)
-		match_strlcpy(p, s, sz);
-	return p;
+	return kmemdup_nul(s->from, s->to - s->from, GFP_KERNEL);
 }
 EXPORT_SYMBOL(match_strdup);

Huh. I never noticed kmemdup_nul() fly past - it rather happened on the sly. We do have some fun goodies in there.

We could make match_strdup() an inline now. But that will probably produce a fatter kernel, as each callsite would then need to prepare three args rather than one.