From: David Kastrup <dak@gnu.org>
Subject: Re: replace alloca with strdup
Date: Sat, 21 May 2005 19:25:10 +0200 [thread overview]
Message-ID: <85y8a81og9.fsf@lola.goethe.zz> (raw)
In-Reply-To: <20050521163028.GM28212@boetes.org> (Han Boetes's message of "Sat, 21 May 2005 18:30:06 +0200")
Han Boetes <han@mijncomputer.nl> 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
next prev parent reply other threads:[~2005-05-21 17:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-05-21 16:30 replace alloca with strdup Han Boetes
2005-05-21 17:07 ` Masatake YAMATO
2005-05-21 17:25 ` David Kastrup [this message]
2005-05-21 23:49 ` Andreas Schwab
2005-05-22 9:15 ` Richard Stallman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=85y8a81og9.fsf@lola.goethe.zz \
--to=dak@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.