linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/5] lib: string.c:  Added a function strzcpy
@ 2014-10-18 22:03 Rickard Strandqvist
  2014-10-19  1:38 ` Joe Perches
  0 siblings, 1 reply; 4+ messages in thread
From: Rickard Strandqvist @ 2014-10-18 22:03 UTC (permalink / raw)
  To: Grant Likely, Andrew Morton
  Cc: Rickard Strandqvist, Andi Kleen, Dan Carpenter, linux-kernel

Added a function strzcpy which works the same as strncpy,
but guaranteed to produce the trailing null character.

There are many places in the code where strncpy used although it
must be zero terminated, and switching to strlcpy is not an option
because the string must nonetheless be fyld with zero characters.

Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
 include/linux/string.h |    1 +
 lib/string.c           |   31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index d36977e..d789ee5e 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -22,6 +22,7 @@ extern char * strcpy(char *,const char *);
 #ifndef __HAVE_ARCH_STRNCPY
 extern char * strncpy(char *,const char *, __kernel_size_t);
 #endif
+extern char *strzcpy(char *, const char *, __kernel_size_t);
 #ifndef __HAVE_ARCH_STRLCPY
 size_t strlcpy(char *, const char *, size_t);
 #endif
diff --git a/lib/string.c b/lib/string.c
index f3c6ff5..582a832 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -134,6 +134,37 @@ char *strncpy(char *dest, const char *src, size_t count)
 EXPORT_SYMBOL(strncpy);
 #endif
 
+/**
+ * strzcpy - Copy a length-limited, C-string
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @count: The maximum number of bytes to copy
+ *
+ * The result is %NUL-terminated,
+ * as long as count is greater than zero.
+ *
+ * In the case where the length of @src is less than  that  of
+ * count, the remainder of @dest will be padded with %NUL.
+ *
+ */
+char *strzcpy(char *dest, const char *src, size_t count)
+{
+	char *tmp = dest;
+
+	while (count) {
+		if ((*tmp = *src) != 0)
+			src++;
+		tmp++;
+		count--;
+	}
+
+	if (dest != tmp)
+		*--tmp = '\0';
+
+	return dest;
+}
+EXPORT_SYMBOL(strzcpy);
+
 #ifndef __HAVE_ARCH_STRLCPY
 /**
  * strlcpy - Copy a C-string into a sized buffer
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/5] lib: string.c:  Added a function strzcpy
  2014-10-18 22:03 [PATCH 1/5] lib: string.c: Added a function strzcpy Rickard Strandqvist
@ 2014-10-19  1:38 ` Joe Perches
  2014-10-19 12:19   ` Rickard Strandqvist
  0 siblings, 1 reply; 4+ messages in thread
From: Joe Perches @ 2014-10-19  1:38 UTC (permalink / raw)
  To: Rickard Strandqvist
  Cc: Grant Likely, Andrew Morton, Andi Kleen, Dan Carpenter, linux-kernel

On Sun, 2014-10-19 at 00:03 +0200, Rickard Strandqvist wrote:
> Added a function strzcpy which works the same as strncpy,
> but guaranteed to produce the trailing null character.
> 
> There are many places in the code where strncpy used although it
> must be zero terminated, and switching to strlcpy is not an option
> because the string must nonetheless be fyld with zero characters.
[]
> diff --git a/lib/string.c b/lib/string.c
[]
> +char *strzcpy(char *dest, const char *src, size_t count)
> +{
> +	char *tmp = dest;
> +
> +	while (count) {
> +		if ((*tmp = *src) != 0)
> +			src++;
> +		tmp++;
> +		count--;
> +	}
> +
> +	if (dest != tmp)
> +		*--tmp = '\0';
> +
> +	return dest;
> +}

why not

char *strzcpy(char *dest, const char *src, size_t count)
{
	strncpy(dest, src, count)
	if (count)
		dest[count - 1] = 0; /* or '\0' or whatever */

	return dest;
}

maybe use static inline too.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/5] lib: string.c: Added a function strzcpy
  2014-10-19  1:38 ` Joe Perches
@ 2014-10-19 12:19   ` Rickard Strandqvist
  2014-10-19 15:05     ` Joe Perches
  0 siblings, 1 reply; 4+ messages in thread
From: Rickard Strandqvist @ 2014-10-19 12:19 UTC (permalink / raw)
  To: Joe Perches
  Cc: Grant Likely, Andrew Morton, Andi Kleen, Dan Carpenter, linux-kernel

2014-10-19 3:38 GMT+02:00 Joe Perches <joe@perches.com>:
> On Sun, 2014-10-19 at 00:03 +0200, Rickard Strandqvist wrote:
>> Added a function strzcpy which works the same as strncpy,
>> but guaranteed to produce the trailing null character.
>>
>> There are many places in the code where strncpy used although it
>> must be zero terminated, and switching to strlcpy is not an option
>> because the string must nonetheless be fyld with zero characters.
> []
>> diff --git a/lib/string.c b/lib/string.c
> []
>> +char *strzcpy(char *dest, const char *src, size_t count)
>> +{
>> +     char *tmp = dest;
>> +
>> +     while (count) {
>> +             if ((*tmp = *src) != 0)
>> +                     src++;
>> +             tmp++;
>> +             count--;
>> +     }
>> +
>> +     if (dest != tmp)
>> +             *--tmp = '\0';
>> +
>> +     return dest;
>> +}
>
> why not
>
> char *strzcpy(char *dest, const char *src, size_t count)
> {
>         strncpy(dest, src, count)
>         if (count)
>                 dest[count - 1] = 0; /* or '\0' or whatever */
>
>         return dest;
> }
>
> maybe use static inline too.
>

Hi Joe

Yes this solution have also been discussed.
https://lkml.org/lkml/2014/10/16/682

Very possible that it is a better solution.
The code that I use in strzcpy is not the way I'd written it, but is
the same as in strncpy now.

But as I understand it the real strncpy code is normally highly
optimized for the hardware it runs on.
Ex: arch/x86/lib/string_32.c

But missing for x86 64 bit and Arm..?


Kind regards
Rickard Strandqvist

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/5] lib: string.c: Added a function strzcpy
  2014-10-19 12:19   ` Rickard Strandqvist
@ 2014-10-19 15:05     ` Joe Perches
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Perches @ 2014-10-19 15:05 UTC (permalink / raw)
  To: Rickard Strandqvist
  Cc: Grant Likely, Andrew Morton, Andi Kleen, Dan Carpenter, linux-kernel

On Sun, 2014-10-19 at 14:19 +0200, Rickard Strandqvist wrote:
> 2014-10-19 3:38 GMT+02:00 Joe Perches <joe@perches.com>:
> > On Sun, 2014-10-19 at 00:03 +0200, Rickard Strandqvist wrote:
> >> Added a function strzcpy which works the same as strncpy,
> >> but guaranteed to produce the trailing null character.
> >>
> >> There are many places in the code where strncpy used although it
> >> must be zero terminated, and switching to strlcpy is not an option
> >> because the string must nonetheless be fyld with zero characters.
> > []
> >> diff --git a/lib/string.c b/lib/string.c
> > []
> >> +char *strzcpy(char *dest, const char *src, size_t count)
> >> +{
> >> +     char *tmp = dest;
> >> +
> >> +     while (count) {
> >> +             if ((*tmp = *src) != 0)
> >> +                     src++;
> >> +             tmp++;
> >> +             count--;
> >> +     }
> >> +
> >> +     if (dest != tmp)
> >> +             *--tmp = '\0';
> >> +
> >> +     return dest;
> >> +}
> >
> > why not
> >
> > char *strzcpy(char *dest, const char *src, size_t count)
> > {
> >         strncpy(dest, src, count)
> >         if (count)
> >                 dest[count - 1] = 0; /* or '\0' or whatever */
> >
> >         return dest;
> > }
> >
> > maybe use static inline too.
> >
> 
> Hi Joe
> 
> Yes this solution have also been discussed.
> https://lkml.org/lkml/2014/10/16/682
> 
> Very possible that it is a better solution.
> The code that I use in strzcpy is not the way I'd written it, but is
> the same as in strncpy now.
> 
> But as I understand it the real strncpy code is normally highly
> optimized for the hardware it runs on.
> Ex: arch/x86/lib/string_32.c

The fact that optimized strncpy variants exist
for any platform is the argument _for_ using it
in strzcpy.

> But missing for x86 64 bit and Arm..?




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-10-19 15:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-18 22:03 [PATCH 1/5] lib: string.c: Added a function strzcpy Rickard Strandqvist
2014-10-19  1:38 ` Joe Perches
2014-10-19 12:19   ` Rickard Strandqvist
2014-10-19 15:05     ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).