public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] 2.5.47: strdup()
@ 2002-11-16  6:21 Olaf Dietsche
  2002-11-16  6:25 ` Jeff Garzik
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Olaf Dietsche @ 2002-11-16  6:21 UTC (permalink / raw)
  To: linux-kernel; +Cc: trivial

This *untested* patch adds strdup(). There are about five or six
different strdup() implementations in various parts of the kernel.

Regards, Olaf.

diff -urN a/include/linux/string.h b/include/linux/string.h
--- a/include/linux/string.h	Sat Oct  5 18:42:33 2002
+++ b/include/linux/string.h	Sat Nov 16 03:09:42 2002
@@ -15,7 +15,7 @@
 extern char * strpbrk(const char *,const char *);
 extern char * strsep(char **,const char *);
 extern __kernel_size_t strspn(const char *,const char *);
-
+extern char *strdup(const char *);
 
 /*
  * Include machine specific inline routines
diff -urN a/kernel/ksyms.c b/kernel/ksyms.c
--- a/kernel/ksyms.c	Tue Nov 12 17:56:02 2002
+++ b/kernel/ksyms.c	Sat Nov 16 06:53:09 2002
@@ -576,6 +576,7 @@
 EXPORT_SYMBOL(strnicmp);
 EXPORT_SYMBOL(strspn);
 EXPORT_SYMBOL(strsep);
+EXPORT_SYMBOL(strdup);
 
 /* software interrupts */
 EXPORT_SYMBOL(tasklet_init);
diff -urN a/lib/string.c b/lib/string.c
--- a/lib/string.c	Sat Oct  5 18:42:33 2002
+++ b/lib/string.c	Sat Nov 16 07:18:16 2002
@@ -22,6 +22,7 @@
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
+#include <linux/slab.h>
 
 #ifndef __HAVE_ARCH_STRNICMP
 /**
@@ -479,6 +480,20 @@
 		s1++;
 	}
 	return NULL;
+}
+#endif
+
+#ifndef __HAVE_ARCH_STRDUP
+/**
+ * strdup - allocate memory and duplicate a string
+ */
+char *strdup(const char *s)
+{
+	char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
+	if (p)
+		strcpy(p, s);
+
+	return p;
 }
 #endif
 

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  6:21 [PATCH] 2.5.47: strdup() Olaf Dietsche
@ 2002-11-16  6:25 ` Jeff Garzik
  2002-11-16  6:31 ` Andrew Morton
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Jeff Garzik @ 2002-11-16  6:25 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: linux-kernel, trivial

Olaf Dietsche wrote:

> This *untested* patch adds strdup(). There are about five or six
> different strdup() implementations in various parts of the kernel.
>

> +#ifndef __HAVE_ARCH_STRDUP
> +/**
> + * strdup - allocate memory and duplicate a string
> + */
> +char *strdup(const char *s)
> +{
> +	char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
> +	if (p)
> +		strcpy(p, s);
> +
> +	return p;
>  }



Comments:

* arch-specific strdup is unlikely

* IMO safer to create a strndup, and then update all strdup callers to 
use strndup...



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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  6:21 [PATCH] 2.5.47: strdup() Olaf Dietsche
  2002-11-16  6:25 ` Jeff Garzik
@ 2002-11-16  6:31 ` Andrew Morton
  2002-11-16 15:42   ` Olaf Dietsche
  2002-11-16  6:41 ` William Lee Irwin III
  2002-11-17  0:08 ` Chris Wedgwood
  3 siblings, 1 reply; 12+ messages in thread
From: Andrew Morton @ 2002-11-16  6:31 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: linux-kernel, trivial

Olaf Dietsche wrote:
> 
> +char *strdup(const char *s)
> +{
> +       char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
> +       if (p)
> +               strcpy(p, s);
> +
> +       return p;
>  }

It's best to not assume GFP_KERNEL in there.  Make the caller
pass in the required allocation mode.

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  6:21 [PATCH] 2.5.47: strdup() Olaf Dietsche
  2002-11-16  6:25 ` Jeff Garzik
  2002-11-16  6:31 ` Andrew Morton
@ 2002-11-16  6:41 ` William Lee Irwin III
  2002-11-17  0:08 ` Chris Wedgwood
  3 siblings, 0 replies; 12+ messages in thread
From: William Lee Irwin III @ 2002-11-16  6:41 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: linux-kernel, trivial

On Sat, Nov 16, 2002 at 07:21:09AM +0100, Olaf Dietsche wrote:
+	char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);

I'm very concerned the implicit allocation will be abused and OOM
highmem boxen. Can you at least optionally highmem-allocate the buffers?


Thanks,
Bill

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

* RE: [PATCH] 2.5.47: strdup()
@ 2002-11-16  8:48 Peter Kjellerstedt
  2002-11-16 15:53 ` Olaf Dietsche
  0 siblings, 1 reply; 12+ messages in thread
From: Peter Kjellerstedt @ 2002-11-16  8:48 UTC (permalink / raw)
  To: 'Olaf Dietsche', linux-kernel

> -----Original Message-----
> From: Olaf Dietsche 
> [mailto:olaf.dietsche#list.linux-kernel@t-online.de] 
> Sent: Saturday, November 16, 2002 07:21
> To: linux-kernel@vger.kernel.org
> Cc: trivial@rustcorp.com.au
> Subject: [PATCH] 2.5.47: strdup()
> 
> This *untested* patch adds strdup(). There are about five or six
> different strdup() implementations in various parts of the kernel.
> 
> Regards, Olaf.

[snip]

> +#ifndef __HAVE_ARCH_STRDUP
> +/**
> + * strdup - allocate memory and duplicate a string
> + */
> +char *strdup(const char *s)
> +{
> +	char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
> +	if (p)
> +		strcpy(p, s);
> +
> +	return p;
>  }
>  #endif

You should make sure s != NULL before doing anything else.

//Peter

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  6:31 ` Andrew Morton
@ 2002-11-16 15:42   ` Olaf Dietsche
  2002-11-17 23:32     ` Werner Almesberger
  0 siblings, 1 reply; 12+ messages in thread
From: Olaf Dietsche @ 2002-11-16 15:42 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, trivial

Andrew Morton <akpm@digeo.com> writes:

> Olaf Dietsche wrote:
>> 
>> +char *strdup(const char *s)
>> +{
>> +       char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
>> +       if (p)
>> +               strcpy(p, s);
>> +
>> +       return p;
>>  }
>
> It's best to not assume GFP_KERNEL in there.  Make the caller
> pass in the required allocation mode.

Ok, here is my next try, trying to include your and the other
suggestions. As before, it compiles, but is untested.

Regards, Olaf.

diff -urN a/include/linux/string.h b/include/linux/string.h
--- a/include/linux/string.h	Sat Oct  5 18:42:33 2002
+++ b/include/linux/string.h	Sat Nov 16 16:23:40 2002
@@ -7,6 +7,7 @@
 
 #include <linux/types.h>	/* for size_t */
 #include <linux/stddef.h>	/* for NULL */
+#include <linux/gfp.h>		/* for GFP_KERNEL */
 
 #ifdef __cplusplus
 extern "C" {
@@ -15,7 +16,11 @@
 extern char * strpbrk(const char *,const char *);
 extern char * strsep(char **,const char *);
 extern __kernel_size_t strspn(const char *,const char *);
+extern void *kmemdup(const void *, size_t, int);
 
+static inline char *kstrdup(const char *s, int flags)
+	{ return kmemdup(s, strlen(s) + 1, flags); }
+static inline char *strdup(const char *s) { return kstrdup(s, GFP_KERNEL); }
 
 /*
  * Include machine specific inline routines
diff -urN a/kernel/ksyms.c b/kernel/ksyms.c
--- a/kernel/ksyms.c	Tue Nov 12 17:56:02 2002
+++ b/kernel/ksyms.c	Sat Nov 16 16:26:37 2002
@@ -576,6 +576,7 @@
 EXPORT_SYMBOL(strnicmp);
 EXPORT_SYMBOL(strspn);
 EXPORT_SYMBOL(strsep);
+EXPORT_SYMBOL(kmemdup);
 
 /* software interrupts */
 EXPORT_SYMBOL(tasklet_init);
diff -urN a/lib/string.c b/lib/string.c
--- a/lib/string.c	Sat Oct  5 18:42:33 2002
+++ b/lib/string.c	Sat Nov 16 16:22:46 2002
@@ -22,6 +22,7 @@
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
+#include <linux/slab.h>
 
 #ifndef __HAVE_ARCH_STRNICMP
 /**
@@ -479,6 +480,20 @@
 		s1++;
 	}
 	return NULL;
+}
+#endif
+
+#ifndef __HAVE_ARCH_KMEMDUP
+/**
+ * kmemdup - allocate memory and duplicate a string
+ */
+void *kmemdup(const void *s, size_t n, int flags)
+{
+	void *p = kmalloc(n, flags);
+	if (p)
+		memcpy(p, s, n);
+
+	return p;
 }
 #endif
 

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  8:48 Peter Kjellerstedt
@ 2002-11-16 15:53 ` Olaf Dietsche
  0 siblings, 0 replies; 12+ messages in thread
From: Olaf Dietsche @ 2002-11-16 15:53 UTC (permalink / raw)
  To: Peter Kjellerstedt; +Cc: linux-kernel

Peter Kjellerstedt <peter.kjellerstedt@axis.com> writes:

>> +char *strdup(const char *s)
>> +{
>> +	char *p = kmalloc(strlen(s) + 1, GFP_KERNEL);
>> +	if (p)
>> +		strcpy(p, s);
>> +
>> +	return p;
>>  }
>>  #endif
>
> You should make sure s != NULL before doing anything else.

Like strcpy(), it's the caller's responsibility to provide a valid
pointer. This shouldn't be a problem however, since access to NULL
triggers an Oops and thus is quickly detected.

Regards, Olaf.

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16  6:21 [PATCH] 2.5.47: strdup() Olaf Dietsche
                   ` (2 preceding siblings ...)
  2002-11-16  6:41 ` William Lee Irwin III
@ 2002-11-17  0:08 ` Chris Wedgwood
  2002-11-17  1:37   ` Olaf Dietsche
  3 siblings, 1 reply; 12+ messages in thread
From: Chris Wedgwood @ 2002-11-17  0:08 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: linux-kernel, trivial

On Sat, Nov 16, 2002 at 07:21:09AM +0100, Olaf Dietsche wrote:

> This *untested* patch adds strdup(). There are about five or six
> different strdup() implementations in various parts of the kernel.

How many users of these functions are there?  I really don't like
certain functions which allocate memory in nebulous ways and almost
would prefer all users of this are fixed to specifically allocate and
str[n]cpy copy themselves making it clear who is allocating memory and
also who should free it.




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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-17  0:08 ` Chris Wedgwood
@ 2002-11-17  1:37   ` Olaf Dietsche
  2002-11-17  4:40     ` Chris Wedgwood
  0 siblings, 1 reply; 12+ messages in thread
From: Olaf Dietsche @ 2002-11-17  1:37 UTC (permalink / raw)
  To: Chris Wedgwood; +Cc: linux-kernel, trivial

Chris Wedgwood <cw@f00f.org> writes:

> On Sat, Nov 16, 2002 at 07:21:09AM +0100, Olaf Dietsche wrote:
>
>> This *untested* patch adds strdup(). There are about five or six
>> different strdup() implementations in various parts of the kernel.
>
> How many users of these functions are there?  I really don't like
> certain functions which allocate memory in nebulous ways and almost
> would prefer all users of this are fixed to specifically allocate and
> str[n]cpy copy themselves making it clear who is allocating memory and
> also who should free it.

So you like duplicate code? Well, to each his own.

Regards, Olaf.

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-17  1:37   ` Olaf Dietsche
@ 2002-11-17  4:40     ` Chris Wedgwood
  0 siblings, 0 replies; 12+ messages in thread
From: Chris Wedgwood @ 2002-11-17  4:40 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: linux-kernel, trivial

On Sun, Nov 17, 2002 at 02:37:27AM +0100, Olaf Dietsche wrote:

> So you like duplicate code? Well, to each his own.

Not at all.

I'm just not sure I like strdup being *easy* to use at it leads to
misuse.  Admittedly this is a very poor argument for not having it.


  --cw

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-16 15:42   ` Olaf Dietsche
@ 2002-11-17 23:32     ` Werner Almesberger
  2002-11-18  2:43       ` Olaf Dietsche
  0 siblings, 1 reply; 12+ messages in thread
From: Werner Almesberger @ 2002-11-17 23:32 UTC (permalink / raw)
  To: Olaf Dietsche; +Cc: Andrew Morton, linux-kernel, trivial

Olaf Dietsche wrote:
> +static inline char *strdup(const char *s) { return kstrdup(s, GFP_KERNEL); }

Why hide what's really going on ? Better change the callers to use
kstrdup.

- Werner

-- 
  _________________________________________________________________________
 / Werner Almesberger, Buenos Aires, Argentina         wa@almesberger.net /
/_http://www.almesberger.net/____________________________________________/

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

* Re: [PATCH] 2.5.47: strdup()
  2002-11-17 23:32     ` Werner Almesberger
@ 2002-11-18  2:43       ` Olaf Dietsche
  0 siblings, 0 replies; 12+ messages in thread
From: Olaf Dietsche @ 2002-11-18  2:43 UTC (permalink / raw)
  To: Werner Almesberger; +Cc: Andrew Morton, linux-kernel

Werner Almesberger <wa@almesberger.net> writes:

> Olaf Dietsche wrote:
>> +static inline char *strdup(const char *s) { return kstrdup(s, GFP_KERNEL); }
>
> Why hide what's really going on ? Better change the callers to use
> kstrdup.

Convenience and laziness. And I haven't seen any other strdup() use,
which uses different allocation flags. But it's there for everybody to
use, if they wish.

Regards, Olaf.

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

end of thread, other threads:[~2002-11-18  2:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-16  6:21 [PATCH] 2.5.47: strdup() Olaf Dietsche
2002-11-16  6:25 ` Jeff Garzik
2002-11-16  6:31 ` Andrew Morton
2002-11-16 15:42   ` Olaf Dietsche
2002-11-17 23:32     ` Werner Almesberger
2002-11-18  2:43       ` Olaf Dietsche
2002-11-16  6:41 ` William Lee Irwin III
2002-11-17  0:08 ` Chris Wedgwood
2002-11-17  1:37   ` Olaf Dietsche
2002-11-17  4:40     ` Chris Wedgwood
  -- strict thread matches above, loose matches on Subject: below --
2002-11-16  8:48 Peter Kjellerstedt
2002-11-16 15:53 ` Olaf Dietsche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox