From d53c8301bf1d51d4001a93acdb54adf40c78e207 Mon Sep 17 00:00:00 2001 From: Marcus Fritzsch Date: Tue, 5 Sep 2017 09:48:46 +0200 Subject: AFBClient: prevent init if already initialized Signed-off-by: Marcus Fritzsch --- AFBClient.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AFBClient.cpp b/AFBClient.cpp index 5ffad29..497b746 100644 --- a/AFBClient.cpp +++ b/AFBClient.cpp @@ -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(&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; -- cgit 1.2.3-korg