Skip to content

Commit

Permalink
[posix] Addressing coverity warning: Resource leak
Browse files Browse the repository at this point in the history
The "assert" and "VerifyOrDie" were checking same condition, so
the second validation was never executed if (rval != sizeof(key)).
Additionally,"aSwapFd" was not freed on assertion.

With this commit,
1. addressed the memory leak to system resources.
2. Replaced VerifyOrDie() or assert() by VerifyOrExit()
3. handle error at exit
  • Loading branch information
hastigondaliya committed Oct 3, 2023
1 parent c6eaeda commit 8137f18
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/posix/platform/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,18 +474,16 @@ otError PlatformSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex,
}

rval = write(swapFd, &key, sizeof(key));
assert(rval == sizeof(key));
VerifyOrDie(rval == sizeof(key), OT_EXIT_FAILURE);
VerifyOrExit(rval == sizeof(key), error = OT_EXIT_FAILURE);

rval = write(swapFd, &length, sizeof(length));
assert(rval == sizeof(length));
VerifyOrDie(rval == sizeof(length), OT_EXIT_FAILURE);
VerifyOrExit(rval == sizeof(length), error = OT_EXIT_FAILURE);

swapWrite(aInstance, swapFd, length);
}

exit:
VerifyOrDie(error != OT_ERROR_PARSE, OT_EXIT_FAILURE);
VerifyOrExit(error != OT_ERROR_PARSE, error = OT_EXIT_FAILURE);

if (aSwapFd != nullptr)
{
Expand All @@ -495,9 +493,10 @@ otError PlatformSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex,
{
swapPersist(aInstance, swapFd);
}
else if (error == OT_ERROR_NOT_FOUND)
else if ((error == OT_ERROR_NOT_FOUND) || (error == OT_EXIT_FAILURE))
{
swapDiscard(aInstance, swapFd);
DieNow(error);
}

return error;
Expand Down

0 comments on commit 8137f18

Please sign in to comment.