From owner-svn-src-head@FreeBSD.ORG Fri Jul 15 20:34:36 2011 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 604CF106564A; Fri, 15 Jul 2011 20:34:36 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) by mx1.freebsd.org (Postfix) with ESMTP id F1DA28FC08; Fri, 15 Jul 2011 20:34:35 +0000 (UTC) Received: from turtle.stack.nl (turtle.stack.nl [IPv6:2001:610:1108:5010::132]) by mx1.stack.nl (Postfix) with ESMTP id 47BCD1DD659; Fri, 15 Jul 2011 22:34:35 +0200 (CEST) Received: by turtle.stack.nl (Postfix, from userid 1677) id 2DF39173CB; Fri, 15 Jul 2011 22:34:35 +0200 (CEST) Date: Fri, 15 Jul 2011 22:34:35 +0200 From: Jilles Tjoelker To: Xin LI Message-ID: <20110715203435.GA83996@stack.nl> References: <201107110531.p6B5Vrfn077306@svn.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201107110531.p6B5Vrfn077306@svn.freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org Subject: Re: svn commit: r223922 - head/usr.bin/rpcgen X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jul 2011 20:34:36 -0000 On Mon, Jul 11, 2011 at 05:31:53AM +0000, Xin LI wrote: > Author: delphij > Date: Mon Jul 11 05:31:52 2011 > New Revision: 223922 > URL: http://svn.freebsd.org/changeset/base/223922 > Log: > Use strlcpy(). > MFC after: 1 month It looks like the length is known in all four cases, so that memcpy() can be used instead of strncpy() or strlcpy(), with explicitly assigning a '\0' (like with the former strncpy()). In particular, strlcpy()'s implicit strlen of the source operand is wasteful here, particularly for input files with long lines. It also adds a hidden dependency on *str being null-terminated, which is a bit ugly even though it is satisfied and used elsewhere in the code. > Modified: > head/usr.bin/rpcgen/rpc_scan.c > Modified: head/usr.bin/rpcgen/rpc_scan.c > ============================================================================== > --- head/usr.bin/rpcgen/rpc_scan.c Mon Jul 11 05:22:31 2011 (r223921) > +++ head/usr.bin/rpcgen/rpc_scan.c Mon Jul 11 05:31:52 2011 (r223922) > @@ -329,10 +329,9 @@ findstrconst(char **str, const char **va > error("unterminated string constant"); > } > p++; > - size = p - *str; > - tmp = xmalloc(size + 1); > - (void) strncpy(tmp, *str, size); > - tmp[size] = 0; > + size = p - *str + 1; > + tmp = xmalloc(size); > + (void) strlcpy(tmp, *str, size); > *val = tmp; > *str = p; > } > @@ -352,13 +351,12 @@ findchrconst(char **str, const char **va > error("unterminated string constant"); > } > p++; > - size = p - *str; > - if (size != 3) { > + size = p - *str + 1; > + if (size != 4) { > error("empty char string"); > } > - tmp = xmalloc(size + 1); > - (void) strncpy(tmp, *str, size); > - tmp[size] = 0; > + tmp = xmalloc(size); > + (void) strlcpy(tmp, *str, size); > *val = tmp; > *str = p; > } > @@ -381,10 +379,9 @@ findconst(char **str, const char **val) > p++; > } while (isdigit(*p)); > } > - size = p - *str; > - tmp = xmalloc(size + 1); > - (void) strncpy(tmp, *str, size); > - tmp[size] = 0; > + size = p - *str + 1; > + tmp = xmalloc(size); > + (void) strlcpy(tmp, *str, size); > *val = tmp; > *str = p; > } > @@ -438,8 +435,7 @@ findkind(char **mark, token *tokp) > tokp->kind = TOK_IDENT; > for (len = 0; isalnum(str[len]) || str[len] == '_'; len++); > tmp = xmalloc(len + 1); > - (void) strncpy(tmp, str, len); > - tmp[len] = 0; > + (void) strlcpy(tmp, str, len + 1); > tokp->str = tmp; > *mark = str + len; > } -- Jilles Tjoelker