Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardening/valgrind #1491

Merged
merged 9 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/app/orionld/orionld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ extern "C"
#include "orionld/context/orionldContextFromUrl.h" // contextDownloadListInit, contextDownloadListRelease
#include "orionld/contextCache/orionldContextCacheRelease.h" // orionldContextCacheRelease
#include "orionld/rest/orionldServiceInit.h" // orionldServiceInit
#include "orionld/entityMaps/entityMapsRelease.h" // entityMapsRelease
#include "orionld/db/dbInit.h" // dbInit
#include "orionld/mqtt/mqttRelease.h" // mqttRelease
#include "orionld/regCache/regCacheInit.h" // regCacheInit
#include "orionld/regCache/regCacheCreate.h" // regCacheCreate
#include "orionld/regCache/regCacheRelease.h" // regCacheRelease
#include "orionld/pernot/pernotSubCacheInit.h" // pernotSubCacheInit
#include "orionld/pernot/pernotLoop.h" // pernotLoopStart
#include "orionld/pernot/pernotRelease.h" // pernotRelease

#include "orionld/version.h"
#include "orionld/orionRestServices.h"
Expand Down Expand Up @@ -630,6 +632,14 @@ void exitFunc(void)
pgConnectionPoolsFree();
}

// Cleanup entity maps
if (entityMaps != NULL)
entityMapsRelease();

// Cleanup periodic notifications
if (pernot == true)
pernotRelease();

kaBufferReset(&kalloc, KFALSE);
}

Expand Down
18 changes: 12 additions & 6 deletions src/lib/orionld/mongoc/mongocGeoIndexInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,15 @@ bool mongocGeoIndexInit(void)
"}");

unwind = BCON_NEW("$unwind", BCON_UTF8("$attributeKeys"));

group = BCON_NEW("$group", "{",
"_id", BCON_UTF8("$attributeKeys.k"),
"}");
group = BCON_NEW("$group", "{", "_id", BCON_UTF8("$attributeKeys.k"), "}");

bson_append_document(objectArray, "0", 1, project);
bson_append_document(objectArray, "1", 1, unwind);
bson_append_document(objectArray, "2", 1, group);

// Append the array to the document
// Append the array to the pipeline
bson_append_array(pipeline, "pipeline", 8, objectArray);

// Print the BSON document as a JSON string
if (lmTraceIsSet(LmtMongoc))
{
char* str = bson_as_relaxed_extended_json(pipeline, NULL);
Expand Down Expand Up @@ -176,11 +172,21 @@ bool mongocGeoIndexInit(void)
LM_T(LmtMongoc, ("Found geoProperty: '%s'", geoPropertyName));

if (dbGeoIndexLookup(tenantP->tenant, geoPropertyName) == NULL)
{
mongocGeoIndexCreate(tenantP, geoPropertyName);
LM_T(LmtMongoc, ("Creating index for property '%s'", geoPropertyName));
}
}

mongoc_cursor_destroy(mongoCursorP);
mongoc_collection_destroy(mCollectionP);

bson_destroy(objectArray);
bson_destroy(group);
bson_destroy(unwind);
bson_destroy(project);
bson_destroy(pipeline);

tenantP = tenantP->next;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/mongoc/mongocRegistrationsIter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int mongocRegistrationsIter(RegCache* rcP, RegCacheIterFunc callback)
{
char* json = bson_as_relaxed_extended_json(mongoDocP, NULL);
LM_T(LmtMongoc, ("Found a registration in the DB: '%s'", json));
bson_free(json);

KjNode* dbRegP = mongocKjTreeFromBson(mongoDocP, &title, &details);
if (dbRegP == NULL)
Expand Down
3 changes: 2 additions & 1 deletion src/lib/orionld/pernot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
SET (SOURCES
pernotSubCacheInit.cpp
pernotSubCacheAdd.cpp
pernotSubCacheRemove.cpp
pernotItemRelease.cpp
pernotRelease.cpp
pernotSubCacheLookup.cpp
pernotLoop.cpp
pernotTreat.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@
*
* Author: Ken Zangelin
*/
#include "common/RenderFormat.h" // RenderFormat
extern "C"
{
#include "kjson/kjFree.h" // kjFree
}

#include "orionld/q/QNode.h" // QNode
#include "orionld/context/OrionldContext.h" // OrionldContext
#include "orionld/pernot/PernotSubCache.h" // Own interface
#include "orionld/pernot/PernotSubscription.h" // PernotSubscription
#include "orionld/pernot/pernotItemRelease.h" // Own interface



// -----------------------------------------------------------------------------
//
// pernotSubCacheRemove -
// pernotItemRelease -
//
bool pernotSubCacheRemove(PernotSubscription* pSubP)
bool pernotItemRelease(PernotSubscription* pSubP)
{
if (pSubP->subscriptionId != NULL)
free(pSubP->subscriptionId);

if (pSubP->kjSubP != NULL)
kjFree(pSubP->kjSubP);

return true;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_
#define SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_
#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_
#define SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_

/*
*
Expand Down Expand Up @@ -38,8 +38,8 @@

// -----------------------------------------------------------------------------
//
// pernotSubCacheRemove -
// pernotItemRelease -
//
extern bool pernotSubCacheRemove(PernotSubscription* pSubP);
extern bool pernotItemRelease(PernotSubscription* pSubP);

#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTSUBCACHEREMOVE_H_
#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTITEMRELEASE_H_
52 changes: 52 additions & 0 deletions src/lib/orionld/pernot/pernotRelease.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* Copyright 2023 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
extern "C"
{
#include "kjson/kjFree.h" // kjFree
}

#include "orionld/common/orionldState.h" // pernotSubCache
#include "orionld/pernot/PernotSubscription.h" // PernotSubscription
#include "orionld/pernot/pernotItemRelease.h" // pernotItemRelease
#include "orionld/pernot/pernotRelease.h" // Own interface



// -----------------------------------------------------------------------------
//
// pernotRelease -
//
void pernotRelease(void)
{
LM_T(LmtPernot, ("Releasing all pernot subscriptions"));

PernotSubscription* psP = pernotSubCache.head;
while (psP != NULL)
{
LM_T(LmtPernot, ("Releasing pernot subscription %s (at %p)", psP->subscriptionId, psP));
pernotItemRelease(psP);
psP = psP->next;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_
#define SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_

/*
*
* Copyright 2023 FIWARE Foundation e.V.
Expand All @@ -22,16 +25,13 @@
*
* Author: Ken Zangelin
*/
#include "orionld/pernot/PernotSubscription.h" // PernotSubscription
#include "orionld/pernot/pernotSubCacheRemove.h" // Own interface



// -----------------------------------------------------------------------------
//
// pernotSubCacheRemove -
// pernotRelease -
//
bool pernotSubCacheRemove(PernotSubscription* pSubP)
{
return true;
}
extern void pernotRelease(void);

#endif // SRC_LIB_ORIONLD_PERNOT_PERNOTRELEASE_H_
4 changes: 3 additions & 1 deletion src/lib/orionld/pernot/pernotSubCacheAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ PernotSubscription* pernotSubCacheAdd
PernotSubscription* pSubP = (PernotSubscription*) malloc(sizeof(PernotSubscription));
bzero(pSubP, sizeof(PernotSubscription));

LM_T(LmtPernot, ("Creating pernot subscription %s (at %p)", subscriptionId, pSubP));

if (subscriptionId == NULL)
{
KjNode* idP = kjLookup(apiSubP, "id");
Expand All @@ -157,7 +159,7 @@ PernotSubscription* pernotSubCacheAdd
subscriptionId = idP->value.s;
}

LM_T(LmtPernot, ("Adding pernot subscription '%s' to cache", subscriptionId));
LM_T(LmtPernot, ("Adding pernot subscription '%s' to cache (top level name: '%s')", subscriptionId, apiSubP->name));

pSubP->subscriptionId = strdup(subscriptionId);
pSubP->timeInterval = timeInterval;
Expand Down
11 changes: 7 additions & 4 deletions src/lib/orionld/q/qLex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ static QNode* qTermPush(QNode* prev, char* term, bool* lastTermIsTimestampP, cha

LM_T(LmtQ, ("term: '%s' (termLen: %d)", term, termLen));

if (strcmp(&term[termLen - 10], "modifiedAt") == 0) *lastTermIsTimestampP = true;
else if (strcmp(&term[termLen - 9], "createdAt") == 0) *lastTermIsTimestampP = true;
else if (strcmp(&term[termLen - 10], "observedAt") == 0) *lastTermIsTimestampP = true;
else *lastTermIsTimestampP = false;
*lastTermIsTimestampP = false;
if (termLen >= 9)
{
if (strcmp(&term[termLen - 10], "modifiedAt") == 0) *lastTermIsTimestampP = true;
else if (strcmp(&term[termLen - 9], "createdAt") == 0) *lastTermIsTimestampP = true;
else if (strcmp(&term[termLen - 10], "observedAt") == 0) *lastTermIsTimestampP = true;
}

if (*lastTermIsTimestampP == true)
LM_T(LmtQ, ("Pushing a Timestamp term: '%s'", term));
Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/serviceRoutines/orionldPostSubscriptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C"
#include "orionld/pernot/PernotSubscription.h" // PernotSubscription
#include "orionld/pernot/PernotSubCache.h" // PernotSubCache
#include "orionld/pernot/pernotSubCacheAdd.h" // pernotSubCacheAdd
#include "orionld/pernot/pernotSubCacheRemove.h" // pernotSubCacheRemove
#include "orionld/pernot/pernotItemRelease.h" // pernotItemRelease
#include "orionld/pernot/pernotSubCacheLookup.h" // pernotSubCacheLookup
#include "orionld/mqtt/mqttParse.h" // mqttParse
#include "orionld/mqtt/mqttConnectionEstablish.h" // mqttConnectionEstablish
Expand Down Expand Up @@ -317,7 +317,7 @@ bool orionldPostSubscriptions(void)
if (cSubP != NULL)
subCacheItemRemove(cSubP);
else
pernotSubCacheRemove(pSubP);
pernotItemRelease(pSubP);

if (qTree != NULL)
qRelease(qTree);
Expand Down
11 changes: 4 additions & 7 deletions src/lib/rest/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ extern "C"
#include "orionld/rest/orionldMhdConnectionInit.h" // orionldMhdConnectionInit
#include "orionld/rest/orionldMhdConnectionPayloadRead.h" // orionldMhdConnectionPayloadRead
#include "orionld/rest/orionldMhdConnectionTreat.h" // orionldMhdConnectionTreat
#include "orionld/forwarding/distOpListRelease.h" // distOpListRelease

#include "rest/HttpHeaders.h" // HTTP_* defines
#include "rest/Verb.h"
Expand Down Expand Up @@ -427,15 +428,11 @@ static void requestCompleted
free(orionldState.curlHeadersV);
}

//
// FIXME:
// Would be nice to do this here instead of in every service routine
// Just, I don't have the pointer (distOpList) in orionldState.
// At least I can do the call to curl_multi_cleanup
//
if (orionldState.distOpList != NULL)
distOpListRelease(orionldState.distOpList);

if (orionldState.curlDoMultiP != NULL)
{
// distOpListRelease(orionldState.distOpList);
curl_multi_cleanup(orionldState.curlDoMultiP);
orionldState.curlDoMultiP = NULL;
}
Expand Down
Loading