From patchwork Thu May 23 15:09:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 10957951 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 27E8D924 for ; Thu, 23 May 2019 15:10:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12EF526E79 for ; Thu, 23 May 2019 15:10:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 07A062855C; Thu, 23 May 2019 15:10:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D535E26E79 for ; Thu, 23 May 2019 15:10:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730672AbfEWPKI (ORCPT ); Thu, 23 May 2019 11:10:08 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:39017 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730756AbfEWPKI (ORCPT ); Thu, 23 May 2019 11:10:08 -0400 Received: by mail-wm1-f67.google.com with SMTP id z23so2103761wma.4 for ; Thu, 23 May 2019 08:10:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UsHaSwSYqaEfZCjR8nTLFDCgOG8KHp7nw1/cLADerdA=; b=R7ltQqcJzf7RXokKGxg+iLJKCnNliYsDPeKV6hrFRWioKHltbPmK70KHA++s7kt9aq YkdSRaXffqXw3BjCx9mbUVw2GsVJVnz8GhhJD5+URvshP/GR3BYFl9FWC8UIjW7Rn53a jQeazI+plWhpPmF/Ts/COUV0rYKm6dLw4W6lR2/1scScD+Iv6V4c7/VZOVY7nhHxuJYj GOkEC3l7hrD6VdJMcO60RA8rxG5t1L7bgX21Dkj5stHiZo+JTItoF3HCcSRz1ni6TqAW eicBuQMGd8QtmBQEg2nbkBOgLAmyUkiFQ+bEr2nlTTvMESiVQjKVRiuBej9aUEEyy3Mj 9fjA== X-Gm-Message-State: APjAAAUHcC+lH6yB+xBLgdDu1RFgKJdDDeJ4EubJj9NCf7h2DBToRg1+ aPF0Lmk2X2YoerRlx18yyC8= X-Google-Smtp-Source: APXvYqy2hD3Rdql+5tGVQNXhKRbcu6lPWnhOwhByZvy8r7VjY2GPZFzxE/vLAOexBKNgM91zigl1Mw== X-Received: by 2002:a1c:9a14:: with SMTP id c20mr12925861wme.61.1558624206436; Thu, 23 May 2019 08:10:06 -0700 (PDT) Received: from mamba.eng.vmware.com ([146.247.46.5]) by smtp.gmail.com with ESMTPSA id k63sm10721996wmf.35.2019.05.23.08.10.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 May 2019 08:10:06 -0700 (PDT) From: Yordan Karadzhov To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org, y.karadz@gmail.com, Yordan Karadzhov Subject: [PATCH 3/3] kernel-shark: Correct memory management when data loading fails Date: Thu, 23 May 2019 18:09:46 +0300 Message-Id: <20190523150946.30769-4-ykaradzhov@vmware.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190523150946.30769-1-ykaradzhov@vmware.com> References: <20190523150946.30769-1-ykaradzhov@vmware.com> MIME-Version: 1.0 Sender: linux-trace-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The erroneous case, when the memory allocation failed during data loading, hasn't been properly handled. Signed-off-by: Yordan Karadzhov --- kernel-shark/src/libkshark.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/kernel-shark/src/libkshark.c b/kernel-shark/src/libkshark.c index b0018f8..175279c 100644 --- a/kernel-shark/src/libkshark.c +++ b/kernel-shark/src/libkshark.c @@ -889,12 +889,6 @@ ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx, fail_free: free_rec_list(rec_list, n_cpus, type); - for (count = 0; count < total; count++) { - if (!rows[count]) - break; - free(rows[count]); - } - free(rows); fail: fprintf(stderr, "Failed to allocate memory during data loading.\n"); @@ -924,15 +918,15 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx, ssize_t count, total = 0; int n_cpus; - total = get_records(kshark_ctx, &rec_list, REC_RECORD); + total = get_records(kshark_ctx, &rec_list, type); if (total < 0) goto fail; + n_cpus = tracecmd_cpus(kshark_ctx->handle); + rows = calloc(total, sizeof(struct tep_record *)); if (!rows) - goto fail; - - n_cpus = tracecmd_cpus(kshark_ctx->handle); + goto fail_free; for (count = 0; count < total; count++) { int next_cpu; @@ -955,6 +949,9 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx, *data_rows = rows; return total; + fail_free: + free_rec_list(rec_list, n_cpus, type); + fail: fprintf(stderr, "Failed to allocate memory during data loading.\n"); return -ENOMEM;