Skip to content

Commit

Permalink
Resource Leak
Browse files Browse the repository at this point in the history
  • Loading branch information
hastigondaliya committed Oct 17, 2023
1 parent 17e433c commit fe0dcac
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/posix/platform/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ otError PlatformSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex,

assert(swapFd != -1);
assert(offset == 0);
VerifyOrExit(offset == 0 && size >= 0, error = OT_ERROR_PARSE);
VerifyOrExit(offset == 0 && size >= 0, error = OT_ERROR_FAILED);

while (offset < size)
{
Expand All @@ -445,25 +445,25 @@ otError PlatformSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex,
ssize_t rval;

rval = read(sSettingsFd, &key, sizeof(key));
VerifyOrExit(rval == sizeof(key), error = OT_ERROR_PARSE);
VerifyOrExit(rval == sizeof(key), error = OT_ERROR_FAILED);

rval = read(sSettingsFd, &length, sizeof(length));
VerifyOrExit(rval == sizeof(length), error = OT_ERROR_PARSE);
VerifyOrExit(rval == sizeof(length), error = OT_ERROR_FAILED);

offset += sizeof(key) + sizeof(length) + length;

if (aKey == key)
{
if (aIndex == 0)
{
VerifyOrExit(offset == lseek(sSettingsFd, length, SEEK_CUR), error = OT_ERROR_PARSE);
VerifyOrExit(offset == lseek(sSettingsFd, length, SEEK_CUR), error = OT_ERROR_FAILED);
swapWrite(aInstance, swapFd, static_cast<uint16_t>(size - offset));
error = OT_ERROR_NONE;
break;
}
else if (aIndex == -1)
{
VerifyOrExit(offset == lseek(sSettingsFd, length, SEEK_CUR), error = OT_ERROR_PARSE);
VerifyOrExit(offset == lseek(sSettingsFd, length, SEEK_CUR), error = OT_ERROR_FAILED);
error = OT_ERROR_NONE;
continue;
}
Expand All @@ -474,19 +474,15 @@ 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_ERROR_FAILED);

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

swapWrite(aInstance, swapFd, length);
}

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

if (aSwapFd != nullptr)
{
*aSwapFd = swapFd;
Expand All @@ -495,9 +491,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_ERROR_FAILED)
{
swapDiscard(aInstance, swapFd);
DieNow(error);
}

return error;
Expand Down

0 comments on commit fe0dcac

Please sign in to comment.