From f3b927f1415695765ce95b2aeeff8d69849e23d9 Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Sun, 26 Mar 2017 17:07:00 -0500
Subject: [PATCH] Fixed memory leak in frontend_nodes

---
 groundStation/src/frontend/frontend_nodes.c | 24 ++++++++++++++++++---
 groundStation/src/frontend/frontend_nodes.h | 13 ++++++++---
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/groundStation/src/frontend/frontend_nodes.c b/groundStation/src/frontend/frontend_nodes.c
index 6f2ef72e9..ffa7c188e 100644
--- a/groundStation/src/frontend/frontend_nodes.c
+++ b/groundStation/src/frontend/frontend_nodes.c
@@ -11,14 +11,22 @@
  *
  * Returns 0 on success, 1 on error
  *
+ * node_data and num_nodes are out-pointers.
+ * node_data must be a pointer to NULL
+ * num_nodes must be a pointer to zero
  * 
- * Note : node_data may be resized inside this function. 
- * That is why you must pass in a strcut ... **
  */
 int frontend_getnodes(
 		struct backend_conn * conn,
 		struct frontend_node_data ** node_data,
 		size_t * num_nodes) {
+	if ((node_data == NULL) || (num_nodes == NULL)) {
+		return 1;
+	}
+
+	if ((*node_data != NULL) || (*num_nodes != 0)) {
+		return 1;
+	}
 
 	char msg[64] = "";
 	int written;
@@ -122,4 +130,14 @@ int frontend_addnode(
 	}
 	
 	return 0;
-}
\ No newline at end of file
+}
+
+void frontend_free_node_data(
+		struct frontend_node_data *nd,
+		size_t num_nodes)
+{
+	for (size_t i = 0; i < num_nodes; i++) {
+		free(nd[i].name);
+	}
+	free(nd);
+}
diff --git a/groundStation/src/frontend/frontend_nodes.h b/groundStation/src/frontend/frontend_nodes.h
index 8410d12de..6b0a34062 100644
--- a/groundStation/src/frontend/frontend_nodes.h
+++ b/groundStation/src/frontend/frontend_nodes.h
@@ -8,8 +8,10 @@
  *
  * Returns 0 on success, 1 on error
  *
- * Note : node_data may be resized inside this function. 
- * That is why you must pass in a strcut ... **
+ * node_data and num_nodes are out pointers
+ * node_data must point to a NULL pointer
+ * num_nodes must point to zero
+ *
  */
 int frontend_getnodes(
 		struct backend_conn * conn,
@@ -25,4 +27,9 @@ int frontend_addnode(
 		struct backend_conn * conn,
 		struct frontend_node_data * node_data);
 
-#endif /* __FRONTEND_NODES_H */
\ No newline at end of file
+/* Free a node_data array */
+void frontend_free_node_data(
+		struct frontend_node_data * nd,
+		size_t num_nodes);
+
+#endif /* __FRONTEND_NODES_H */
-- 
GitLab