[v1,2/3] lib/string_helpers: Change returned value of the strreplace()
Commit Message
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
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
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!
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)
@@ -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);
@@ -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;