[PATCH 1/4] Don't use old pointer after realloc

Dirk Hohndel dirk at hohndel.org
Wed Dec 11 13:17:14 UTC 2013


Oh boy. I can't believe I missed that when I applied Miika's patch...

/D

On Wed, 2013-12-11 at 21:21 +0100, Anton Lundin wrote:
> If realloc moved the memory, we shouldn't try to access it. realloc
> copied that memory so access it via the new function instead.
> 
> Signed-off-by: Anton Lundin <glance at acc.umu.se>
> ---
>  file.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/file.c b/file.c
> index 987c097..f1afde2 100644
> --- a/file.c
> +++ b/file.c
> @@ -131,12 +131,14 @@ static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, char
>  	 */
>  	buf = realloc(mem->buffer, mem->size + strlen("<csv></csv>"));
>  	if (buf != NULL) {
> -		memmove(buf + 5, mem->buffer, mem->size);
> +		memmove(buf + 5, buf, mem->size);
>  		memcpy(buf, "<csv>", 5);
> -		memcpy(mem->buffer + mem->size + 5, "</csv>", 7);
> -		mem->buffer = buf;
> +		memcpy(buf + mem->size + 5, "</csv>", 7);
>  		mem->size += strlen("<csv></csv>");
> +		mem->buffer = buf;
>  	} else {
> +		/* we can atleast try to strdup a error... */
> +		*error = strdup("realloc failed in __func__\n");
>  		free(mem->buffer);
>  		return 1;
>  	}




More information about the subsurface mailing list