From d97ac81f20b83ac8e71ab59e8e3ec65d606bcc33 Mon Sep 17 00:00:00 2001 From: Ken Zangelin Date: Wed, 17 May 2023 11:09:44 +0200 Subject: [PATCH] Leaks fixed --- src/lib/cache/subCache.cpp | 4 +++ .../common/subCacheApiSubscriptionInsert.cpp | 8 ------ src/lib/orionld/q/qBuild.cpp | 2 -- .../orionldPatchSubscription.cpp | 26 +++++++++++++++++++ 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/lib/cache/subCache.cpp b/src/lib/cache/subCache.cpp index 6304d5b879..490ae9c77c 100644 --- a/src/lib/cache/subCache.cpp +++ b/src/lib/cache/subCache.cpp @@ -939,7 +939,11 @@ bool subCacheItemInsert { if (orionldState.apiVersion == NGSI_LD_V1) { + // FIXME: Instead of calling qBuild here, I should pass the pointer from pCheckSubscription + if (cSubP->qP != NULL) + qRelease(cSubP->qP); cSubP->qP = qBuild(q.c_str(), &cSubP->qText, &validForV2, &isMq, true); // cSubP->qText needs real allocation + if (cSubP->qText != NULL) cSubP->qText = strdup(cSubP->qText); else diff --git a/src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp b/src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp index f45b113480..e594bc695c 100644 --- a/src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp +++ b/src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp @@ -120,11 +120,7 @@ CachedSubscription* subCacheApiSubscriptionInsert(KjNode* apiSubscriptionP, QNod cSubP->lastFailure = lastFailureP->value.f; if (descriptionP != NULL) - { - if (cSubP->description != NULL) - free(cSubP->description); cSubP->description = strdup(descriptionP->value.s); - } if (qP != NULL) cSubP->expression.q = qP->value.s; @@ -133,11 +129,7 @@ CachedSubscription* subCacheApiSubscriptionInsert(KjNode* apiSubscriptionP, QNod cSubP->expression.mq = mqP->value.s; if (ldqP != NULL) - { - if (cSubP->qText != NULL) - free(cSubP->qText); cSubP->qText = strdup(ldqP->value.s); - } if (isActiveP != NULL) { diff --git a/src/lib/orionld/q/qBuild.cpp b/src/lib/orionld/q/qBuild.cpp index 82b6f9d692..ba45e0235f 100644 --- a/src/lib/orionld/q/qBuild.cpp +++ b/src/lib/orionld/q/qBuild.cpp @@ -69,8 +69,6 @@ // QNode* qBuild(const char* q, char** qRenderP, bool* v2ValidP, bool* isMqP, bool qToDbModel) { - LM_T(LmtQ, ("In qBuild: %s", q)); - QNode* qP = NULL; char* title; char* detail; diff --git a/src/lib/orionld/serviceRoutines/orionldPatchSubscription.cpp b/src/lib/orionld/serviceRoutines/orionldPatchSubscription.cpp index a6d00d8dfe..cecb04e04c 100644 --- a/src/lib/orionld/serviceRoutines/orionldPatchSubscription.cpp +++ b/src/lib/orionld/serviceRoutines/orionldPatchSubscription.cpp @@ -51,6 +51,7 @@ extern "C" #include "orionld/context/orionldAttributeExpand.h" // orionldAttributeExpand #include "orionld/payloadCheck/PCHECK.h" // PCHECK_URI #include "orionld/payloadCheck/pCheckSubscription.h" // pCheckSubscription +#include "orionld/q/qRelease.h" // qRelease #include "orionld/mongoc/mongocSubscriptionLookup.h" // mongocSubscriptionLookup #include "orionld/mongoc/mongocSubscriptionReplace.h" // mongocSubscriptionReplace #include "orionld/dbModel/dbModelFromApiSubscription.h" // dbModelFromApiSubscription @@ -319,7 +320,16 @@ static void fixDbSubscription(KjNode* dbSubscriptionP, char* qRenderedForDb) // static bool subCacheItemUpdateEntities(CachedSubscription* cSubP, KjNode* entityArray) { + // + // To replace "entities", we first need to frre up the old "entities" + // + for (long unsigned int ix = 0; ix < cSubP->entityIdInfos.size(); ix++) + { + cSubP->entityIdInfos[ix]->release(); + delete cSubP->entityIdInfos[ix]; + } cSubP->entityIdInfos.clear(); + for (KjNode* entityP = entityArray->value.firstChildP; entityP != NULL; entityP = entityP->next) { KjNode* idP = kjLookup(entityP, "id"); @@ -588,7 +598,11 @@ static bool subCacheItemUpdate(OrionldTenant* tenantP, const char* subscriptionI } if (qNodeP != NULL) + { + if (cSubP->qP != NULL) + qRelease(cSubP->qP); cSubP->qP = qNodeP; + } for (KjNode* itemP = subscriptionTree->value.firstChildP; itemP != NULL; itemP = itemP->next) { @@ -809,6 +823,8 @@ bool orionldPatchSubscription(void) &mqttChange); if (r == false) { + if (qNodeP != NULL) + qRelease(qNodeP); LM_E(("pCheckSubscription FAILED")); return false; } @@ -820,6 +836,8 @@ bool orionldPatchSubscription(void) if (dbSubscriptionP == NULL) { + if (qNodeP != NULL) + qRelease(qNodeP); orionldError(OrionldResourceNotFound, "Subscription not found", subscriptionId, 404); return false; } @@ -875,7 +893,11 @@ bool orionldPatchSubscription(void) CachedSubscription* cSubP = subCacheItemLookup(orionldState.tenantP->tenant, subscriptionId); if (ngsildSubscriptionPatch(dbSubscriptionP, cSubP, orionldState.requestTree, qP, geoqP, qRenderedForDb) == false) + { + if (qNodeP != NULL) + qRelease(qNodeP); LM_RE(false, ("ngsildSubscriptionPatch failed!")); + } // // Update modifiedAt @@ -898,6 +920,8 @@ bool orionldPatchSubscription(void) mqttInfoFromDbTree(dbSubscriptionP, newUriP, &newMqttInfo); if (mqttConnectFromInfo(&newMqttInfo) == false) { + if (qNodeP != NULL) + qRelease(qNodeP); orionldError(OrionldInternalError, "MQTT Error", "unable to connect to MQTT broker", 500); return false; } @@ -915,6 +939,8 @@ bool orionldPatchSubscription(void) // if (mongocSubscriptionReplace(subscriptionId, dbSubscriptionP) == false) { + if (qNodeP != NULL) + qRelease(qNodeP); orionldError(OrionldInternalError, "Database Error", "patching a subscription", 500); return false; }