linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: Resend [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE
@ 2003-05-25  9:21 René Scharfe
  2003-05-25 12:05 ` Christoph Hellwig
  2003-05-25 17:24 ` Edgar Toernig
  0 siblings, 2 replies; 27+ messages in thread
From: René Scharfe @ 2003-05-25  9:21 UTC (permalink / raw)
  To: Ben Collins, Linus Torvalds; +Cc: linux-kernel

> How about just adding a sane
> 
> 	int copy_string(char *dest, const char *src, int len)
> 	{
> 		int size;
> 
> 		if (!len)
> 			return 0;
> 		size = strlen(src);
> 		if (size >= len)
> 			size = len-1;
> 		memcpy(dest, src, size);
> 		dest[size] = '\0';
> 		return size;
> 	}
> 
> which is what pretty much everybody really _wants_ to have anyway? We 
> should deprecate "strncpy()" within the kernel entirely.

This looks suspiciously like strlcpy() from the *BSDs. Why not name it
so?

The following patch is based on Samba's implementation (found in
source/lib/replace.c). I just reformatted it somewhat, there are no
functional changes. What do you think?

René



diff -ur linux-a/kernel/ksyms.c linux-b/kernel/ksyms.c
--- linux-a/kernel/ksyms.c	2003-05-05 01:52:49.000000000 +0200
+++ linux-b/kernel/ksyms.c	2003-05-25 11:02:22.000000000 +0200
@@ -588,6 +588,7 @@
 EXPORT_SYMBOL(strnicmp);
 EXPORT_SYMBOL(strspn);
 EXPORT_SYMBOL(strsep);
+EXPORT_SYMBOL(strlcpy);
 
 /* software interrupts */
 EXPORT_SYMBOL(tasklet_init);
diff -ur linux-a/lib/string.c linux-b/lib/string.c
--- linux-a/lib/string.c	2003-05-05 01:53:40.000000000 +0200
+++ linux-b/lib/string.c	2003-05-25 11:12:58.000000000 +0200
@@ -527,3 +527,28 @@
 }
 
 #endif
+
+#ifndef __HAVE_ARCH_STRLCPY
+/**
+ * strlcpy - Copy a length-limited, %NUL-terminated string
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @bufsize: Size of the destination buffer
+ *
+ * Returns the length of @src, or 0 if @bufsize is 0. Unlike strncpy(),
+ * strlcpy() always NUL-terminates @dest and does no padding.
+ */
+size_t strlcpy(char *dest, const char *src, size_t bufsize)
+{
+	size_t len = strlen(src);
+	size_t ret = len;
+
+	if (bufsize == 0)
+		return 0;
+	if (len >= bufsize)
+		len = bufsize-1;
+	memcpy(dest, src, len);
+	dest[len] = '\0';   
+	return ret;
+}
+#endif

^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE
@ 2003-05-13  7:14 Ben Collins
  2003-05-13 15:08 ` Patrick Mochel
  0 siblings, 1 reply; 27+ messages in thread
From: Ben Collins @ 2003-05-13  7:14 UTC (permalink / raw)
  To: Christoph Hellwig, Linus Torvalds, linux-kernel

On Tue, May 13, 2003 at 08:10:32AM +0100, Christoph Hellwig wrote:
> On Tue, May 13, 2003 at 02:26:40AM -0400, Ben Collins wrote:
> > This was causing me all sorts of problems with linux1394's 16-18 byte
> > long bus_id lengths. The sysfs names were all broken.
> > 
> > This not only makes KOBJ_NAME_LEN match BUS_ID_SIZE, but fixes the
> > strncpy's in drivers/base/ so that it can't happen again (atleast the
> > strings will be null terminated).
> 
> What about defining BUS_ID_SIZE in terms of KOBJ_NAME_LEN?

Ok, then add this in addition to the previous patch.


Index: include/linux/device.h
===================================================================
RCS file: /home/scm/linux-2.5/include/linux/device.h,v
retrieving revision 1.48
diff -u -u -r1.48 device.h
--- include/linux/device.h	29 Apr 2003 17:30:20 -0000	1.48
+++ include/linux/device.h	13 May 2003 07:47:39 -0000
@@ -35,7 +35,7 @@
 #define DEVICE_NAME_SIZE	50
 #define DEVICE_NAME_HALF	__stringify(20)	/* Less than half to accommodate slop */
 #define DEVICE_ID_SIZE		32
-#define BUS_ID_SIZE		20
+#define BUS_ID_SIZE		KOBJ_NAME_LEN
 
 
 enum {

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

end of thread, other threads:[~2003-07-11  9:35 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-25  9:21 Resend [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE René Scharfe
2003-05-25 12:05 ` Christoph Hellwig
2003-05-25 17:24 ` Edgar Toernig
2003-05-25 19:05   ` René Scharfe
2003-05-25 18:16     ` Ben Collins
2003-05-25 20:11       ` René Scharfe
2003-05-25 19:01     ` Valdis.Kletnieks
2003-05-25 19:31       ` René Scharfe
2003-05-26  1:13     ` Linus Torvalds
2003-05-26 13:29       ` [RFC] [2.5 patch] Change strlcpy and strlcat Adrian Bunk
2003-05-26 14:10         ` Ben Collins
2003-05-26 17:11         ` Linus Torvalds
  -- strict thread matches above, loose matches on Subject: below --
2003-05-13  7:14 [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE Ben Collins
2003-05-13 15:08 ` Patrick Mochel
2003-05-16  0:20   ` Resend " Ben Collins
2003-05-16 18:43     ` Felipe Alfaro Solana
2003-05-25  0:07     ` Ben Collins
2003-05-25  3:52       ` Linus Torvalds
2003-05-25  3:10         ` Ben Collins
2003-05-25 12:03         ` Adam Sampson
2003-05-25 17:10           ` Linus Torvalds
2003-05-25 16:40             ` Ben Collins
2003-05-25 15:51         ` Matt Mackall
2003-05-25 17:25           ` Riley Williams
2003-05-25 18:13           ` Valdis.Kletnieks
2003-05-25 23:42             ` Matt Mackall
2003-05-25 16:41         ` Ben Collins
2003-07-11  9:50         ` Rogier Wolff
2003-05-25  8:02       ` Russell King

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).