Received: by 10.223.185.116 with SMTP id b49csp8772318wrg; Fri, 2 Mar 2018 07:42:54 -0800 (PST) X-Google-Smtp-Source: AG47ELuucGGbCFuKd3feb5l7984jUujqzqYrNWGOK16892FEfhDG3iMVXIXSZCW4nOAnohxXIYG6 X-Received: by 10.98.80.15 with SMTP id e15mr5970997pfb.90.1520005373961; Fri, 02 Mar 2018 07:42:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520005373; cv=none; d=google.com; s=arc-20160816; b=ocoWIofqlfq8AwBcxkltkcSbR4s57fKeNzTiiYa6Wp6pq9/pqc1QDdn6JelVNrJM6M 0pf7wzr13eslfWnpfmnNrlicHfu51GuHWl/s/OsyyY2PySDgchdPogBDP/Snl63z99X5 Ec27BqqS6gXvFHPWnIILEJoVc7x22rVhDFYZSiQgkSRHUqwS9TTV2qJ3U6jzwFV3v7cg PhKKuitkKwgyR9clq0yAZeZvDFMhu23utHuj0rW0r8E8PUdIMHGPVr9iVf09VtNY+EUE 7wOk1lFI3rgHIr+WSRIBUgGGfksZppiEVhL/TknDKgyhCjcbmUlrLIYSykubQfAA/blS iHag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=xhTWXCHfLejjI4WR6lqMgTVYygdC8LDjNBQBeci5xlM=; b=SzMgE495uSK0jvI9ESqOPQL2GZ643kdgzKoheY5kReAtkeBzfIiZxpZ1cl95vk+bJJ FES/kDupI45//jv6Ta8CRABLK0cmL7JJqi7F0WfysY+VmpWMMTQGwOT5XRkR8FyVV/Hf 2VDiG53ou03WvNmeMjfRn6PNmCvF6ELPHO3Wto6RZtawaksGm61wuO17RKmqhoV6GOr+ wYx+JKQT2lyG4tlCrYXFt2GQE72ubkA8OffpxDDQytyc3g8TwkMwRFZMK3K4YAPVuv7s ZN/ct2DOyHNPT9mZqZN25xhc216HUqjaUgFvIPtyHSp+Qp7g5ruK3BRP40bvKFMWPerm tTuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Q4HgCOUS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11-v6si418839plj.748.2018.03.02.07.42.39; Fri, 02 Mar 2018 07:42:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Q4HgCOUS; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1428282AbeCBNpJ (ORCPT + 99 others); Fri, 2 Mar 2018 08:45:09 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:35569 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1424655AbeCBNpH (ORCPT ); Fri, 2 Mar 2018 08:45:07 -0500 Received: by mail-lf0-f66.google.com with SMTP id 70so13415519lfw.2; Fri, 02 Mar 2018 05:45:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=xhTWXCHfLejjI4WR6lqMgTVYygdC8LDjNBQBeci5xlM=; b=Q4HgCOUSvPrh9C2t/it7Y0y21qx57G7b8CHQZ2pVp0bYKyIFqezndeg10BVHhTg1qt T6JlHNzqjHSmDSGoplG0q0UvoLTBRRJvYI7CTITbIalp7bwIgs/f+6oC+nbhUCk4yevX ezs6Q7dpfj8xo+Jw1POQdEkx3NyoQBRxEx27kgyLbCDe3iOq64XzIpFciEiDTpYLcGjV 8narEVMLFSEhtTQlns5hJxi+fRmhFE803XGOR5la5tAo/JNXOaEz+MidN2J654aJDru/ hvsZhAX5F5yF7wfIAhHsy0xebJsJaxB3baHVVFnx6c/dCIixSZu4gn7f9T04283O0ZrJ /YEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=xhTWXCHfLejjI4WR6lqMgTVYygdC8LDjNBQBeci5xlM=; b=JMooJZ4i7NwWiUePQFu9SP6dPtoDpng1zG4QnXRBgxaSQex264ngG0neVpJ+25p2Nf pfwlb4LzFpQfhZvJJ7a8qzJJiZwLVIikYmzbeRVJhr3qAEXKik44deI1rlaVzb8wvsh3 UGsz1x1EhBi3xXoc+o5f1lpITOx7bRLFh6dQCk/foOhiZtUEAvS0HmUdzdmt9lLZ6F7B 3j9cGP+2DCL2kxBqjl5eC02MCbwKHEf/phTZcLnhBY1gIRfhxYalE4AWFMoApWMjRIV0 C/cHQPNjNTlqwe26Og0Cz4ppgmE3i/BBbfcKKk9B3CFp4HJbx/8oZlNwyZpDkGdVO04R MzXg== X-Gm-Message-State: AElRT7FIbJd7jpH8kjzUC3WSU07oUDg4L5IDa45sOCCvo8qVAAURK+bi avxUSoaURhzlY5mLtooaM6M= X-Received: by 10.25.222.207 with SMTP id i76mr3592273lfl.133.1519998305465; Fri, 02 Mar 2018 05:45:05 -0800 (PST) Received: from huvuddator (ua-213-113-106-221.cust.bredbandsbolaget.se. [213.113.106.221]) by smtp.gmail.com with ESMTPSA id i13sm1353461lfa.97.2018.03.02.05.45.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 02 Mar 2018 05:45:04 -0800 (PST) Date: Fri, 2 Mar 2018 14:44:53 +0100 From: Ulf Magnusson To: Joey Pabalinas Cc: linux-kbuild@vger.kernel.org, Arnaud Lacombe , Masahiro Yamada , Linux Kernel Mailing List Subject: Re: [PATCH] scripts/kconfig: replace single character strcat() appends Message-ID: <20180302134453.mtd4uepeiviicyef@huvuddator> References: <20180302074424.2myh3zhxnbpaohjq@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20180302074424.2myh3zhxnbpaohjq@gmail.com> User-Agent: NeoMutt/20170609 (1.8.3) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 01, 2018 at 09:44:24PM -1000, Joey Pabalinas wrote: > Convert strcat() calls which only append single characters > to the end of res into simpler (and most likely cheaper) > single assignment statements. > > Signed-off-by: Joey Pabalinas > > 1 file changed, 9 insertions(+), 10 deletions(-) > > diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c > index cca9663be5ddd91870..67600f48660f2ac142 100644 > --- a/scripts/kconfig/symbol.c > +++ b/scripts/kconfig/symbol.c > @@ -910,8 +910,7 @@ char *sym_expand_string_value(const char *in) > * freed, so make sure to always allocate a new string > */ > reslen = strlen(in) + 1; > - res = xmalloc(reslen); > - res[0] = '\0'; > + res = xcalloc(1, reslen); Not sure this is an improvement. Zeroing the bytes after the initial null terminator is redundant, and the explicit '\0' makes it clearer to me what's going on. > > while ((src = strchr(in, '$'))) { > char *p, name[SYMBOL_MAXLENGTH]; > @@ -951,7 +950,7 @@ const char *sym_escape_string_value(const char *in) > { > const char *p; > size_t reslen; > - char *res; > + char *res, *end; > size_t l; > > reslen = strlen(in) + strlen("\"\"") + 1; > @@ -968,25 +967,25 @@ const char *sym_escape_string_value(const char *in) > p++; > } > > - res = xmalloc(reslen); > - res[0] = '\0'; > - > - strcat(res, "\""); > + res = xcalloc(1, reslen); > + end = res; > + *end++ = '\"'; Could make this xmalloc() instead and write the null terminator via 'end' -- see below. > > p = in; > for (;;) { > l = strcspn(p, "\"\\"); > strncat(res, p, l); Could replace this with memcpy(end, p, l). At that point, all the writing would be done via 'end', and 'res' would just be a way to remember the starting address of the result. > p += l; > + end += l; > > if (p[0] == '\0') Unrelated, but *p is clearer than p[0] to me when doing pointers rather than indices. > break; > > - strcat(res, "\\"); > - strncat(res, p++, 1); > + *end++ = '\\'; > + *end++ = *p++; > } > + *end = '\"'; With all the writing done via 'end', the null terminator could be written here. > > - strcat(res, "\""); > return res; > } > > -- > 2.16.2 > > Sorry about the double send, clipboards are very fickle beasts :( I like the approach, but I wonder if we can take it a bit further. Here's what I'd do: 1. Rename the 'in' parameter to 's'. 2. Rename 'p' to 'in'. 3. Rename 'end' to 'out' At that point, you're reading from 'in' and writing to 'out', which seems pretty nice and readable. This code is pretty cold by the way, so it wouldn't matter for performance. GCC knows how functions like strcat() work too, and uses that to optimize (see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html). I'm all for trying to make Kconfig's code neater though. Tip: If you want to run Valgrind, you can do it with a command like $ ARCH=x86 SRCARCH=x86 KERNELVERSION=`make kernelversion` valgrind scripts/kconfig/mconf Kconfig That works the same as $ make menuconfig Cheers, Ulf