:: Re: [DNG] netman-gtk3: invalid poin…
Góra strony
Delete this message
Reply to this message
Autor: Irrwahn
Data:  
Dla: dng
Temat: Re: [DNG] netman-gtk3: invalid pointer error
On Wed, 06 Jan 2016 20:05:12 +0100, Aitor Czr wrote:
> On 01/06/2016 07:59 PM, Edward Bartolo <edbarx@???> wrote:
>> Hi,
>>
>> I can't free the memory of some pointers ( *cad2 and *res) in
>> netman-gtk3. This is what i get:
>>
>> ~$ gcc main.c -o main
>>
>> ~$ ./main
>> *** Error in `./main': munmap_chunk(): invalid pointer:
>> 0x0000000001bfdab4 ***
>> Aborted


[...]

Note: I didn't try to undertand what the code is intended
to do, just commenting on some obvious flaws.

> void scan_buffer (char **ptr, char *command, char *name, char *str1, char *str2)
> {
> FILE *fp;
> char *cad1, *cad2, *res;
>
> cad1=(char*)malloc(1024*sizeof(char));


Better C style would've been:

    cad1=malloc(1024);


Casting malloc()'s return value is nonsense and potentially dangerous,
sizeof(char) is always guaranteed to equal 1.

[...]

>          cad2 = strstr((char *) cad1, name); 
>          res  = strstr((char *) cad2, str1);



Now you assigned to cad2 and res and lost your only reference
to your meticulously allocated buffers! (More bogus casts, BTW.)

[...]

And then you tried to free something that was not a pointer value
returned by any of the *alloc() functions:

> // free(cad2);
> // free(res);


[...]

As I said, I didn't try to guess the intention - if you want to
work in-place an the string or if you're actually looking for strcpy().

HTH, regards
Irrwahn