[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAFo99gZUiSB2cgSAAj7EBjZJTs7QxvCDZr6Wms3ud5qQvBWQMw@mail.gmail.com>
Date: Thu, 18 Sep 2014 00:12:42 +0200
From: Rickard Strandqvist <rickard_strandqvist@...ctrumdigital.se>
To: Dan Carpenter <dan.carpenter@...cle.com>
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Peng Tao <bergwolf@...il.com>, devel@...verdev.osuosl.org,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] staging: lustre: lustre: libcfs: debug.c: Cleaning up
unnecessary use of memset in conjunction with strncpy
2014-09-15 10:23 GMT+02:00 Dan Carpenter <dan.carpenter@...cle.com>:
> On Sun, Sep 14, 2014 at 06:03:16PM +0200, Rickard Strandqvist wrote:
>> Using memset before strncpy just to ensure a trailing null
>> character is an unnecessary double writing of a string
>>
>
> You really should make a function which pads and NUL terminates.
>
> I've said this before, of course, but you haven't even tried.
>
> I can't get excited about these cleanups which open code NUL termination
> every where. They are risky and have introduced bugs before.
>
> regards,
> dan carpenter
Hi Dan
Ok, I have made two suggestions for strncpy function that also
guarantees a terminating null character.
1) retunerar number of characters to be copied, it can be good to
have, but was not really satisfied.
int strncpyz(char *dest, const char *src, size_t count)
{
size_t len=0;
if(0 == count)
return 0;
--count;
while(len < count && src[len])
*dest++ = src[len++];
do {
*dest++ = '\0';
}
while(len < count--);
return len;
}
2) The next version is almost the same code as the regular strncpy,
but with two extra lines.
char *strncpyz(char *dest, const char *src, size_t count)
{
char *tmp = dest;
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
if(tmp != dest)
*--tmp = '\0';
return dest;
}
Since I did not got any better solution to variant 1, I prefer variant 2.
Then the next question is of course what it should be called :-)
Kind regards
Rickard Strandqvist
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists