control: Retain current backend on initialisation failure

A failure to initialise a new backend should not lead to the current
backend becoming invalid. Fix the current behaviour by only destroying
the current backend once we know the requested backend has successfully
initialised.

Change-Id: I5da11b346b8b5ab2c95085f512de4599d1272f57
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/control.c b/control.c
index 4b73efa..4f472f1 100644
--- a/control.c
+++ b/control.c
@@ -120,6 +120,7 @@
 int control_set_backend(struct mbox_context *context, struct backend *backend,
 			void *data)
 {
+	struct backend successor;
 	int rc;
 
 	if (context->state & STATE_SUSPENDED)
@@ -129,12 +130,14 @@
 	if (rc < 0)
 		return rc;
 
-	backend_free(&context->backend);
-
-	rc = backend_init(&context->backend, backend, data);
+	rc = backend_init(&successor, backend, data);
 	if (rc < 0)
 		return rc;
 
+	backend_free(&context->backend);
+
+	context->backend = successor;
+
 	rc = __protocol_reset(context);
 	if (rc < 0)
 		return rc;