vendredi 18 août 2023

Klockwork Error: RH.LEAK (2:Error) Analyze Resource acquired to 'socFd' at line 385 may be lost here

When we are trying to generate the Klockwork report it is showing

FrameTxRxMngr::sendStrmReqInfoToTarget(
    void
) {

    uint32_t           retVal    = IPNEXT_SUCCESS;
    uint32_t           timeout   = TIMEOUT_IN_MS;
    stFrameHeader      frameHdr{};
    struct sockaddr_in destAddr{};

    SOCKET socFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (socFd == INVALID_SOCKET)
    {
        printf("sendStrmReqInfoToTarget() Socket creation failed");
        retVal = IPNEXT_ERR_SCKT_CRT_FAIL;
    }
    else
    {
        struct sockaddr_in localAddr;

        localAddr.sin_family = AF_INET;

        // ! Copy the IP address to localAddr.sin_addr
        memcpy(&(localAddr.sin_addr.s_addr),
                &m_pIpCfginfo->x86SrcIPAddr,
                sizeof(m_pIpCfginfo->x86SrcIPAddr));

        localAddr.sin_port = htons(PORT_NUM);

        //! attempt to bind the socket to specific IP address
        int32_t bindResult = bind(socFd,
                                (SOCKADDR *)&localAddr,
                                sizeof(localAddr));

        if (bindResult == SOCKET_ERROR)
        {
            printf("ERROR: bind failed with %d for sendStrmReqInfoToTarget\n",
                    WSAGetLastError());
            retVal = IPNEXT_ERR_UDP_BIND_FAIL;
        }
        else
        {
            //! set the address family to IPv4
            destAddr.sin_family = AF_INET;

            // ! Copy the IP address to m_destAddr.sin_addr
            memcpy(&(destAddr.sin_addr.s_addr),
                    &m_pIpCfginfo->c10DestIPAddr,
                    sizeof(m_pIpCfginfo->c10DestIPAddr));

            destAddr.sin_port = htons(PORT_NUM);
        }

        if (retVal == IPNEXT_SUCCESS)
        {
            //! Set the timeout value for the socket connection, if it fails
            int32_t sockoptResult = setsockopt(socFd,
                                            SOL_SOCKET,
                                            SO_RCVTIMEO,
                                            (const CHAR *)&timeout,
                                            sizeof(int32_t));

            if (sockoptResult == SOCKET_ERROR)
            {
                printf("sendStrmReqInfoToTarget setsockopt for SO_RCVTIMEO Failed");
                retVal = IPNEXT_ERR_UDP_RECV_TIMEOUT;
            }
        }
    }

    //! Send the num of stream to process to target
    if (retVal == IPNEXT_SUCCESS)
    {
        int32_t bytesSent = sendto(socFd,
                                   (CHAR *)m_pTxRxConfig->pActStrmToProcess,
                                   sizeof(stActiveStreamFiles),
                                   0,
                                   (sockaddr*)&destAddr,
                                   sizeof(destAddr));

        if (bytesSent == SOCKET_ERROR)
        {
            printf("sendStrmReqInfoToTarget sendto failed with ErrCode: %d\n",
                    WSAGetLastError());
            retVal = IPNEXT_ERR_UDP_SENDTO;
        }
    }

    if (retVal == IPNEXT_SUCCESS)
    {
        //! Receive data using the socket connection and store it
        int32_t bytesRecv = recvfrom(socFd,
                                    (CHAR *)&frameHdr,
                                    sizeof(stFrameHeader),
                                    0,
                                    NULL,
                                    NULL);

        if (bytesRecv == SOCKET_ERROR)
        {
            //! Check if the error is due to timeout
            if (WSAGetLastError() == WSAETIMEDOUT)
            {
                printf("sendStrmReqInfoToTarget recvfrom Timeout!! Didn't recv all the data");
                retVal = IPNEXT_ERR_UDP_RECV_TIMEOUT;
            }
            else
            {
                printf("sendStrmReqInfoToTarget recvfrom failed ErrCode: %d",
                        WSAGetLastError());
                retVal = IPNEXT_ERR_UDP_RECVFROM;
            }
        }
    }
  
    if (retVal == IPNEXT_SUCCESS)
    {
        //! Incase negative ack received
        if (!frameHdr.isFrmAckSuccess)
        {
            retVal = IPNEXT_ERR_NEG_ACK;
        }
    }
   closesocket(socFd);
    return retVal;
    
}
# Error: frametxrxmngr.cpp:500 RH.LEAK (2:Error) Analyze
Resource acquired to 'socFd' at line 385 may be lost here.
  * frametxrxmngr.cpp:385: Resource is acquired: 'socFd' in the call to 'socket'
  * frametxrxmngr.cpp:500: Resource is lost: 'socFd'
Current status 'Analyze'

Even after removing the closesocket(socFd); getting the same error.

Aucun commentaire:

Enregistrer un commentaire