LWN.net Logo

Re: Resend [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE

From:  Linus Torvalds <torvalds@transmeta.com>
To:  Ben Collins <bcollins@debian.org>
Subject:  Re: Resend [PATCH] Make KOBJ_NAME_LEN match BUS_ID_SIZE
Date:  Sat, 24 May 2003 20:52:53 -0700 (PDT)
Cc:  Patrick Mochel <mochel@osdl.org>, <linux-kernel@vger.kernel.org>


On Sat, 24 May 2003, Ben Collins wrote:
>
> Given that the problem with KOBJ_NAME_LEN == 20 affecting one snd driver
> has so far only been explained as a compiler bug, can I suggest this
> patch be applied? Even aside from the KOBJ_NAME_LEN == 20, the snprintf
> changes will keep things from breaking in other ways that are current
> now.

I hate using snprintf() for this kind of mindless string copy opertation.

Yeah, "strncpy()" is a frigging disaster when it comes to '\0', in many
ways. We should probably disallow using strncpy(), and aim for a _sane_
implementation that does what we actually want (none of that zero-padding
crap, and _always_ put a NUL at the end). I bet that is what most current
strncpy() users actually would want.

But switching it over to "snprintf()" is overkill. 

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.

		Linus

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


(Log in to post comments)

Copyright © 2003, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds