From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B90BC61D97 for ; Wed, 22 Nov 2023 15:38:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344403AbjKVPia (ORCPT ); Wed, 22 Nov 2023 10:38:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344404AbjKVPiG (ORCPT ); Wed, 22 Nov 2023 10:38:06 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 490EA3879; Wed, 22 Nov 2023 07:36:01 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C00FC433AD; Wed, 22 Nov 2023 15:36:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700667360; bh=Lq00onOaGhMQrQm6KMEWgcF+DMXr0QemXLunGniudF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pQRB8xFfRd/9S+tpfoBdeXtJpgUz1ftEFNxtRdr3PWJ+/nnFE7oPFg4ttOvMEI1lF mVHYzSsgQcnuKPpZ59w3gn/qD2/p8TE9wKtHtp6hgjZuyFj/H/XRJUYnF7PhRuO5MU i+lrLC/USM7BnUxvj28FuxTZgrUvSg2iVDbSdjbhso4c8nQg6p8pEer7T90puUB8Ic QO6koqJP1JakK8eBNRJRzHx9ZLTJh+Kf2P0md4hLBCFvQza6gZgRAOVObN7yxYbEQT FW4ixiUfhx08AHfRPHN0Xod8qtcc+pdqpV91miujuOBa2I3bPqEzcR3pf2J6q5Kcw3 f57FunZeTluQQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Masahiro Yamada , Sasha Levin , linux-kbuild@vger.kernel.org Subject: [PATCH AUTOSEL 5.10 6/7] kconfig: fix memory leak from range properties Date: Wed, 22 Nov 2023 10:35:34 -0500 Message-ID: <20231122153541.853179-6-sashal@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231122153541.853179-1-sashal@kernel.org> References: <20231122153541.853179-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.10.201 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masahiro Yamada [ Upstream commit ae1eff0349f2e908fc083630e8441ea6dc434dc0 ] Currently, sym_validate_range() duplicates the range string using xstrdup(), which is overwritten by a subsequent sym_calc_value() call. It results in a memory leak. Instead, only the pointer should be copied. Below is a test case, with a summary from Valgrind. [Test Kconfig] config FOO int "foo" range 10 20 [Test .config] CONFIG_FOO=0 [Before] LEAK SUMMARY: definitely lost: 3 bytes in 1 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,465 bytes in 21 blocks suppressed: 0 bytes in 0 blocks [After] LEAK SUMMARY: definitely lost: 0 bytes in 0 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks still reachable: 17,462 bytes in 20 blocks suppressed: 0 bytes in 0 blocks Signed-off-by: Masahiro Yamada Signed-off-by: Sasha Levin --- scripts/kconfig/symbol.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c index ffa3ec65cc907..a2056fa80de2b 100644 --- a/scripts/kconfig/symbol.c +++ b/scripts/kconfig/symbol.c @@ -123,9 +123,9 @@ static long long sym_get_range_val(struct symbol *sym, int base) static void sym_validate_range(struct symbol *sym) { struct property *prop; + struct symbol *range_sym; int base; long long val, val2; - char str[64]; switch (sym->type) { case S_INT: @@ -141,17 +141,15 @@ static void sym_validate_range(struct symbol *sym) if (!prop) return; val = strtoll(sym->curr.val, NULL, base); - val2 = sym_get_range_val(prop->expr->left.sym, base); + range_sym = prop->expr->left.sym; + val2 = sym_get_range_val(range_sym, base); if (val >= val2) { - val2 = sym_get_range_val(prop->expr->right.sym, base); + range_sym = prop->expr->right.sym; + val2 = sym_get_range_val(range_sym, base); if (val <= val2) return; } - if (sym->type == S_INT) - sprintf(str, "%lld", val2); - else - sprintf(str, "0x%llx", val2); - sym->curr.val = xstrdup(str); + sym->curr.val = range_sym->curr.val; } static void sym_set_changed(struct symbol *sym) -- 2.42.0