Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1914962lqb; Mon, 27 May 2024 01:27:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8JVi8tXK/mxRGCQYIRCICMBP7ckJRggNx1FuL2e9TgNmcIZr4AhWqd8hZvZlU18Iigv5Oul5gWSOMCndwjpPcJEYgOm39VdN2bWccgg== X-Google-Smtp-Source: AGHT+IGmeP2YXFsK9UJBMIw67gk2B0FxHQMrUE9+BQJEGfkhVyeLCoNUpYFjpXiekX2mYdCPJRak X-Received: by 2002:a05:620a:988:b0:793:fdb:c9a with SMTP id af79cd13be357-794ab05ceb7mr969068585a.6.1716798466379; Mon, 27 May 2024 01:27:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716798466; cv=pass; d=google.com; s=arc-20160816; b=j1KK7XzsxnHJ9gexIrolgNV9Fsd3yJWqSyYkcC1owDWi0IFxyHaJ71zkAbtD1JTo/N FVWlcf3PwNqtN+MSSE1vE6ulC2U24geBiOmD1W1fMQl07bNwS2qG52revNgvCEzfNsGq kOgQMzWReODtTbQdsvLdiw7X3jBQWPLzorNFNkDS7oAgk5ajW/1jQfB8LreIH6BjGQ46 EF0aYPmD7b14NJtn773OglnMfChw2LRkm0Vp0CpLhVW9V4YE62bIzDHEYGlhWz7cBgMO oL9FKUoYaAwMXnnOB3WUy/ICEZsqGAq/HxUKc8dMH6zWvSh1PxY1NxreBYctZ0NmL6Cw YuFA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=ZhZjUilNRK98NKk0PgUpOnL6APuWzRtChNBYlBFqNXg=; fh=uo5u2TQK/HQlhydMwGLjBAnuDwaLxndJ/cgWTjIagS8=; b=buA0Re3CTT1rRQMSSSU0kJ7eT3LF4/QSMoz5DzEXndzwMc+TJjEe1YnX1u9bviINvd EDVtmP43+6KW18ZIGjJbclLt8otouEZvGyNqcqSszVll9mpz77ZQKy6oY8c0vRmuaFpP MHNrPU1Pk4FY4mChGooM+yIxZ38fAuZPfLp07of08O/wph5SN1Twqx6Ba/S4gu+mX1cB 5I6fxz6sqWfMff/MmCGCnCfINJig30SDIFxsynv6K+CgmzjPCbbB5gRuOnFwb4xOKTzD uKJPRlXgRhtkyqaPpJvR7dhLvXMJZBjbokqq/k4O9OkaNxEuQzIaMRpSxsEbN5Y9dZM/ OsYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-4964-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4964-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id af79cd13be357-794abcac363si800041585a.38.2024.05.27.01.27.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 01:27:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-4964-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=hadess.net); spf=pass (google.com: domain of linux-bluetooth+bounces-4964-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4964-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 06BA11C20366 for ; Mon, 27 May 2024 08:27:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5FE9853368; Mon, 27 May 2024 08:27:41 +0000 (UTC) X-Original-To: linux-bluetooth@vger.kernel.org Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A9CE751C21 for ; Mon, 27 May 2024 08:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716798461; cv=none; b=FnYt1flhcSAqTID7/1rVM2n7ccWjd+FWjMto/h/pX0oExS2rj48TbxjKBfPCZwxPgsF6weKwW1iafEifanIaAJM+YRxkHRg3v5FXowf6QQc1tiPSHDf29S14xQJSm8QNsmOtL5Ce3yNj5SZQRVY4LRhShumI4yaK1TaABbYqpME= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716798461; c=relaxed/simple; bh=uUuxnrBqCgbeLa8XyOmR/4JUO+r1wy7Pa+/qxQzSELw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Qk03dyx1SefJoZA2XZ2m4hBw4FIcfpmzJrIguCaAffaEUpIyEZ0tn9hPZ3yfRO7lpmoMlgTitOaSUqXWqPaYxu8Exfi/Xlq+wVtn3Xu68lIYhLzExnqa6BBZ11ORkk0kJHHBB284vX5P43AvqQ6EpvXeduitlNIKIyGc71sqckE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net; spf=pass smtp.mailfrom=hadess.net; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=hadess.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=hadess.net Received: by mail.gandi.net (Postfix) with ESMTPSA id CE16CE000A; Mon, 27 May 2024 08:27:30 +0000 (UTC) From: Bastien Nocera To: linux-bluetooth@vger.kernel.org Cc: Bastien Nocera Subject: [BlueZ] main: Simplify parse_config_string() Date: Mon, 27 May 2024 10:26:51 +0200 Message-ID: <20240527082730.967400-1-hadess@hadess.net> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: hadess@hadess.net The memory management done by parse_config_string() was quite complicated, as it expected to be able to free the value in the return variable if it was already allocated. That particular behaviour was only used for a single variable which was set to its default value during startup and might be overwritten after this function call. Use an intermediate variable to check whether we need to free btd_opts.name and simplify parse_config_string(). Error: RESOURCE_LEAK (CWE-772): [#def39] [important] bluez-5.75/src/main.c:425:2: alloc_fn: Storage is returned from allocation function "g_key_file_get_string". bluez-5.75/src/main.c:425:2: var_assign: Assigning: "tmp" = storage returned from "g_key_file_get_string(config, group, key, &err)". bluez-5.75/src/main.c:433:2: noescape: Assuming resource "tmp" is not freed or pointed-to as ellipsis argument to "btd_debug". bluez-5.75/src/main.c:440:2: leaked_storage: Variable "tmp" going out of scope leaks the storage it points to. 438| } 439| 440|-> return true; 441| } 442| --- Essentially a v3 of "main: Simplify variable assignment" with a better commit message. src/main.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index ac840d684f6d..f6369a20d879 100644 --- a/src/main.c +++ b/src/main.c @@ -420,9 +420,10 @@ static bool parse_config_string(GKeyFile *config, const char *group, const char *key, char **val) { GError *err = NULL; - char *tmp; - tmp = g_key_file_get_string(config, group, key, &err); + g_return_val_if_fail(val, false); + + *val = g_key_file_get_string(config, group, key, &err); if (err) { if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) DBG("%s", err->message); @@ -430,12 +431,7 @@ static bool parse_config_string(GKeyFile *config, const char *group, return false; } - DBG("%s.%s = %s", group, key, tmp); - - if (val) { - g_free(*val); - *val = tmp; - } + DBG("%s.%s = %s", group, key, *val); return true; } @@ -1005,7 +1001,12 @@ static void parse_secure_conns(GKeyFile *config) static void parse_general(GKeyFile *config) { - parse_config_string(config, "General", "Name", &btd_opts.name); + char *str = NULL; + + if (parse_config_string(config, "General", "Name", &str)) { + g_free(btd_opts.name); + btd_opts.name = str; + } parse_config_hex(config, "General", "Class", &btd_opts.class); parse_config_u32(config, "General", "DiscoverableTimeout", &btd_opts.discovto, -- 2.44.0