diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-04 17:28:04 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-14 11:00:49 +0100 |
commit | eba541dae4461603e16efc12019c38e87eb4fcb1 (patch) | |
tree | 1231c0720df005b791824da9efc6c052962cd0a5 | |
parent | 55e4e3b31f2cbec776eb89c0b780f895fa0f0b86 (diff) |
Avoid mem leack, overwriting, initializing variable.
Fix a memory squashing bug that segfault binder when
loading directory searching for binding.
Change-Id: Iedc6747d8ce49e7ff5fc2013bb7d03b5b553b89c
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | signal-composer-binding/signal-composer.cpp | 17 | ||||
-rw-r--r-- | signal-composer-binding/signal.cpp | 2 | ||||
-rw-r--r-- | signal-composer-binding/source.cpp | 4 |
3 files changed, 15 insertions, 8 deletions
diff --git a/signal-composer-binding/signal-composer.cpp b/signal-composer-binding/signal-composer.cpp index 572a6c4..d9d004c 100644 --- a/signal-composer-binding/signal-composer.cpp +++ b/signal-composer-binding/signal-composer.cpp @@ -177,6 +177,7 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio char *function; const char *plugin; CtlActionT *ctlAction = new CtlActionT; + memset(ctlAction, 0, sizeof(CtlActionT)); if(actionJ && !wrap_json_unpack(actionJ, "{ss,s?s,s?o !}", "function", &function, @@ -218,9 +219,12 @@ CtlActionT* Composer::convert2Action(const std::string& name, json_object* actio } if(ctlActionJ) - {ActionLoadOne(nullptr, ctlAction, ctlActionJ, 0);} + { + if(!ActionLoadOne(nullptr, ctlAction, ctlActionJ, 0)) + return ctlAction; + } - return ctlAction; + return nullptr; } /// @brief Add the builtin plugin in the default plugins section definition @@ -359,13 +363,13 @@ int Composer::loadOneSignal(json_object* signalJ) const char *id = nullptr, *event = nullptr, *unit = nullptr; - int retention; + int retention = 0; double frequency=0.0; std::vector<std::string> dependsV; ssize_t sep; std::shared_ptr<SourceAPI> src = nullptr; - int err = wrap_json_unpack(signalJ, "{ss,s?s,s?o,s?o,s?F,s?s,s?F,s?o !}", + int err = wrap_json_unpack(signalJ, "{ss,s?s,s?o,s?o,s?i,s?s,s?F,s?o !}", "uid", &id, "event", &event, "depends", &dependsJ, @@ -443,8 +447,9 @@ int Composer::loadOneSignal(json_object* signalJ) unit = !unit ? "" : unit; // Set default onReceived action if not specified - char* uid = strndup("onReceived_", 11); - uid = strncat(uid, id, strlen(id)); + char uid[CONTROL_MAXPATH_LEN] = "onReceived_"; + strncat(uid, id, strlen(id)); + if(!onReceivedJ) { onReceivedCtl = src->signalsDefault().onReceived ? diff --git a/signal-composer-binding/signal.cpp b/signal-composer-binding/signal.cpp index 2af440c..17286f5 100644 --- a/signal-composer-binding/signal.cpp +++ b/signal-composer-binding/signal.cpp @@ -142,7 +142,7 @@ json_object* Signal::toJSON() const struct signalCBT* Signal::get_context() { - struct signalCBT* ctx = (struct signalCBT*)calloc (1, sizeof(struct signalCBT)); + struct signalCBT* ctx = new struct signalCBT; if(!ctx->searchNsetSignalValue) {ctx->searchNsetSignalValue = searchNsetSignalValueHandle;} diff --git a/signal-composer-binding/source.cpp b/signal-composer-binding/source.cpp index 9e10e65..830f799 100644 --- a/signal-composer-binding/source.cpp +++ b/signal-composer-binding/source.cpp @@ -112,12 +112,14 @@ void SourceAPI::makeSubscription() AFB_ERROR("Error building JSON query object to subscribe to for signal %s", sig.second->id().c_str()); break; } - source.uid = sig.second->id().c_str(); + source.uid = sig.first.c_str(); source.context = (void*)sig.second->get_context(); ActionExecOne(&source, getSignals_, signalJ); // Considerate signal subscribed no matter what sig.second->subscribed_ = true; + delete(source.context); } + source.uid = ""; ActionExecOne(&source, getSignals_, nullptr); } } |