0
Hello,
I am facing an error "SIGSEGV":
strcpy(buffer_ReceiveAutomation, ls_buffer_PPouAUT);
<< ERROR
and
buffer is incomplete from recv,
but if I change the sizeof(ls_buffer_PPouAUT)
by the amount of characters works, why?
I am using C language in a Posix system, if anyone can help me I appreciate.
Statements:
char
*ls_buffer_PPouAUT,
*buffer_ReceivePinPad,
*buffer_ReceiveAutomation;
Initiations:
//
// se ha memoria alocada
//
if(ls_buffer_PPouAUT != NULL)
{
//
// Libera memoria alocada
//
free(ls_buffer_PPouAUT);
ls_buffer_PPouAUT = NULL;
}
//
// Inicializa o buffer que recebera com pacote
//
ls_buffer_PPouAUT = malloc(5120*sizeof(char));
if(ls_buffer_PPouAUT == NULL)
{
//
// Exibe a mensagem
//
GEDI_LCD_DrawString(CENTER, APFONT_HEIGHT*20, APFONT_WIDTH, APFONT_HEIGHT, "Erro realocar memoria!");
GEDI_CLOCK_Delay(1000);
//
// Fecha Thread Recive
//
pthread_detach(pthread_self());
pthread_exit(NULL);
}
//ls_buffer_PPouAUT = "TESTE";
//
// Recebe pacote
//
ln_retorno_receive = recv
(
in_socket_handler,
ls_buffer_PPouAUT,
sizeof(ls_buffer_PPouAUT),
0
);
//
// Validacao de erros
//
if(ln_retorno_receive < 0)
{
//
// Exibe a mensagem
//
GEDI_LCD_DrawString(CENTER, APFONT_HEIGHT*20, APFONT_WIDTH, APFONT_HEIGHT, "Erro ao receber!");
GEDI_CLOCK_Delay(1000);
//
// Libera memoria alocada
//
free(ls_buffer_PPouAUT);
ls_buffer_PPouAUT = NULL;
//
// Para a transacao
//
in_controle_execucao = 9;
perror("recv");
//
// Fecha Thread Recive
//
pthread_detach(pthread_self());
pthread_exit(NULL);
}
//
// Seta o fim do pacote
//
ls_buffer_PPouAUT[ln_retorno_receive] = '\0';
GEDI_LCD_DrawString(5, APFONT_HEIGHT*18, APFONT_WIDTH*0.65, APFONT_HEIGHT*0.65,"P1 >%s< ", ls_buffer_PPouAUT);
GEDI_CLOCK_Delay(1000);
Print above shows only the first 4 characters that should be received
//
// Se ha memoria alocada
//
if(buffer_ReceiveAutomation != NULL)
{
//
// Libera memoria alocada
//
free(buffer_ReceiveAutomation);
buffer_ReceiveAutomation = NULL;
}
//
// Inicializa o buffer que recebera com pacote
//
buffer_ReceiveAutomation = malloc(5120*sizeof(char));
if(buffer_ReceiveAutomation == NULL)
{
//
// Exibe a mensagem
//
GEDI_LCD_DrawString(CENTER, APFONT_HEIGHT*20, APFONT_WIDTH, APFONT_HEIGHT, "AUT - Erro alocar memoria!");
GEDI_CLOCK_Delay(1000);
//
// Fecha Thread Recive
//
pthread_detach(pthread_self());
pthread_exit(NULL);
}
//
// Seta buffer Local
//
strcpy(buffer_ReceiveAutomation, ls_buffer_PPouAUT);
Yes, I think that might be so, because debugging remotely because this code is being run on an external machine with a very rudimentary system, I will do a test just running to see if I pass this strcpy and, thanks, I will adopt the use of strncpy
– Lucas Fernandes
It worked, I switched to
strncpy
, but I have another problem when it includesrecv
, he is keeping only the first 4 characters he would have received– Lucas Fernandes
I’m sorry, I don’t understand the other problem. What is "recv included"? When you say that "you are keeping only the first 4 characters," do you mean keeping where? Vc is sure to have received more than 4 characters (i.e., what is the size of the variable
ln_retorno_receive
?). Anyway, this site is not a forum. If you have another problem, open another question (and take advantage of the questions I have commented to make your question clear).– Luiz Vieira