Received: by 10.223.185.111 with SMTP id b44csp1303250wrg; Sat, 10 Mar 2018 02:58:36 -0800 (PST) X-Google-Smtp-Source: AG47ELsh056bqD/9sSQQqI7qh1kZAs5oPpcrgmo+Nt3HJjR5KuQsvm8jqTujeBGEV50NfQDneYTJ X-Received: by 10.98.201.88 with SMTP id k85mr1683048pfg.9.1520679516149; Sat, 10 Mar 2018 02:58:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520679516; cv=none; d=google.com; s=arc-20160816; b=RIjirtcPJAOCnNvvoGFQvSSCX4DCo51MQ+C1jP6AtVeEaiWzBhipFa8UEkTrdfNIhv tN5aoB6q3aaOU+YFkKPlwkQtQUsD8bRHSu61XNd058uQKv3pBcIoqj2s5XMvUKoKURJN 48zxBc7JXyOVKResK4CuR7Ff3R7er6obfCEIjFobtuHIDxWbg5+Y/njnitOO70TVawZL tKlqhsbuOfTMY34RqvtvTQKNh0HzGjM1cqTbhtqUJzRsllJ0GERx1DIqQFjhhMq2L1d/ e5EBzvkirHobOyvLT+ExrhuglnElxcJgh2F5xL+ekiXr0pPQizl5yRzQ/qbgpFtqiOCb DtNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=ZPTLo8vp+Vf4WSuSfdHGbhqq/+fr3E9AiV43dWgGOZ4=; b=K0ubTLqmC8VRp40PsrZdfcsntTaIrPuj/4r5Fa2XV8Icjt1EolUP/ofI+nVxKItqM7 5QdGASSmM4EG9Wbvgfd+kHV9A42t/3VrcnBmURwf5g3ss1d2mZaVLGbSP1sB8g8FWTh7 9T5cW5ww4cdpiksCzigSk0g87znosXwvtzYNZMvEpGh9o73emiygEDsYiPxbJTSzFdBX Q1GQEdddnouCsklMF3o23CQl86zy6OpOfpBqALO7zGqiJAJQALHzFMWAz4IfpYjSqRiI LSZwIsMcxDEQWnZCKe02BVUnEiP1YVsoqRzDtiHHXXr3f+LLqAofvN3husEtuvZRLzlc eIsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=c66ieTAS; 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 i11si2161918pgq.332.2018.03.10.02.58.21; Sat, 10 Mar 2018 02:58:36 -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=c66ieTAS; 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 S1752108AbeCJK5H (ORCPT + 99 others); Sat, 10 Mar 2018 05:57:07 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:41648 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751974AbeCJK5E (ORCPT ); Sat, 10 Mar 2018 05:57:04 -0500 Received: by mail-pl0-f67.google.com with SMTP id d9-v6so6650699plo.8; Sat, 10 Mar 2018 02:57:04 -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:mime-version:content-disposition :user-agent; bh=ZPTLo8vp+Vf4WSuSfdHGbhqq/+fr3E9AiV43dWgGOZ4=; b=c66ieTASV19vXWuaB1wT79VoIQY2IKYjoVcE08kFJ6p4uYoWGumpYgUw3bCBn2NB7H kLOOCvQ09rFr+Q8QvZhdxWmWSuC2fklmZQW+lW3lGVqAwsVNizzcKgAQeFSug8Uy5fHA Uo4H1cIhodbb5D5M+ZLl1pjvivL5xafgjjyhmmjMZ1sLSSkVZ0DQpUtWax42z3g9C4fo bzocOgdiq2Oty6LF21dJ/WZXKzN7U8loZTPCfPG85ny+qGrPZRhlUguJQdoqvJ5hAzLF zX89jT1RUaMcXnMQR/pl5zIYQGJ8niF0mlawmIBs3H0My/0LzWsh8uABYW71IyrlOkKL HJkQ== 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:mime-version :content-disposition:user-agent; bh=ZPTLo8vp+Vf4WSuSfdHGbhqq/+fr3E9AiV43dWgGOZ4=; b=q9HyH1zHeZ7s+pW861zgZAQYmEPf4IMLnj7Jbdcie5c+xb+vXKRfdISCmkw6gBolcn EbeXljmcahKn7XIwKaQtroX3aNFI/fBprvj0VNY+LEd+azxatfP1EZlx2wYC3a1anyLe l1YZPqmVg+hq++/kqlZukpyFGVgQH1h+zXILDgWWR47o4JG4xWmfT2qzwgAVxGnsZ0c8 WoDUfYVArwZuzPXpsNo++CIhcLqEfQ9O9Zt94M8Yah6OoJaAoB2ZoMFewPJPlIbrU0pK 2SDtvQN61qrFbm24KneTgxFQI8z7JNcQrSZd9lDjPPU2PG/v8NRrOtC+nICR0vK8JOq/ GDrQ== X-Gm-Message-State: AElRT7Go8efRpU4cgv6RvYdiQ/NnLjWtXmCydQWynwGIYaGif/4zZYSo Q3uIHEYX4k2EDTfPmim3iy71Dg== X-Received: by 2002:a17:902:8f8f:: with SMTP id z15-v6mr1743103plo.315.1520679423808; Sat, 10 Mar 2018 02:57:03 -0800 (PST) Received: from gmail.com ([2605:e000:1117:c19a:2e0:1bff:fe70:ea]) by smtp.gmail.com with ESMTPSA id e22sm6530764pgn.50.2018.03.10.02.57.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 10 Mar 2018 02:57:01 -0800 (PST) Date: Sat, 10 Mar 2018 00:56:59 -1000 From: Joey Pabalinas To: linux-kbuild@vger.kernel.org Cc: Ulf Magnusson , Masahiro Yamada , linux-kernel@vger.kernel.org, Joey Pabalinas Subject: [PATCH v2] scripts/kconfig: cleanup symbol handling code Message-ID: <20180310105659.7e34lk4kvcbl64lg@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="5oo6xr74tsg54goh" Content-Disposition: inline User-Agent: NeoMutt/20180223 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --5oo6xr74tsg54goh Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Many of the variable names in scripts/kconfig/symbol.c are far too terse to the point of not at all identifying _what_ they are actually used for (`p` and `l` as a couple examples), and overall there is a large amount of code that could use some cleaning up. Give more explicit names to these variables, fix a couple cases where different variables were sharing the same name and shadowing each other, and overall cleanup a bit of the messiness in sym_expand_string_value() and sym_escape_string_value() while maintaining equivalent program behavior. Suggested-by: Ulf Magnusson Signed-off-by: Joey Pabalinas 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c index 2220bc4b051bd914e3..9ee32ddb44e193719c 100644 --- a/scripts/kconfig/symbol.c +++ b/scripts/kconfig/symbol.c @@ -5,8 +5,8 @@ =20 #include #include -#include #include +#include #include =20 #include "lkc.h" @@ -337,7 +337,7 @@ void sym_calc_value(struct symbol *sym) { struct symbol_value newval, oldval; struct property *prop; - struct expr *e; + struct expr *expr; =20 if (!sym) return; @@ -469,7 +469,7 @@ void sym_calc_value(struct symbol *sym) struct symbol *choice_sym; =20 prop =3D sym_get_choice_prop(sym); - expr_list_for_each_sym(prop->expr, e, choice_sym) { + expr_list_for_each_sym(prop->expr, expr, choice_sym) { if ((sym->flags & SYMBOL_WRITE) && choice_sym->visible !=3D no) choice_sym->flags |=3D SYMBOL_WRITE; @@ -899,94 +899,100 @@ struct symbol *sym_find(const char *name) * name to be expanded shall be prefixed by a '$'. Unknown symbol expands = to * the empty string. */ -char *sym_expand_string_value(const char *in) +char *sym_expand_string_value(const char *src) { - const char *src; - char *res; - size_t reslen; + const char *in; + char *res, *out; + size_t res_len, src_len; =20 /* - * Note: 'in' might come from a token that's about to be + * Note: 'src' might come from a token that'src about to be * freed, so make sure to always allocate a new string */ - reslen =3D strlen(in) + 1; - res =3D xmalloc(reslen); - res[0] =3D '\0'; + res_len =3D strlen(src) + 1; + res =3D xmalloc(res_len); + out =3D res; =20 - while ((src =3D strchr(in, '$'))) { + while ((in =3D strchr(src, '$'))) { char *p, name[SYMBOL_MAXLENGTH]; - const char *symval =3D ""; + const char *sym_val =3D ""; struct symbol *sym; - size_t newlen; + size_t new_len, sym_len; =20 - strncat(res, in, src - in); - src++; + strscpy(out, src, in - src); + out +=3D in - src; + in++; =20 p =3D name; - while (isalnum(*src) || *src =3D=3D '_') - *p++ =3D *src++; + while (isalnum(*in) || *in =3D=3D '_') + *p++ =3D *in++; *p =3D '\0'; =20 sym =3D sym_find(name); if (sym !=3D NULL) { sym_calc_value(sym); - symval =3D sym_get_string_value(sym); + sym_val =3D sym_get_string_value(sym); } =20 - newlen =3D strlen(res) + strlen(symval) + strlen(src) + 1; - if (newlen > reslen) { - reslen =3D newlen; - res =3D xrealloc(res, reslen); + sym_len =3D strlen(sym_val); + new_len =3D sym_len + strlen(res) + strlen(in) + 1; + if (new_len > res_len) { + res_len =3D new_len; + res =3D xrealloc(res, res_len); } =20 - strcat(res, symval); - in =3D src; + strscpy(out, sym_val, sym_len); + out +=3D sym_len; + src =3D in; } - strcat(res, in); + src_len =3D strlen(src); + strscpy(out, src, src_len); + out +=3D src_len; + *out =3D '\0'; =20 return res; } =20 -const char *sym_escape_string_value(const char *in) +const char *sym_escape_string_value(const char *src) { - const char *p; - size_t reslen; - char *res; - size_t l; + const char *in; + size_t res_len, in_len; + char *res, *out; =20 - reslen =3D strlen(in) + strlen("\"\"") + 1; + res_len =3D strlen(src) + strlen("\"\"") + 1; =20 - p =3D in; + in =3D src; for (;;) { - l =3D strcspn(p, "\"\\"); - p +=3D l; + in_len =3D strcspn(in, "\"\\"); + in +=3D in_len; =20 - if (p[0] =3D=3D '\0') + if (*in =3D=3D '\0') break; =20 - reslen++; - p++; + res_len++; + in++; } =20 - res =3D xmalloc(reslen); - res[0] =3D '\0'; + res =3D xmalloc(res_len); + out =3D res; + *out++ =3D '\"'; =20 - strcat(res, "\""); - - p =3D in; + in =3D src; for (;;) { - l =3D strcspn(p, "\"\\"); - strncat(res, p, l); - p +=3D l; + in_len =3D strcspn(in, "\"\\"); + strscpy(out, in, in_len); + in +=3D in_len; + out +=3D in_len; =20 - if (p[0] =3D=3D '\0') + if (*in =3D=3D '\0') break; =20 - strcat(res, "\\"); - strncat(res, p++, 1); + *out++ =3D '\\'; + *out++ =3D *in++; } + *out++ =3D '\"'; + *out =3D '\0'; =20 - strcat(res, "\""); return res; } =20 @@ -1014,8 +1020,8 @@ static int sym_rel_comp(const void *sym1, const void = *sym2) * exactly; if this is the case, we can't decide which comes first, * and we fallback to sorting alphabetically. */ - exact1 =3D (s1->eo - s1->so) =3D=3D strlen(s1->sym->name); - exact2 =3D (s2->eo - s2->so) =3D=3D strlen(s2->sym->name); + exact1 =3D (s1->eo - s1->so) =3D=3D (off_t)strlen(s1->sym->name); + exact2 =3D (s2->eo - s2->so) =3D=3D (off_t)strlen(s2->sym->name); if (exact1 && !exact2) return -1; if (!exact1 && exact2) @@ -1390,31 +1396,33 @@ const char *prop_get_type_name(enum prop_type type) return "unknown"; } =20 -static void prop_add_env(const char *env) +static void prop_add_env(const char *env_key) { struct symbol *sym, *sym2; struct property *prop; - char *p; + char *env_val; =20 sym =3D current_entry->sym; sym->flags |=3D SYMBOL_AUTO; for_all_properties(sym, prop, P_ENV) { sym2 =3D prop_get_symbol(prop); - if (strcmp(sym2->name, env)) + if (strcmp(sym2->name, env_key)) menu_warn(current_entry, "redefining environment symbol from %s", sym2->name); return; } =20 prop =3D prop_alloc(P_ENV, sym); - prop->expr =3D expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST)); + prop->expr =3D expr_alloc_symbol(sym_lookup(env_key, SYMBOL_CONST)); =20 sym_env_list =3D expr_alloc_one(E_LIST, sym_env_list); sym_env_list->right.sym =3D sym; + env_val =3D getenv(env_key); =20 - p =3D getenv(env); - if (p) - sym_add_default(sym, p); - else - menu_warn(current_entry, "environment variable %s undefined", env); + if (likely(env_val)) { + sym_add_default(sym, env_val); + return; + } + + menu_warn(current_entry, "environment variable %s undefined", env_key); } --=20 2.16.2 --5oo6xr74tsg54goh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEKlZXrihdNOcUPZTNruvLfWhyVBkFAlqjufsACgkQruvLfWhy VBnPgw/+OMcFdo5BJ5otPdx+8lSdeiIRlnIv9NRnk4HTlsiJDyMBLWRc2QsIbMvp 5X2BbQorijnj+O25K9XEOZnU3bImPaik1XNZBhRC4e2JdeHwrgXiluJ/Ei1AZ9Wa jqFOz2YEFj2z8R6Zep/7Oceowe8GP7D/vZHSz8zn+l44T627ovSuXPwWVZQPkUDg 1XZ8IYXNXm7ItXvF6LTIYmPgl7n0+HajAKlPuEcqFe31q9QX7H5gMzyEcz/MGGed 8J3bmRlgIthI3G2NUv7LQZmiphwt/o+yaMn83V4cCEWjbk+1BW56MX4t3LUA+Bga W/WVkBvbTzX87l6tnVv1RSaqzhOt8VXlRHiydHxhs9gSTNciVVT0PMbODmpWNQpb JO5DsEeGAU6Jy4rYnB/IwLLH98vZpTVzNW+h+GOQD650bbUHQeseDkVErImdilNs EuW/qu68BRXniUCFonktFhmX44XGMs5x78out1kYscRBWSIok54nbO5/VaA2//Sy 54YwdVM0hYQxBsD3pxoB+nfbq7LyTg0iFQh1f7aQqmSHBEx58FxBTXlVrCB1RfMK NGZSw2FubXCy5QQpHNotOsZtMq+2jyjv/w17Yb8bMAWOFnf6JLQtejSP7z7ERbHX Pf+XDdyt33pHRiYGJLs8qE+gwKlzn8LE17oWrxaUsDdVboiRZd4= =mb9x -----END PGP SIGNATURE----- --5oo6xr74tsg54goh--