Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1364229ybg; Thu, 11 Jun 2020 08:01:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVQ+OI1d5h7mPDrjjNpY7++bYavq+CWQdsOafrTNw9RmeJtYSzWOqr9pSLe9gcmjeCijLZ X-Received: by 2002:a17:906:694c:: with SMTP id c12mr8736249ejs.495.1591887688419; Thu, 11 Jun 2020 08:01:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591887688; cv=none; d=google.com; s=arc-20160816; b=wvs12IC3ReWDor3xk1K0Rp3VixfoPmN0Wmr5FYTRP8DdRJC0KYoVOl2XO5SllhRuLo IpabSNCAppq6cNCw/CyRIHZQJKhr+8yH5gQNhMicWNQDXsIz2JoS5s72mfp8Q0ORjtUW 65KQi7gSevr8arZ1QGQ19O9s+WlxmKOlim6ropKAQck5qtc3a0G6tmdVdPpQF1p3+ebA JUyiYMynjjS73vTWHcWJ/pAFam7C816JZzvRahXJK47Xh2elMsB1AvIh+ZXLiIiRbbYs a3QfmFkxBhBMzKnoTe6z/ABYg3HIG95f0sWV+ywNAhr2JP2AKUSACEF78r3FjVfL/xUa 59lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=oNDP9cQdHqy5nkN0ZOunj53zAeX/9RodEBSiyPiTe90=; b=hRfC/BtMVVCm3SsNgqXAP4jvx/oQti6x6Qr1OonxFl4VHyyDmsINup+KhxP/YNrPsQ YVaoMFAUR2CAsvx9n0clEV0ZjuA0msXwa70YWLoIoXLqDv9ZSLyjHCalGKtZwdC+PIuD i3RmONbadCw5m50I/YwUsM2J6vS39YsuBVkBxOvAx+mu1Nt263mQm13kvRZcRbdi4r3F jEp5f0IDXqO8Hwzzdfm3ItsBNx+SLjddb/M4tajBPTZ62VoRRR57b1yXrtBuK6gY8lqP pix0MzsJA6oKLB6z88miCVrnxXhrfJd0cHJp4F1pM527bJrM7GxnWmW4dJJvDf0sNrxI C5Xw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qw12si2104973ejb.492.2020.06.11.08.01.03; Thu, 11 Jun 2020 08:01:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728413AbgFKO4u (ORCPT + 99 others); Thu, 11 Jun 2020 10:56:50 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42288 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728344AbgFKO4u (ORCPT ); Thu, 11 Jun 2020 10:56:50 -0400 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 00A9E489A0EC382B33C9; Thu, 11 Jun 2020 22:56:46 +0800 (CST) Received: from euler.huawei.com (10.175.124.27) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.487.0; Thu, 11 Jun 2020 22:56:38 +0800 From: Chen Wandun To: , CC: , , , , Subject: [PATCH next v2 2/2] perf tools: fix potential memleak in perf events parser Date: Thu, 11 Jun 2020 22:56:05 +0800 Message-ID: <20200611145605.21427-3-chenwandun@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200611145605.21427-1-chenwandun@huawei.com> References: <20200611145605.21427-1-chenwandun@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.124.27] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix potential memory leak. Function new_term may return error, so it is need to free memory when the return value is negative. Fixes: b6645a723595 ("perf parse: Ensure config and str in terms are unique") Signed-off-by: Chen Wandun --- tools/perf/util/parse-events.c | 47 ++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 6f4dc8a92817..a9f32032af08 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -2947,6 +2947,7 @@ int parse_events_term__sym_hw(struct parse_events_term **term, .type_term = PARSE_EVENTS__TERM_TYPE_USER, .config = config, }; + int ret; if (!temp.config) { temp.config = strdup("event"); @@ -2958,12 +2959,23 @@ int parse_events_term__sym_hw(struct parse_events_term **term, str = strdup(sym->symbol); if (!str) { - if (!config) - free(temp.config); - return -ENOMEM; + ret = -ENOMEM; + goto free_config; } - return new_term(term, &temp, str, 0); + ret = new_term(term, &temp, str, 0); + if (ret) + goto free_str; + + return 0; + +free_str: + free(str); +free_config: + if (!config) + free(temp.config); + + return ret; } int parse_events_term__clone(struct parse_events_term **new, @@ -2977,23 +2989,38 @@ int parse_events_term__clone(struct parse_events_term **new, .err_term = term->err_term, .err_val = term->err_val, }; + int ret; if (term->config) { temp.config = strdup(term->config); if (!temp.config) return -ENOMEM; } - if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) - return new_term(new, &temp, NULL, term->val.num); + if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { + ret = new_term(new, &temp, NULL, term->val.num); + if (ret) + goto free_config; + } str = strdup(term->val.str); if (!str) { - if (term->config) - free(temp.config); - return -ENOMEM; + ret = -ENOMEM; + goto free_config; } - return new_term(new, &temp, str, 0); + ret = new_term(new, &temp, str, 0); + if (ret) + goto free_str; + + return 0; + +free_str: + free(str); +free_config: + if (term->config) + free(temp.config); + + return ret; } void parse_events_term__delete(struct parse_events_term *term) -- 2.17.1