Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4723670iob; Sun, 8 May 2022 23:19:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSMTvywdFCEXOuPHhHz3XCyg9oyRjokg0gKKtmyfJmQ72UWw+lC0juZFDkFvsyY9TPQXV8 X-Received: by 2002:aa7:88d2:0:b0:50a:cf7d:6ff1 with SMTP id k18-20020aa788d2000000b0050acf7d6ff1mr14538484pff.67.1652077157610; Sun, 08 May 2022 23:19:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652077157; cv=none; d=google.com; s=arc-20160816; b=VDna9lnyA7iGKLBtUcqV/ttLC3exmP2BNrRqxk3G05/dKcQyzS4Qviwqt92HX77j+z NhCx73mN4c/eDoBe8z/JIY5YPY2UakFbTlLoeSyc8dyE+cuMIv0rHgsu9CWOG9BRuAOH pbTPZoO7sYa53/dXdSp1Cjws5k5iLsSx2jDTyrOUkSJi2DpoYrzwMyGXxkmYY7eeYWvn ExuZWGKf9yotCEH3grn0k7Aixdq6vB4qQzqq0/OusxvE/NdZIts359xwH7xW5zhuva1+ lYLKyUf6lqVCLYguqt2+HKVVFTa0QL1o1Fj3GUVEk32MxNoQ+TtuFxw26m9IVJBI3K5O Ofjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-filter; bh=hj4TDTKpxx14ggbXLHdhsGaLfOrEF2umBqeenV9WgeA=; b=M+3JXUYa1KCrthE/sMRANgF9boSBy6zL2KcbZeaC4uniPds7yB4ehRtyCrEUNsfqck DhhDRLcaT2mgFf3F+45RAlQnXmjEZoq1H85k8/lZqZq336lfCwN2fonWgI+66PinJuLA 6VBamptqn04Exk34+eVxQZxhm8rZHAGCS8dgRjNw8FjKkj/e28Sw6dAD7mCG9WWd/JJK fRTjOtQt2A7U1pvRnFlCrnClvqhqnddgAPGWV3YjcVUQdww+c8xn48n1NvMU41m67hMl Va/UlZpzFkmZtHMSzJd9C5yncWLmssw0KP0eBGg9tow41LC1zZSkSspEbl7H4HJt/ubO FENA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id l9-20020a655609000000b003aa91344e53si11431067pgs.362.2022.05.08.23.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 23:19:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 59E3EBCC; Sun, 8 May 2022 23:15:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352051AbiEGRjK (ORCPT + 99 others); Sat, 7 May 2022 13:39:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242983AbiEGRjF (ORCPT ); Sat, 7 May 2022 13:39:05 -0400 Received: from mxout03.lancloud.ru (mxout03.lancloud.ru [45.84.86.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D6E22BD3 for ; Sat, 7 May 2022 10:35:17 -0700 (PDT) Received: from LanCloud DKIM-Filter: OpenDKIM Filter v2.11.0 mxout03.lancloud.ru AA2AD20EBC8F Received: from LanCloud Received: from LanCloud Received: from LanCloud From: Ildar Kamaletdinov To: CC: Ildar Kamaletdinov Subject: [PATCH BlueZ 2/4] tools: Fix memory leaks in btgatt-server/client Date: Sat, 7 May 2022 20:35:03 +0300 Message-ID: <20220507173505.31249-3-i.kamaletdinov@omp.ru> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220507173505.31249-1-i.kamaletdinov@omp.ru> References: <20220507173505.31249-1-i.kamaletdinov@omp.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [192.168.11.198] X-ClientProxiedBy: LFEXT01.lancloud.ru (fd00:f066::141) To LFEX1910.lancloud.ru (fd00:f066::80) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org According to man buffer allocated by getline() should be freed by the user program even if getline() failed. Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool. --- tools/btgatt-client.c | 6 +++++- tools/btgatt-server.c | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c index 8c9365aa2..58a03bd48 100644 --- a/tools/btgatt-client.c +++ b/tools/btgatt-client.c @@ -1355,12 +1355,16 @@ static void prompt_read_cb(int fd, uint32_t events, void *user_data) return; } - if ((read = getline(&line, &len, stdin)) == -1) + read = getline(&line, &len, stdin); + if (read < 0) { + free(line); return; + } if (read <= 1) { cmd_help(cli, NULL); print_prompt(); + free(line); return; } diff --git a/tools/btgatt-server.c b/tools/btgatt-server.c index 4a5d2b720..90a6c9b0a 100644 --- a/tools/btgatt-server.c +++ b/tools/btgatt-server.c @@ -1080,12 +1080,15 @@ static void prompt_read_cb(int fd, uint32_t events, void *user_data) } read = getline(&line, &len, stdin); - if (read < 0) + if (read < 0) { + free(line); return; + } if (read <= 1) { cmd_help(server, NULL); print_prompt(); + free(line); return; } -- 2.35.3