how to solve "SIGSEGV" error when using 'strcpy'

0

Hello,

I'm experiencing a "SIGSEGV" error on the following line:

strcpy(buffer_ReceiveAutomation, ls_buffer_PPouAUT);

asked by anonymous 22.07.2016 / 19:07

1 answer

2
  

Important Note:

     

When you do:

ls_buffer_PPouAUT = "TESTE";
     

You immediately cause a memory leak ( memory leak ).   Previously you had allocated memory using malloc and assigned the   allocation address to that variable, and in that line you simply   "throws this address out" and places in the variable the address allocated by the   compiler for the fixed string "TESTE" . Thus, allocated memory   previously lost and can not be redeemed until your   application is terminated! To avoid this problem, use strcpy here   also!

I was unable to identify why your second call to strcpy is causing an error, since you are copying a smaller memory area (the content "TESTEls_buffer_PPouAUT" in variable buffer_ReceiveAutomation ) to the larger area allocated Any garbage - if you are running in release - in the strcpy variable). If it were to the contrary the explanation would be obvious: the copy invaded the allocated space, because this function will copy bytes until finding the termination of string (the strncpy ).

So good practice says do not use strcpy , but rather ls_buffer_PPouAUT (where you need to enter the maximum size that should be copied).

Anyway, as I said, despite the memory leak there does not seem to be anything necessarily wrong with calling "TESTEmalloc" at that location. But as it involves access to memory, maybe this error is due to some other previous operation where you did some wrong manipulation and the error did not happen immediately. Some compilers, for example, "clean" the memory as it is allocated when you run in debug mode, but do not do this when you run in release mode for performance improvement purposes (that is, let your care take care! ). Then some errors are not so apparent when you run debugging.

  

For example : if your source string ( strcpy ) does not   had been initialized with strncpy and you were running   in release mode, there would be garbage in the area allocated by %code% . The function    %code% will try to copy characters from this source until you find a    %code% , which can take a long time to find (since the area was not   initialized and only has garbage). If it takes more than 5120 bytes   destination, the copy will invade unallocated memory area (it will exceed the maximum limit of the destination),   and then the error will certainly occur.

In addition, invade memory (that is, copy data beyond from the area may not be problematic for certain operating systems if this occurs within the application's usage space (which is generally not the case for * nix systems). And then the "error" can also take time to appear, producing false indications elsewhere.

So you may want to look more closely at your code to look for other leaks and / or intrusions. And even consider using %code% instead of the other. :)

    
22.07.2016 / 19:36