From 8137f18214d62ff1de935a427cf7583e6d203784 Mon Sep 17 00:00:00 2001 From: Hasti M Gondaliya Date: Tue, 3 Oct 2023 15:45:20 +0530 Subject: [PATCH] [posix] Addressing coverity warning: Resource leak 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 | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/posix/platform/settings.cpp b/src/posix/platform/settings.cpp index 73925fe21500..499a68024e8b 100644 --- a/src/posix/platform/settings.cpp +++ b/src/posix/platform/settings.cpp @@ -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) { @@ -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;