"SIGSEGV" error when receiving a large data packet

Asked

Viewed 46 times

2

Hello,

I am facing 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 a 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

  • Hello. Welcome to SOPT. If you haven’t done it yet, do the [tour] and read [Ask]. When you say "when receiving a big package", where exactly does the error occur? Have you debugged and retrieved the line of code where the error occurs? Have you checked the buffer (ls_buffer_PPouAUT - by the way, you did not put the code where it is allocated) managed to be allocated to the amount of bytes received? Without more details it’s hard to help you.

  • Sorry, I’m new here

  • 1

    I’ll edit it, thanks for the tip.

  • Not at all. I withdrew my vote to close then. :)

1 answer

1


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

In that case, you’ll try to access ls_buffer_PPouAUT[-1] originating the error "SIGSEGV".

Check 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;
  • +1 Exact. Another possible thing is the recv receive more than 2047 bytes (AP says the error occurs when it receives a large packet of data) and then invade beyond the allocated memory. Perhaps the ideal is to allocate memory dynamically or (better yet) adjust the pre-allocated size if the received size exceeds it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.