core: Cleanup after failing to start binding

Previously we changed mctp_register_bus() to return the error if the
binding failed to start. That change was broken in that ignored
resources that had been allocated on the path to invoking the binding
start function. Add the necessary cleanup.

Fixes: 0dc1375dbc41 ("core: Propagate binding start-up errors in mctp_register_bus()")
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I232242b3be76db9d0c7de60262779cb768173416
diff --git a/core.c b/core.c
index 95c38aa..6e59993 100644
--- a/core.c
+++ b/core.c
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
 
 #include <assert.h>
+#include <errno.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
@@ -273,10 +274,16 @@
 		struct mctp_binding *binding,
 		mctp_eid_t eid)
 {
+	int rc = 0;
+
 	/* todo: multiple busses */
 	assert(mctp->n_busses == 0);
 	mctp->n_busses = 1;
+
 	mctp->busses = __mctp_alloc(sizeof(struct mctp_bus));
+	if (!mctp->busses)
+		return -ENOMEM;
+
 	memset(mctp->busses, 0, sizeof(struct mctp_bus));
 	mctp->busses[0].binding = binding;
 	mctp->busses[0].eid = eid;
@@ -284,10 +291,16 @@
 	binding->mctp = mctp;
 	mctp->route_policy = ROUTE_ENDPOINT;
 
-	if (binding->start)
-		return binding->start(binding);
+	if (binding->start) {
+		rc = binding->start(binding);
+		if (rc < 0) {
+			mctp_prerr("Failed to start binding: %d", rc);
+			__mctp_free(mctp->busses);
+			mctp->busses = NULL;
+		}
+	}
 
-	return 0;
+	return rc;
 }
 
 int mctp_bridge_busses(struct mctp *mctp,