Segmentation fault at malloc.c:4232

Jakub Jelinek jakub at redhat.com
Fri Feb 29 23:07:22 UTC 2008


On Fri, Feb 29, 2008 at 11:57:53PM +0100, Michael Schwendt wrote:
> Your theory is false, your code is broken. You really need to free()
> the line buffer when it is allocated by getline(). But you call getline()
> in a while-loop without resetting the line pointer to NULL.

True.

> Here's the fix:

But this is not how getline is meant to be used.  This way each getline call
will allocate a new buffer.  getline is designed so that you can use it in a
loop with just one allocated buffer, which is realloced by getline when
needed.  So the right fix would be actually to move the free(line);
statement after the loop.  Of course if you e.g. want to preserve the buffer
with the line data, stick pointer to it into some data structure, then you'd
just do that and clear the pointer so that next getline iteration will
allocate a fresh buffer.

> --- compiler.c~ 2008-02-29 07:06:46.000000000 +0100
> +++ compiler.c  2008-02-29 23:55:41.000000000 +0100
> @@ -25,6 +25,7 @@
>         }
>  
>         free(line);
> +    line = NULL;
>         STACKfree();
>      }
>  

	Jakub




More information about the fedora-list mailing list