AFBClient: prevent init if already initialized
authorMarcus Fritzsch <marcus_fritzsch@mentor.com>
Tue, 5 Sep 2017 07:48:46 +0000 (09:48 +0200)
committerMarcus Fritzsch <marcus_fritzsch@mentor.com>
Thu, 14 Sep 2017 12:04:51 +0000 (14:04 +0200)
Signed-off-by: Marcus Fritzsch <marcus_fritzsch@mentor.com>
AFBClient.cpp

index 5ffad29..497b746 100644 (file)
@@ -206,7 +206,6 @@ AFBClient::Impl::~Impl() {
     TRACE();
     afb_wsj1_unref(wsj1);
     sd_event_unref(loop);
-    loop = nullptr;
 }
 
 int AFBClient::Impl::init(int port, char const *token) {
@@ -214,6 +213,12 @@ int AFBClient::Impl::init(int port, char const *token) {
     char *uribuf = nullptr;
     int rc = -1;
 
+    if (this->loop != nullptr && this->wsj1 != nullptr) {
+        fputs("AFBClient instance is already initialized!\n", stderr);
+        rc = -EALREADY;
+        goto fail;
+    }
+
     if ((token == nullptr) || strlen(token) > token_maxlen) {
         fprintf(stderr, "Token is invalid\n");
         rc = -EINVAL;
@@ -248,7 +253,8 @@ int AFBClient::Impl::init(int port, char const *token) {
     wsj1 = afb_ws_client_connect_wsj1(
         loop, uribuf, const_cast<struct afb_wsj1_itf *>(&itf), this);
     if (wsj1 == nullptr) {
-        sd_event_unref(loop);
+        sd_event_unref(this->loop);
+        this->loop = nullptr;
         fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
         rc = -errno;
         goto fail;