"SIGSEGV" error when receiving a large data packet

2

Hello,

I'm experiencing a "SIGSEGV" error when receiving a large data packet with the recv function of the library using C language on a Posix-UNIX system, if any soul can help me one thank you.

buffer:

char ls_buffer_PPouAUT[2048] =  {0};

rcv:

    ln_retorno_receive          =   recv
                                    (
                                        in_socket_handler,
                                        ls_buffer_PPouAUT,
                                        sizeof(ls_buffer_PPouAUT),
                                        0
                                    );

    ls_buffer_PPouAUT[ln_retorno_receive]           =   0x00;

Socket:

int     SocketConnect
    (
        char*           as_host,
        int             an_port
    )
{

int
ln_connection_status            =   9;

//
// Variavel que guarda o retorno
//
GEDI_e_Ret      ret;

//
// Cria o Socket
//
// AF_INET      =   ARPA INTERNET PROTOCOLS
// SOCK_STREAM  =   orientado a conexao
// 0            =   protocolo padrao para o tipo escolhido -- TCP
in_socket_handler           =   socket  (AF_INET, SOCK_STREAM, 0);

//
// Informa para conectar no server
//
// IP do servidor
server.sin_family           =   AF_INET;
// familia ARPANET
server.sin_addr.s_addr      =   inet_addr(  as_host );
// Porta - hton = host to network short (2bytes) ou htons para mais
server.sin_port             =   htons ( an_port );

//
// Limpa varivavel
//
memset  (
            &(server.sin_zero),
            0x00,
            sizeof (server.sin_zero)
        );

//
// Inicia comunicacao com server
//
if (
        connect (
                    in_socket_handler,
                    (struct sockaddr *) &server,
                    sizeof (server)
                )
        < 0
    )
{
    //
    // Se ocoreu uma falha
    //
    GEDI_LCD_DrawString(10, FONT_HEIGHT*10, FONT_WIDTH*1, FONT_HEIGHT*1,
            " Falha ao criar socket!                           ");
    GEDI_CLOCK_Delay(1000);

    ln_connection_status            =   9;
}

else
{
    //
    //  Se conectou com sucesso
    //
    ln_connection_status            =   0;

}

//
// Retorna o status da conexao.
//
return  ln_connection_status;
}

Thank you. Lucas

    
asked by anonymous 21.07.2016 / 14:17

1 answer

1

recv() can return -1 in case of error.

In this case, you will try to access ls_buffer_PPouAUT[-1] that causes the "SIGSEGV" error.

Checks if recv () went well:

   ln_retorno_receive = recv(in_socket_handler, ls_buffer_PPouAUT, sizeof ls_buffer_PPouAUT, 0);
   // validacao de erros
   if (ln_retorno_receive < 0) {
       perror("recv");
       exit(EXIT_FAILURE);
   }
   ls_buffer_PPouAUT[ln_retorno_receive] = 0;
    
21.07.2016 / 15:33