From fbeb4d18f2d31c585fdd360c75a3369fa235adac Mon Sep 17 00:00:00 2001 From: hastigondaliya <118960681+hastigondaliya@users.noreply.github.com> Date: Wed, 25 Oct 2023 03:08:30 +0530 Subject: [PATCH] [posix] addressing coverity warning: resource leak (#9472) 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 --- src/posix/platform/settings.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/posix/platform/settings.cpp b/src/posix/platform/settings.cpp index 73925fe21..f6139402a 100644 --- a/src/posix/platform/settings.cpp +++ b/src/posix/platform/settings.cpp @@ -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) { @@ -445,10 +445,10 @@ 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; @@ -456,14 +456,14 @@ otError PlatformSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex, { 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(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; } @@ -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; @@ -499,6 +495,11 @@ exit: { swapDiscard(aInstance, swapFd); } + else if (error == OT_ERROR_FAILED) + { + swapDiscard(aInstance, swapFd); + DieNow(error); + } return error; }