emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: replace alloca with strdup


From: David Kastrup
Subject: Re: replace alloca with strdup
Date: Sat, 21 May 2005 19:25:10 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Han Boetes <address@hidden> writes:

> While looking at some compilerwarnings I found an alloca and an
> strcpy.  And I realized this can also be done as follows:
>
>
> Index: xterm.c
> ===================================================================
> RCS file: /cvsroot/emacs/emacs/src/xterm.c,v
> retrieving revision 1.864
> diff -u -p -r1.864 xterm.c
> --- xterm.c   10 May 2005 09:19:19 -0000      1.864
> +++ xterm.c   21 May 2005 16:23:31 -0000
> @@ -7635,8 +7635,9 @@ x_connection_closed (dpy, error_message)
>    Lisp_Object frame, tail;
>    int count;
>  
> -  error_msg = (char *) alloca (strlen (error_message) + 1);
> -  strcpy (error_msg, error_message);
> +  if ((error_msg = strdup(error_message)) == NULL)
> +    errx(1, "Out of memory.");
> +
>    handling_signal = 0;
>  
>    /* Prevent being called recursively because of an error condition
>
>
> Advantages are:
>
>   - simpler code
>   - strdup is more portable
>   - error handling
>   - you don't have to worry about strdup concerning security
>
> I hope you like the idea.

Disadvantage: memory leak.

I don't like it.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum




reply via email to

[Prev in Thread] Current Thread [Next in Thread]