diff options
author | Jeremy Fitzhardinge <jeremy@xensource.com> | 2007-07-17 21:37:02 -0400 |
---|---|---|
committer | Jeremy Fitzhardinge <jeremy@goop.org> | 2007-07-18 11:47:39 -0400 |
commit | 1e66df3ee301209f4a38df097d7cc5cb9b367a3f (patch) | |
tree | 55beb2a342dbe08c0404f749e02808e3f09023ac | |
parent | 8b4a40809e5330c9da5d20107d693d92d73b31dc (diff) |
add kstrndup
Add a kstrndup function, modelled on strndup. Like strndup this
returns a string copied into its own allocated memory, but it copies
no more than the specified number of bytes from the source.
Remove private strndup() from irda code.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Randy Dunlap <randy.dunlap@oracle.com>
Cc: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
Cc: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@mandriva.com>
Cc: Al Viro <viro@ftp.linux.org.uk>
Cc: Panagiotis Issaris <takis@issaris.org>
Cc: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
-rw-r--r-- | include/linux/string.h | 1 | ||||
-rw-r--r-- | mm/util.c | 26 | ||||
-rw-r--r-- | net/irda/irias_object.c | 43 |
3 files changed, 30 insertions, 40 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index 7f2eb6a477f9..ee5e9ccc4aae 100644 --- a/include/linux/string.h +++ b/include/linux/string.h | |||
@@ -105,6 +105,7 @@ extern void * memchr(const void *,int,__kernel_size_t); | |||
105 | #endif | 105 | #endif |
106 | 106 | ||
107 | extern char *kstrdup(const char *s, gfp_t gfp); | 107 | extern char *kstrdup(const char *s, gfp_t gfp); |
108 | extern char *kstrndup(const char *s, size_t len, gfp_t gfp); | ||
108 | extern void *kmemdup(const void *src, size_t len, gfp_t gfp); | 109 | extern void *kmemdup(const void *src, size_t len, gfp_t gfp); |
109 | 110 | ||
110 | #ifdef __cplusplus | 111 | #ifdef __cplusplus |
@@ -6,7 +6,6 @@ | |||
6 | 6 | ||
7 | /** | 7 | /** |
8 | * kstrdup - allocate space for and copy an existing string | 8 | * kstrdup - allocate space for and copy an existing string |
9 | * | ||
10 | * @s: the string to duplicate | 9 | * @s: the string to duplicate |
11 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory | 10 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
12 | */ | 11 | */ |
@@ -27,6 +26,30 @@ char *kstrdup(const char *s, gfp_t gfp) | |||
27 | EXPORT_SYMBOL(kstrdup); | 26 | EXPORT_SYMBOL(kstrdup); |
28 | 27 | ||
29 | /** | 28 | /** |
29 | * kstrndup - allocate space for and copy an existing string | ||
30 | * @s: the string to duplicate | ||
31 | * @max: read at most @max chars from @s | ||
32 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory | ||
33 | */ | ||
34 | char *kstrndup(const char *s, size_t max, gfp_t gfp) | ||
35 | { | ||
36 | size_t len; | ||
37 | char *buf; | ||
38 | |||
39 | if (!s) | ||
40 | return NULL; | ||
41 | |||
42 | len = strnlen(s, max); | ||
43 | buf = kmalloc_track_caller(len+1, gfp); | ||
44 | if (buf) { | ||
45 | memcpy(buf, s, len); | ||
46 | buf[len] = '\0'; | ||
47 | } | ||
48 | return buf; | ||
49 | } | ||
50 | EXPORT_SYMBOL(kstrndup); | ||
51 | |||
52 | /** | ||
30 | * kmemdup - duplicate region of memory | 53 | * kmemdup - duplicate region of memory |
31 | * | 54 | * |
32 | * @src: memory region to duplicate | 55 | * @src: memory region to duplicate |
@@ -80,7 +103,6 @@ EXPORT_SYMBOL(krealloc); | |||
80 | 103 | ||
81 | /* | 104 | /* |
82 | * strndup_user - duplicate an existing string from user space | 105 | * strndup_user - duplicate an existing string from user space |
83 | * | ||
84 | * @s: The string to duplicate | 106 | * @s: The string to duplicate |
85 | * @n: Maximum number of bytes to copy, including the trailing NUL. | 107 | * @n: Maximum number of bytes to copy, including the trailing NUL. |
86 | */ | 108 | */ |
diff --git a/net/irda/irias_object.c b/net/irda/irias_object.c index 4adaae242b9e..cf302457097b 100644 --- a/net/irda/irias_object.c +++ b/net/irda/irias_object.c | |||
@@ -36,39 +36,6 @@ hashbin_t *irias_objects; | |||
36 | */ | 36 | */ |
37 | struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}}; | 37 | struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}}; |
38 | 38 | ||
39 | /* | ||
40 | * Function strndup (str, max) | ||
41 | * | ||
42 | * My own kernel version of strndup! | ||
43 | * | ||
44 | * Faster, check boundary... Jean II | ||
45 | */ | ||
46 | static char *strndup(char *str, size_t max) | ||
47 | { | ||
48 | char *new_str; | ||
49 | int len; | ||
50 | |||
51 | /* Check string */ | ||
52 | if (str == NULL) | ||
53 | return NULL; | ||
54 | /* Check length, truncate */ | ||
55 | len = strlen(str); | ||
56 | if(len > max) | ||
57 | len = max; | ||
58 | |||
59 | /* Allocate new string */ | ||
60 | new_str = kmalloc(len + 1, GFP_ATOMIC); | ||
61 | if (new_str == NULL) { | ||
62 | IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__); | ||
63 | return NULL; | ||
64 | } | ||
65 | |||
66 | /* Copy and truncate */ | ||
67 | memcpy(new_str, str, len); | ||
68 | new_str[len] = '\0'; | ||
69 | |||
70 | return new_str; | ||
71 | } | ||
72 | 39 | ||
73 | /* | 40 | /* |
74 | * Function ias_new_object (name, id) | 41 | * Function ias_new_object (name, id) |
@@ -90,7 +57,7 @@ struct ias_object *irias_new_object( char *name, int id) | |||
90 | } | 57 | } |
91 | 58 | ||
92 | obj->magic = IAS_OBJECT_MAGIC; | 59 | obj->magic = IAS_OBJECT_MAGIC; |
93 | obj->name = strndup(name, IAS_MAX_CLASSNAME); | 60 | obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC); |
94 | if (!obj->name) { | 61 | if (!obj->name) { |
95 | IRDA_WARNING("%s(), Unable to allocate name!\n", | 62 | IRDA_WARNING("%s(), Unable to allocate name!\n", |
96 | __FUNCTION__); | 63 | __FUNCTION__); |
@@ -360,7 +327,7 @@ void irias_add_integer_attrib(struct ias_object *obj, char *name, int value, | |||
360 | } | 327 | } |
361 | 328 | ||
362 | attrib->magic = IAS_ATTRIB_MAGIC; | 329 | attrib->magic = IAS_ATTRIB_MAGIC; |
363 | attrib->name = strndup(name, IAS_MAX_ATTRIBNAME); | 330 | attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC); |
364 | 331 | ||
365 | /* Insert value */ | 332 | /* Insert value */ |
366 | attrib->value = irias_new_integer_value(value); | 333 | attrib->value = irias_new_integer_value(value); |
@@ -404,7 +371,7 @@ void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets, | |||
404 | } | 371 | } |
405 | 372 | ||
406 | attrib->magic = IAS_ATTRIB_MAGIC; | 373 | attrib->magic = IAS_ATTRIB_MAGIC; |
407 | attrib->name = strndup(name, IAS_MAX_ATTRIBNAME); | 374 | attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC); |
408 | 375 | ||
409 | attrib->value = irias_new_octseq_value( octets, len); | 376 | attrib->value = irias_new_octseq_value( octets, len); |
410 | if (!attrib->name || !attrib->value) { | 377 | if (!attrib->name || !attrib->value) { |
@@ -446,7 +413,7 @@ void irias_add_string_attrib(struct ias_object *obj, char *name, char *value, | |||
446 | } | 413 | } |
447 | 414 | ||
448 | attrib->magic = IAS_ATTRIB_MAGIC; | 415 | attrib->magic = IAS_ATTRIB_MAGIC; |
449 | attrib->name = strndup(name, IAS_MAX_ATTRIBNAME); | 416 | attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC); |
450 | 417 | ||
451 | attrib->value = irias_new_string_value(value); | 418 | attrib->value = irias_new_string_value(value); |
452 | if (!attrib->name || !attrib->value) { | 419 | if (!attrib->name || !attrib->value) { |
@@ -506,7 +473,7 @@ struct ias_value *irias_new_string_value(char *string) | |||
506 | 473 | ||
507 | value->type = IAS_STRING; | 474 | value->type = IAS_STRING; |
508 | value->charset = CS_ASCII; | 475 | value->charset = CS_ASCII; |
509 | value->t.string = strndup(string, IAS_MAX_STRING); | 476 | value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC); |
510 | if (!value->t.string) { | 477 | if (!value->t.string) { |
511 | IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__); | 478 | IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__); |
512 | kfree(value); | 479 | kfree(value); |