Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1322715ybt; Sun, 14 Jun 2020 18:53:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy3HzX5HSjkaI519HD4L1jVImZZo5ReBPD3wpyN511g5CCXTMQ+uflD/UPOLEqVT1hXQmm6 X-Received: by 2002:a05:6402:7d4:: with SMTP id u20mr21395596edy.30.1592186006262; Sun, 14 Jun 2020 18:53:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592186006; cv=none; d=google.com; s=arc-20160816; b=X6MeYW8KnSFvF08hPUfNb+dQcj4h4+BjcKBrCm5dh99DjibR7soBqk+HR4s2MuYEwb h/peYP9d17zTObvAtklMpX/z3xxKHb8lQkhZIhpmIJJNzFJ7yzI2zcCBXeBcWcQY53km ywta+cT11vzqkmlGK/IVodMRjZPwtUmtDxAUwQXciYcUkuatO0XOcatd2pk3CixTV61R JCEWRsoMPqfD/u7UPt+y/GGkjLZwrus45ijfekVi0CwWh3OiwxxcDdn+PgHWYtZEXA8x 1ShrZ9pGjcjJoaG4gXQ5n+bBceq/QC2+ZimvfAy1pXN65aZWP1uMjo7/shgoOobM3zB2 7/Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=dP7pJHoAuHDlKFHbB7gWrpSn8MSYgOjR20J4uKhS2DE=; b=oRO1lyRksUvrpMjQHyKGJVL9T/7epckqcMhX9zPjyvmb15ZuQJb2MwQsaXHDtxkUKr aj2yoIifeQZ8PkhEKMUpccuEgjeNtHQ0VtU6rZyj7hwhlLPTo5AEzNWEF37bivoy+C/P VSEgd4IIeCwcRRkwCy2TEbDL9b1ElpqV2EcA/3maS1aRjLmOHYQBUSTq36P2cqGs3quv fqlWepMNDYStwVAVDOWwTNf9MTRtycUInjfk3btk2DLytw3cnVed6fiDpGGHsbDlNTVH x5xYw5bBxdD8p4jH4bRRD7+jtFAYh4LdSUEXXMG0xjn09P+937ZFQbYj17Cwlprahq8q prOQ== 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 i22si8238187eja.116.2020.06.14.18.53.03; Sun, 14 Jun 2020 18:53:26 -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 S1728092AbgFOBuo (ORCPT + 99 others); Sun, 14 Jun 2020 21:50:44 -0400 Received: from mail.kernel.org ([198.145.29.99]:48406 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727946AbgFOBuo (ORCPT ); Sun, 14 Jun 2020 21:50:44 -0400 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 30E382053B; Mon, 15 Jun 2020 01:50:43 +0000 (UTC) Date: Sun, 14 Jun 2020 21:50:41 -0400 From: Steven Rostedt To: Gaurav Singh Cc: Arnaldo Carvalho de Melo , Tzvetomir Stoyanov , Konstantin Khlebnikov , Sakari Ailus , linux-kernel@vger.kernel.org (open list) Subject: Re: [PATCH] [traceevent] add_new_comm(): Fix memory leak Message-ID: <20200614215041.024f818f@gandalf.local.home> In-Reply-To: <20200614181517.30187-1-gaurav1086@gmail.com> References: <20200614181517.30187-1-gaurav1086@gmail.com> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 14 Jun 2020 14:14:53 -0400 Gaurav Singh wrote: > The pointer cmdlines need to be explicity freed in case the > realloc() fails. Fix it by adding a free() if realloc() > returns a NULL pointer. > > Signed-off-by: Gaurav Singh > --- > tools/lib/traceevent/event-parse.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c > index e1bd2a93c6db..7f0133420931 100644 > --- a/tools/lib/traceevent/event-parse.c > +++ b/tools/lib/traceevent/event-parse.c > @@ -284,12 +284,13 @@ static int add_new_comm(struct tep_handle *tep, > return 0; > } > > - cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1)); > - if (!cmdlines) { > + struct tep_cmdline *new_cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (tep->cmdline_count + 1)); > + if (!new_cmdlines) { > + free(cmdlines); > errno = ENOMEM; NAK! This was fine as is. What you just did was make tep->cmdline point to freed data, which will crash later on when tep is freed, and we free tep->cmdline. If we fail to realloc, then the caller of add_new_comm will get the report that it failed, and then can determine what to do next. We don't want to touch tep->cmdline on failure. -- Steve > return -1; > } > - tep->cmdlines = cmdlines; > + tep->cmdlines = new_cmdlines; > > key.comm = strdup(comm); > if (!key.comm) {