Skip to content

Commit

Permalink
Merge pull request #1376 from FIWARE/hardening/valgrind
Browse files Browse the repository at this point in the history
Hardening/valgrind
  • Loading branch information
kzangeli authored May 17, 2023
2 parents 31f312c + d97ac81 commit 2c92489
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/lib/cache/subCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
6 changes: 5 additions & 1 deletion src/lib/orionld/notifications/orionldAlterationsTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ bool notificationResponseRead

// Step over all \r | \n and assign the bodyP
++headerBodyDelimiterP;

while ((*headerBodyDelimiterP == '\r') || (*headerBodyDelimiterP == '\n'))
{
++headerBodyDelimiterP;
}

body = headerBodyDelimiterP;

Expand Down Expand Up @@ -274,7 +277,6 @@ bool notificationResponseRead
ssize_t headersLen = (ssize_t) headerBodyDelimiterP - (ssize_t) buf; // Including the Start-Line
ssize_t bodyBytesRead = bytesRead - headersLen;

LM_T(LmtNotificationMsg, ("%s: total no of bytes of Start-Line + Headers: %d", npP->subP->subscriptionId, headersLen));
LM_T(LmtNotificationMsg, ("%s: total no of bytes read: %d", npP->subP->subscriptionId, bytesRead));
LM_T(LmtNotificationMsg, ("%s: no of bytes of body read: %d", npP->subP->subscriptionId, bodyBytesRead));

Expand Down Expand Up @@ -354,6 +356,8 @@ static void notificationResponseTreat(NotificationPending* npP, double notificat
char* headers = NULL;
char* subId = npP->subP->subscriptionId;

bzero(buf, sizeof(buf));

if (notificationResponseRead(npP, buf, sizeof(buf), &httpStatusCode, &contentLength, &headers, &body, notificationTime) == false)
{
// if notificationResponseRead() returns false, it has invoked notificationFailure()
Expand Down
2 changes: 0 additions & 2 deletions src/lib/orionld/q/qBuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 37 additions & 2 deletions src/lib/orionld/serviceRoutines/orionldPatchSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -457,10 +467,19 @@ static bool subCacheItemUpdateNotificationEndpoint(CachedSubscription* cSubP, Kj
cSubP->url = url;
cSubP->httpInfo.url = url;

if (cSubP->protocolString != NULL)
free(cSubP->protocolString);
cSubP->protocolString = strdup(protocol);
cSubP->ip = strdup(ip);

if (cSubP->ip != NULL)
free(cSubP->ip);
cSubP->ip = strdup(ip);

if (cSubP->rest != NULL)
free(cSubP->rest);
cSubP->rest = strdup(rest);

cSubP->port = port;
cSubP->rest = strdup(rest);
cSubP->protocol = protocolFromString(cSubP->protocolString);
}
else
Expand Down Expand Up @@ -579,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)
{
Expand Down Expand Up @@ -800,6 +823,8 @@ bool orionldPatchSubscription(void)
&mqttChange);
if (r == false)
{
if (qNodeP != NULL)
qRelease(qNodeP);
LM_E(("pCheckSubscription FAILED"));
return false;
}
Expand All @@ -811,6 +836,8 @@ bool orionldPatchSubscription(void)

if (dbSubscriptionP == NULL)
{
if (qNodeP != NULL)
qRelease(qNodeP);
orionldError(OrionldResourceNotFound, "Subscription not found", subscriptionId, 404);
return false;
}
Expand Down Expand Up @@ -866,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
Expand All @@ -889,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;
}
Expand All @@ -906,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;
}
Expand Down

0 comments on commit 2c92489

Please sign in to comment.