aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2014-02-28 13:45:34 -0800
committerPaul Eggert2014-02-28 13:45:34 -0800
commit8268febfe0c336ff47a61d0d416fd4bebf61a993 (patch)
tree4a1590e6aff922e69646cc35347192ea507b1c7a
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.
-rw-r--r--src/ChangeLog12
-rw-r--r--src/alloc.c14
-rw-r--r--src/gtkutil.c3
-rw-r--r--src/lisp.h1
-rw-r--r--src/term.c14
-rw-r--r--src/xsettings.c23
6 files changed, 40 insertions, 27 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index d2eeab33e4f..704ac68b67e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
12014-02-28 Paul Eggert <eggert@penguin.cs.ucla.edu>
2
3 Fix a few crashes and leaks when cloning C strings.
4 * alloc.c, lisp.h (dupstring): New function.
5 * gtkutil.c (xg_get_font):
6 * term.c (tty_default_color_capabilities):
7 * xsettings.c (store_monospaced_changed)
8 (store_font_name_changed, parse_settings)
9 (read_and_apply_settings, init_gsettings, init_gconf): Use it.
10 This avoids some unlikely crashes due to accessing freed storage,
11 and avoids some minor memory leaks in the more-typical case.
12
12014-02-28 Martin Rudalics <rudalics@gmx.at> 132014-02-28 Martin Rudalics <rudalics@gmx.at>
2 14
3 * xdisp.c (note_mode_line_or_margin_highlight): Don't show drag 15 * xdisp.c (note_mode_line_or_margin_highlight): Don't show drag
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
diff --git a/src/gtkutil.c b/src/gtkutil.c
index 6e039c7a141..cebff68614f 100644
--- a/src/gtkutil.c
+++ b/src/gtkutil.c
@@ -2106,8 +2106,7 @@ xg_get_font (struct frame *f, const char *default_name)
2106 font = Ffont_spec (8, args); 2106 font = Ffont_spec (8, args);
2107 2107
2108 pango_font_description_free (desc); 2108 pango_font_description_free (desc);
2109 xfree (x_last_font_name); 2109 dupstring (&x_last_font_name, name);
2110 x_last_font_name = xstrdup (name);
2111 } 2110 }
2112 2111
2113#else /* Use old font selector, which just returns the font name. */ 2112#else /* Use old font selector, which just returns the font name. */
diff --git a/src/lisp.h b/src/lisp.h
index a7e80a41c28..2f9a30fdfe9 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -4398,6 +4398,7 @@ extern void *xpalloc (void *, ptrdiff_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t);
4398 4398
4399extern char *xstrdup (const char *); 4399extern char *xstrdup (const char *);
4400extern char *xlispstrdup (Lisp_Object); 4400extern char *xlispstrdup (Lisp_Object);
4401extern void dupstring (char **, char const *);
4401extern void xputenv (const char *); 4402extern void xputenv (const char *);
4402 4403
4403extern char *egetenv (const char *); 4404extern char *egetenv (const char *);
diff --git a/src/term.c b/src/term.c
index 0c40c2d16b9..61a8d39d971 100644
--- a/src/term.c
+++ b/src/term.c
@@ -2052,17 +2052,9 @@ tty_default_color_capabilities (struct tty_display_info *tty, bool save)
2052 2052
2053 if (save) 2053 if (save)
2054 { 2054 {
2055 xfree (default_orig_pair); 2055 dupstring (&default_orig_pair, tty->TS_orig_pair);
2056 default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : NULL; 2056 dupstring (&default_set_foreground, tty->TS_set_foreground);
2057 2057 dupstring (&default_set_background, tty->TS_set_background);
2058 xfree (default_set_foreground);
2059 default_set_foreground = tty->TS_set_foreground ? xstrdup (tty->TS_set_foreground)
2060 : NULL;
2061
2062 xfree (default_set_background);
2063 default_set_background = tty->TS_set_background ? xstrdup (tty->TS_set_background)
2064 : NULL;
2065
2066 default_max_colors = tty->TN_max_colors; 2058 default_max_colors = tty->TN_max_colors;
2067 default_max_pairs = tty->TN_max_pairs; 2059 default_max_pairs = tty->TN_max_pairs;
2068 default_no_color_video = tty->TN_no_color_video; 2060 default_no_color_video = tty->TN_no_color_video;
diff --git a/src/xsettings.c b/src/xsettings.c
index 458c3d45e9a..844da19f638 100644
--- a/src/xsettings.c
+++ b/src/xsettings.c
@@ -91,8 +91,7 @@ store_monospaced_changed (const char *newfont)
91 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0) 91 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
92 return; /* No change. */ 92 return; /* No change. */
93 93
94 xfree (current_mono_font); 94 dupstring (&current_mono_font, newfont);
95 current_mono_font = xstrdup (newfont);
96 95
97 if (dpyinfo_valid (first_dpyinfo) && use_system_font) 96 if (dpyinfo_valid (first_dpyinfo) && use_system_font)
98 { 97 {
@@ -111,8 +110,7 @@ store_font_name_changed (const char *newfont)
111 if (current_font != NULL && strcmp (newfont, current_font) == 0) 110 if (current_font != NULL && strcmp (newfont, current_font) == 0)
112 return; /* No change. */ 111 return; /* No change. */
113 112
114 xfree (current_font); 113 dupstring (&current_font, newfont);
115 current_font = xstrdup (newfont);
116 114
117 if (dpyinfo_valid (first_dpyinfo)) 115 if (dpyinfo_valid (first_dpyinfo))
118 { 116 {
@@ -492,13 +490,13 @@ parse_settings (unsigned char *prop,
492 ++settings_seen; 490 ++settings_seen;
493 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0) 491 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
494 { 492 {
495 settings->tb_style = xstrdup (sval); 493 dupstring (&settings->tb_style, sval);
496 settings->seen |= SEEN_TB_STYLE; 494 settings->seen |= SEEN_TB_STYLE;
497 } 495 }
498#ifdef HAVE_XFT 496#ifdef HAVE_XFT
499 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0) 497 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
500 { 498 {
501 settings->font = xstrdup (sval); 499 dupstring (&settings->font, sval);
502 settings->seen |= SEEN_FONT; 500 settings->seen |= SEEN_FONT;
503 } 501 }
504 else if (strcmp (name, "Xft/Antialias") == 0) 502 else if (strcmp (name, "Xft/Antialias") == 0)
@@ -742,10 +740,7 @@ read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
742 if (send_event_p) 740 if (send_event_p)
743 store_font_name_changed (settings.font); 741 store_font_name_changed (settings.font);
744 else 742 else
745 { 743 dupstring (&current_font, settings.font);
746 xfree (current_font);
747 current_font = xstrdup (settings.font);
748 }
749 xfree (settings.font); 744 xfree (settings.font);
750 } 745 }
751#endif 746#endif
@@ -835,7 +830,7 @@ init_gsettings (void)
835 { 830 {
836 g_variant_ref_sink (val); 831 g_variant_ref_sink (val);
837 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING)) 832 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
838 current_mono_font = xstrdup (g_variant_get_string (val, NULL)); 833 dupstring (&current_mono_font, g_variant_get_string (val, NULL));
839 g_variant_unref (val); 834 g_variant_unref (val);
840 } 835 }
841 836
@@ -844,7 +839,7 @@ init_gsettings (void)
844 { 839 {
845 g_variant_ref_sink (val); 840 g_variant_ref_sink (val);
846 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING)) 841 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
847 current_font = xstrdup (g_variant_get_string (val, NULL)); 842 dupstring (&current_font, g_variant_get_string (val, NULL));
848 g_variant_unref (val); 843 g_variant_unref (val);
849 } 844 }
850#endif /* HAVE_XFT */ 845#endif /* HAVE_XFT */
@@ -886,13 +881,13 @@ init_gconf (void)
886 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL); 881 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
887 if (s) 882 if (s)
888 { 883 {
889 current_mono_font = xstrdup (s); 884 dupstring (&current_mono_font, s);
890 g_free (s); 885 g_free (s);
891 } 886 }
892 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL); 887 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
893 if (s) 888 if (s)
894 { 889 {
895 current_font = xstrdup (s); 890 dupstring (&current_font, s);
896 g_free (s); 891 g_free (s);
897 } 892 }
898 gconf_client_add_dir (gconf_client, 893 gconf_client_add_dir (gconf_client,