diff mbox series

[3/3] kernel-shark: Correct memory management when data loading fails

Message ID 20190523150946.30769-4-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Commit 0c957d2d85787350083ac990b35428e665f392a5
Headers show
Series Fixes toward KS 1.0 | expand

Commit Message

Yordan Karadzhov May 23, 2019, 3:09 p.m. UTC
The erroneous case, when the memory allocation failed during data loading,
hasn't been properly handled.

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark/src/libkshark.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
diff mbox series

Patch

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;