From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755303AbaIRIve (ORCPT ); Thu, 18 Sep 2014 04:51:34 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:49613 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755112AbaIRIvV (ORCPT ); Thu, 18 Sep 2014 04:51:21 -0400 Date: Thu, 18 Sep 2014 11:51:05 +0300 From: Dan Carpenter To: Rickard Strandqvist Cc: Greg Kroah-Hartman , Peng Tao , devel@driverdev.osuosl.org, "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] staging: lustre: lustre: libcfs: debug.c: Cleaning up unnecessary use of memset in conjunction with strncpy Message-ID: <20140918085105.GB16827@mwanda> References: <1410710597-14520-1-git-send-email-rickard_strandqvist@spectrumdigital.se> <20140915082346.GB17875@mwanda> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Source-IP: acsinet22.oracle.com [141.146.126.238] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 18, 2014 at 12:12:42AM +0200, Rickard Strandqvist wrote: > 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. strlcpy() is more popular the strncpy() in the kernel. No one uses the return value of strncpy() because what is the point? There are around 15-20 places which use the return value of strlcpy(). Some of the place which use the return value assume that the copy fits. I think we should return the number of bytes before the NUL or else count. > > 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. I also prefer variant 2. > > Then the next question is of course what it should be called :-) I think a lot of people call this function strzcpy(). This sort of patch would go through Andrew Morton. regards, dan carpenter