aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
authorPaul Eggert2014-02-28 13:45:34 -0800
committerPaul Eggert2014-02-28 13:45:34 -0800
commit8268febfe0c336ff47a61d0d416fd4bebf61a993 (patch)
tree4a1590e6aff922e69646cc35347192ea507b1c7a /src/alloc.c
parentb70257b07ea6053bce27b20ad0bda50f547bd393 (diff)
downloademacs-8268febfe0c336ff47a61d0d416fd4bebf61a993.tar.gz
emacs-8268febfe0c336ff47a61d0d416fd4bebf61a993.zip
Fix a few crashes and leaks when cloning C strings.
* alloc.c, lisp.h (dupstring): New function. * gtkutil.c (xg_get_font): * term.c (tty_default_color_capabilities): * xsettings.c (store_monospaced_changed) (store_font_name_changed, parse_settings) (read_and_apply_settings, init_gsettings, init_gconf): Use it. This avoids some unlikely crashes due to accessing freed storage, and avoids some minor memory leaks in the more-typical case.
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/alloc.c b/src/alloc.c
index 7f0a74ca834..7c671e25cfc 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -850,6 +850,20 @@ xlispstrdup (Lisp_Object string)
850 return memcpy (xmalloc (size), SSDATA (string), size); 850 return memcpy (xmalloc (size), SSDATA (string), size);
851} 851}
852 852
853/* Assign to *PTR a copy of STRING, freeing any storage *PTR formerly
854 pointed to. If STRING is null, assign it without copying anything.
855 Allocate before freeing, to avoid a dangling pointer if allocation
856 fails. */
857
858void
859dupstring (char **ptr, char const *string)
860{
861 char *old = *ptr;
862 *ptr = string ? xstrdup (string) : 0;
863 xfree (old);
864}
865
866
853/* Like putenv, but (1) use the equivalent of xmalloc and (2) the 867/* Like putenv, but (1) use the equivalent of xmalloc and (2) the
854 argument is a const pointer. */ 868 argument is a const pointer. */
855 869