Fix a memory leak in Deserializer
authorSunwook Bae <sunwook45.bae@samsung.com>
Mon, 26 Aug 2013 06:36:34 +0000 (15:36 +0900)
committerSunwook Bae <sunwook45.bae@samsung.com>
Mon, 26 Aug 2013 08:45:05 +0000 (17:45 +0900)
Change-Id: I8745a71a542d5f506c8db5c7a47d86d567c44cf1
Signed-off-by: Sunwook Bae <sunwook45.bae@samsung.com>
src/io/FIo_IpcClient.cpp

index 88c628c..748c557 100644 (file)
@@ -504,6 +504,7 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
        int messageId = 0;
        int fd = -1;
+       int ret = 0;
 
        char* pData = null;
        int remain = 0;
@@ -515,14 +516,20 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        std::string message;
 
        IPC::Message* pReply = null;
-       MessageReplyDeserializer* pReplyDeserializer = null;
        IPC::SyncMessage* pSyncMessage = dynamic_cast <IPC::SyncMessage*>(pMessage);
        SysTryReturnResult(NID_IO, pSyncMessage != null, E_INVALID_ARG, "pMessage is not a sync message.");
 
+       MessageReplyDeserializer* pReplyDeserializer = pSyncMessage->GetReplyDeserializer();
        messageId = SyncMessage::GetMessageId(*pSyncMessage);
 
        fd = AcquireFd();
-       SysTryReturnResult(NID_IO, fd != -1, E_SYSTEM, "Failed to get fd.");
+       if (fd < 0)
+       {
+               SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to get an fd");
+
+               delete pReplyDeserializer;
+               return E_SYSTEM;
+       }
 
        pData = (char*) pSyncMessage->data();
        remain = pSyncMessage->size();
@@ -532,21 +539,14 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                written = write(fd, (char*) pData, remain);
                if (written < 0)
                {
-                       ReleaseFd(fd);
+                       SysTryCatch(NID_IO, errno != EAGAIN, r = E_RESOURCE_UNAVAILABLE, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
 
-                       if (errno == EAGAIN)
-                       {
-                               SysLogException(NID_IO, E_RESOURCE_UNAVAILABLE, "[E_RESOURCE_UNAVAILABLE] The socket buffer is full.");
-                               return E_RESOURCE_UNAVAILABLE;
-                       }
-                       else if (errno == EPIPE)
-                       {
-                               SysLogException(NID_IO, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] The socket connection is closed.");
-                               return E_INVALID_CONNECTION;
-                       }
+                       SysTryCatch(NID_IO, errno != EPIPE, r = E_INVALID_CONNECTION, E_INVALID_CONNECTION, "[E_INVALID_CONNECTION] The socket connection is closed.");
 
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to send a request: %d, %s", errno, strerror(errno));
-                       return E_SYSTEM;
+
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                remain -= written;
@@ -560,8 +560,6 @@ _IpcClient::SendSync(IPC::Message* pMessage)
        pfd.events = POLLIN | POLLRDHUP;
        pfd.revents = 0;
 
-       int ret = 0;
-
        while (true)
        {
                ret = poll(&pfd, 1, -1);
@@ -574,16 +572,16 @@ _IpcClient::SendSync(IPC::Message* pMessage)
 
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to poll (%d, %s).", errno, strerror(errno));
 
-                       ReleaseFd(fd);
-                       return E_SYSTEM;
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                if (pfd.revents & POLLRDHUP)
                {
                        SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] POLLRDHUP");
 
-                       ReleaseFd(fd);
-                       return E_SYSTEM;
+                       r = E_SYSTEM;
+                       goto CATCH;
                }
 
                if (pfd.revents & POLLIN)
@@ -600,22 +598,16 @@ _IpcClient::SendSync(IPC::Message* pMessage)
                if (pEndOfMessage)
                {
                        pReply = new (std::nothrow) IPC::Message(message.data(), pEndOfMessage - message.data());
-                       if (pReply == null)
-                       {
-                               SysLogException(NID_IO, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
-
-                               ReleaseFd(fd);
-                               return E_OUT_OF_MEMORY;
-                       }
+                       SysTryCatch(NID_IO, pReply != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
                        break;
                }
        }
 
-       pReplyDeserializer = pSyncMessage->GetReplyDeserializer();
        pReplyDeserializer->SerializeOutputParameters(*pReply);
-
        delete pReply;
+
+CATCH:
        delete pReplyDeserializer;
 
        ReleaseFd(fd);