diff mbox series

[v2,20/34] kernelshark: Fix potential memory leaks in libkshark-configio

Message ID 20240128192539.56331-1-dev@benjarobin.fr (mailing list archive)
State Accepted
Commit e892dc5e34244276f67c73905f858ced8f818aef
Headers show
Series None | expand

Commit Message

Benjamin ROBIN Jan. 28, 2024, 7:25 p.m. UTC
Free previously allocated kshark_config_doc if format is not supported.

Also allow to call kshark_export_*() functions that allocate a new
kshark_config_doc with format equal to KS_CONFIG_AUTO.

Signed-off-by: Benjamin ROBIN <dev@benjarobin.fr>
---
 src/libkshark-configio.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/src/libkshark-configio.c b/src/libkshark-configio.c
index 9a1ba60..3a0e979 100644
--- a/src/libkshark-configio.c
+++ b/src/libkshark-configio.c
@@ -515,7 +515,7 @@  kshark_export_trace_file(const char *file, const char *name,
 	if (!conf)
 		return NULL;
 
-	switch (format) {
+	switch (conf->format) {
 	case KS_CONFIG_JSON:
 		kshark_trace_file_to_json(file, name, conf->conf_doc);
 		return conf;
@@ -523,6 +523,7 @@  kshark_export_trace_file(const char *file, const char *name,
 	default:
 		fprintf(stderr, "Document format %d not supported\n",
 			conf->format);
+		kshark_free_config_doc(conf);
 		return NULL;
 	}
 }
@@ -682,7 +683,7 @@  kshark_export_plugin_file(struct kshark_plugin_list *plugin,
 	if (!conf)
 		return NULL;
 
-	switch (format) {
+	switch (conf->format) {
 	case KS_CONFIG_JSON:
 		kshark_plugin_to_json(plugin, conf->conf_doc);
 		return conf;
@@ -690,6 +691,7 @@  kshark_export_plugin_file(struct kshark_plugin_list *plugin,
 	default:
 		fprintf(stderr, "Document format %d not supported\n",
 			conf->format);
+		kshark_free_config_doc(conf);
 		return NULL;
 	}
 }
@@ -740,12 +742,12 @@  kshark_export_all_plugins(struct kshark_context *kshark_ctx,
 			  enum kshark_config_formats format)
 {
 	struct kshark_config_doc *conf =
-		kshark_config_new("kshark.config.plugins", KS_CONFIG_JSON);
+		kshark_config_new("kshark.config.plugins", format);
 
 	if (!conf)
 		return NULL;
 
-	switch (format) {
+	switch (conf->format) {
 	case KS_CONFIG_JSON:
 		kshark_all_plugins_to_json(kshark_ctx, conf->conf_doc);
 		return conf;
@@ -753,6 +755,7 @@  kshark_export_all_plugins(struct kshark_context *kshark_ctx,
 	default:
 		fprintf(stderr, "Document format %d not supported\n",
 			conf->format);
+		kshark_free_config_doc(conf);
 		return NULL;
 	}
 }
@@ -868,12 +871,12 @@  kshark_export_stream_plugins(struct kshark_data_stream *stream,
 			     enum kshark_config_formats format)
 {
 	struct kshark_config_doc *conf =
-		kshark_config_new("kshark.config.plugins", KS_CONFIG_JSON);
+		kshark_config_new("kshark.config.plugins", format);
 
 	if (!conf)
 		return NULL;
 
-	switch (format) {
+	switch (conf->format) {
 	case KS_CONFIG_JSON:
 		kshark_stream_plugins_to_json(stream, conf->conf_doc);
 		return conf;
@@ -881,6 +884,7 @@  kshark_export_stream_plugins(struct kshark_data_stream *stream,
 	default:
 		fprintf(stderr, "Document format %d not supported\n",
 			conf->format);
+		kshark_free_config_doc(conf);
 		return NULL;
 	}
 }
@@ -1026,14 +1030,15 @@  kshark_export_model(struct kshark_trace_histo *histo,
 	if (!conf)
 		return NULL;
 
-	switch (format) {
+	switch (conf->format) {
 	case KS_CONFIG_JSON:
 		kshark_model_to_json(histo, conf->conf_doc);
 		return conf;
 
 	default:
 		fprintf(stderr, "Document format %d not supported\n",
-			format);
+			conf->format);
+		kshark_free_config_doc(conf);
 		return NULL;
 	}
 }