[v1,2/3] lib/string_helpers: Change returned value of the strreplace()

Message ID 20230322141206.56347-3-andriy.shevchenko@linux.intel.com
State New
Headers
Series lib/string_helpers et al.: Change return value of strreplace() |

Commit Message

Andy Shevchenko March 22, 2023, 2:12 p.m. UTC
  It's more useful to return the original string with strreplace(),
so it may be used like

	attr->name = strreplace(name, '/', '_');

While at it, amend the kernel documentation.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/string.h |  2 +-
 lib/string_helpers.c   | 10 +++++++---
 2 files changed, 8 insertions(+), 4 deletions(-)
  

Comments

Kees Cook March 22, 2023, 4:51 p.m. UTC | #1
On Wed, Mar 22, 2023 at 04:12:05PM +0200, Andy Shevchenko wrote:
> It's more useful to return the original string with strreplace(),

I found the use of "original" confusing here and in the comments. This
just returns arg 1, yes? i.e. it's not the original (unreplaced) string,
but rather just the string itself.

I agree, though, that's much more useful than a pointer to the end of
the string.

-Kees
  
Andy Shevchenko March 23, 2023, 12:26 p.m. UTC | #2
On Wed, Mar 22, 2023 at 09:51:22AM -0700, Kees Cook wrote:
> On Wed, Mar 22, 2023 at 04:12:05PM +0200, Andy Shevchenko wrote:
> > It's more useful to return the original string with strreplace(),
> 
> I found the use of "original" confusing here and in the comments. This
> just returns arg 1, yes? i.e. it's not the original (unreplaced) string,
> but rather just the string itself.

Yes.

Okay, I will try my best as non-native speaker to fix that for v2. Meanwhile
I'm open ears for the better suggestions.

> I agree, though, that's much more useful than a pointer to the end of
> the string.

Thank you!
  
David Laight March 23, 2023, 10:23 p.m. UTC | #3
From: Kees Cook
> Sent: 22 March 2023 16:51
> 
> On Wed, Mar 22, 2023 at 04:12:05PM +0200, Andy Shevchenko wrote:
> > It's more useful to return the original string with strreplace(),

Won't that break anything that is using the result?

> I found the use of "original" confusing here and in the comments. This
> just returns arg 1, yes? i.e. it's not the original (unreplaced) string,
> but rather just the string itself.
> 
> I agree, though, that's much more useful than a pointer to the end of
> the string.

If you want a pointer to the start of the string, you've
already got it.
Almost all the time you can do the assignment first.

But if you want a pointer to the end you'll need to scan it again.

I have a feeling that the reason many of the string functions
return the original pointer is a historic side effect of
the original implementation.
Going back to before C had a 'return' statement.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
  

Patch

diff --git a/include/linux/string.h b/include/linux/string.h
index a7bff7ed3cb0..cb0c24ce0826 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -169,7 +169,7 @@  static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
 #endif
 
 void *memchr_inv(const void *s, int c, size_t n);
-char *strreplace(char *s, char old, char new);
+char *strreplace(char *str, char old, char new);
 
 extern void kfree_const(const void *x);
 
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 230020a2e076..ab7b1577cbcf 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -979,14 +979,18 @@  EXPORT_SYMBOL(__sysfs_match_string);
 
 /**
  * strreplace - Replace all occurrences of character in string.
- * @s: The string to operate on.
+ * @str: The string to operate on.
  * @old: The character being replaced.
  * @new: The character @old is replaced with.
  *
- * Returns pointer to the nul byte at the end of @s.
+ * Replaces the each @old character with a @new one in the given string @str.
+ *
+ * Return: pointer to the original string @str.
  */
-char *strreplace(char *s, char old, char new)
+char *strreplace(char *str, char old, char new)
 {
+	char *s = str;
+
 	for (; *s; ++s)
 		if (*s == old)
 			*s = new;