From b9a3185e4556e8c5785a60fb57c2924bbd0529f7 Mon Sep 17 00:00:00 2001
From: Kumar Kaushik <kaushikk@vmware.com>
Date: Wed, 29 Nov 2017 18:47:39 -0800
Subject: [PATCH] Avoiding previous errno variable use

Change-Id: I76b0ef967df8b1ad6fa1844e1e461af470bb3b0f
---
 transport/posix/socket.c | 51 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/transport/posix/socket.c b/transport/posix/socket.c
index 79c7a38..d3a3dbc 100644
--- a/transport/posix/socket.c
+++ b/transport/posix/socket.c
@@ -853,6 +853,8 @@ VmSockPosixRead(
     do
     {
         nRead = 0;
+        errno = 0;
+        errorCode = 0;
         if (pRESTHandle->pSSLInfo->isSecure && (pSocket->ssl != NULL))
         {
             nRead = SSL_read(pSocket->ssl, (pszBufPrev + nPrevBuf), MAX_DATA_BUFFER_LEN);
@@ -864,7 +866,7 @@ VmSockPosixRead(
             errorCode = errno;
         }
 
-        if ((nRead > 0) && (nRead <= MAX_DATA_BUFFER_LEN))
+        if (nRead > 0)
         {
             nPrevBuf += nRead;
             dwError = VmRESTReallocateMemory(
@@ -877,22 +879,6 @@ VmSockPosixRead(
         }
     }while((nRead > 0) && (nPrevBuf < pRESTHandle->pRESTConfig->maxDataPerConnMB));
 
-    if (((pSocket->fd > 0) && (errorCode == EAGAIN || errorCode == EWOULDBLOCK)) || ((pRESTHandle->pSSLInfo->isSecure) && (errorCode == SSL_ERROR_WANT_READ)))
-    {
-        dwError = REST_ENGINE_SUCCESS;
-    }
-    else if (nRead < 0)
-    {
-        VMREST_LOG_ERROR(pRESTHandle, "Socket read failed with error code %u", errorCode);
-        dwError = errorCode;
-    }
-    else if (nRead == 0)
-    {
-        VMREST_LOG_ERROR(pRESTHandle,"%s", "Socket read failed due to broken pipe");
-        dwError = VM_SOCK_POSIX_ERROR_BROKEN_PIPE;
-    }
-    BAIL_ON_VMREST_ERROR(dwError);
-
     if (nPrevBuf >= pRESTHandle->pRESTConfig->maxDataPerConnMB)
     {
         /**** Discard the request here itself. This might be the first read IO cycle ****/
@@ -901,6 +887,33 @@ VmSockPosixRead(
     }
     BAIL_ON_VMREST_ERROR(dwError);
 
+    if (nRead == -1)
+    {
+        if (((pSocket->fd > 0) && (errorCode == EAGAIN || errorCode == EWOULDBLOCK)) || ((pRESTHandle->pSSLInfo->isSecure) && (errorCode == SSL_ERROR_WANT_READ)))
+        {
+            dwError = REST_ENGINE_SUCCESS;
+        }
+        else
+        {
+            VMREST_LOG_ERROR(pRESTHandle,"%s","Unknown socket read error: errno %u, errorCode %u, nRead %d", errno, errorCode, nRead);
+            dwError = REST_ENGINE_FAILURE;
+        }
+    }
+    else
+    {
+        if (nRead == 0)
+        {
+            VMREST_LOG_ERROR(pRESTHandle,"%s","Socket Read Failed: Remote has closed the connection");
+            dwError = VM_SOCK_POSIX_ERROR_BROKEN_PIPE;
+        }
+        else
+        {
+            VMREST_LOG_ERROR(pRESTHandle, "Socket read failed with error code %u", errorCode);
+            dwError = errorCode;
+        }
+    }
+    BAIL_ON_VMREST_ERROR(dwError);
+
     pSocket->pszBuffer = pszBufPrev;
     pSocket->nProcessed = 0;
     pSocket->nBufData = nPrevBuf;
@@ -933,11 +946,12 @@ VmSockPosixRead(
     if (pszBufPrev && pSocket && pRESTHandle->pSockContext)
     {
         /**** Delete the socket from poller ****/
+        
         VmSockPosixEventQueueDelete_inlock(
             pRESTHandle->pSockContext->pEventQueue,
             pSocket
             );
-
+        
         VmRESTFreeMemory(pszBufPrev);
         pszBufPrev = NULL;
         pSocket->pszBuffer = NULL;
@@ -1566,6 +1580,7 @@ VmSockPosixSetRequestHandle(
                       pSocket
                       );
         BAIL_ON_VMREST_ERROR(dwError);
+       
 
         if (pSocket->pTimerSocket->fd > 0)
         {