emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] trunk r116615: Fix a few crashes and leaks when cloning C


From: Paul Eggert
Subject: [Emacs-diffs] trunk r116615: Fix a few crashes and leaks when cloning C strings.
Date: Fri, 28 Feb 2014 21:45:38 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 116615
revision-id: address@hidden
parent: address@hidden
author: Paul Eggert <address@hidden>
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Fri 2014-02-28 13:45:34 -0800
message:
  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.
modified:
  src/ChangeLog                  changelog-20091113204419-o5vbwnq5f7feedwu-1438
  src/alloc.c                    alloc.c-20091113204419-o5vbwnq5f7feedwu-252
  src/gtkutil.c                  gtkutil.c-20091113204419-o5vbwnq5f7feedwu-2527
  src/lisp.h                     lisp.h-20091113204419-o5vbwnq5f7feedwu-253
  src/term.c                     term.c-20091113204419-o5vbwnq5f7feedwu-220
  src/xsettings.c                xsettings.c-20091117210551-bqxn4u9uesl3d17a-4
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2014-02-28 10:32:11 +0000
+++ b/src/ChangeLog     2014-02-28 21:45:34 +0000
@@ -1,3 +1,15 @@
+2014-02-28  Paul Eggert  <address@hidden>
+
+       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.
+
 2014-02-28  Martin Rudalics  <address@hidden>
 
        * xdisp.c (note_mode_line_or_margin_highlight): Don't show drag

=== modified file 'src/alloc.c'
--- a/src/alloc.c       2014-01-20 16:55:28 +0000
+++ b/src/alloc.c       2014-02-28 21:45:34 +0000
@@ -850,6 +850,20 @@
   return memcpy (xmalloc (size), SSDATA (string), size);
 }
 
+/* Assign to *PTR a copy of STRING, freeing any storage *PTR formerly
+   pointed to.  If STRING is null, assign it without copying anything.
+   Allocate before freeing, to avoid a dangling pointer if allocation
+   fails.  */
+
+void
+dupstring (char **ptr, char const *string)
+{
+  char *old = *ptr;
+  *ptr = string ? xstrdup (string) : 0;
+  xfree (old);
+}
+
+
 /* Like putenv, but (1) use the equivalent of xmalloc and (2) the
    argument is a const pointer.  */
 

=== modified file 'src/gtkutil.c'
--- a/src/gtkutil.c     2014-01-13 01:40:35 +0000
+++ b/src/gtkutil.c     2014-02-28 21:45:34 +0000
@@ -2106,8 +2106,7 @@
          font = Ffont_spec (8, args);
 
          pango_font_description_free (desc);
-         xfree (x_last_font_name);
-         x_last_font_name = xstrdup (name);
+         dupstring (&x_last_font_name, name);
        }
 
 #else /* Use old font selector, which just returns the font name.  */

=== modified file 'src/lisp.h'
--- a/src/lisp.h        2014-01-03 06:47:27 +0000
+++ b/src/lisp.h        2014-02-28 21:45:34 +0000
@@ -4398,6 +4398,7 @@
 
 extern char *xstrdup (const char *);
 extern char *xlispstrdup (Lisp_Object);
+extern void dupstring (char **, char const *);
 extern void xputenv (const char *);
 
 extern char *egetenv (const char *);

=== modified file 'src/term.c'
--- a/src/term.c        2014-01-25 08:25:51 +0000
+++ b/src/term.c        2014-02-28 21:45:34 +0000
@@ -2052,17 +2052,9 @@
 
   if (save)
     {
-      xfree (default_orig_pair);
-      default_orig_pair = tty->TS_orig_pair ? xstrdup (tty->TS_orig_pair) : 
NULL;
-
-      xfree (default_set_foreground);
-      default_set_foreground = tty->TS_set_foreground ? xstrdup 
(tty->TS_set_foreground)
-                              : NULL;
-
-      xfree (default_set_background);
-      default_set_background = tty->TS_set_background ? xstrdup 
(tty->TS_set_background)
-                              : NULL;
-
+      dupstring (&default_orig_pair, tty->TS_orig_pair);
+      dupstring (&default_set_foreground, tty->TS_set_foreground);
+      dupstring (&default_set_background, tty->TS_set_background);
       default_max_colors = tty->TN_max_colors;
       default_max_pairs = tty->TN_max_pairs;
       default_no_color_video = tty->TN_no_color_video;

=== modified file 'src/xsettings.c'
--- a/src/xsettings.c   2014-01-01 07:43:34 +0000
+++ b/src/xsettings.c   2014-02-28 21:45:34 +0000
@@ -91,8 +91,7 @@
   if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
     return; /* No change. */
 
-  xfree (current_mono_font);
-  current_mono_font = xstrdup (newfont);
+  dupstring (&current_mono_font, newfont);
 
   if (dpyinfo_valid (first_dpyinfo) && use_system_font)
     {
@@ -111,8 +110,7 @@
   if (current_font != NULL && strcmp (newfont, current_font) == 0)
     return; /* No change. */
 
-  xfree (current_font);
-  current_font = xstrdup (newfont);
+  dupstring (&current_font, newfont);
 
   if (dpyinfo_valid (first_dpyinfo))
     {
@@ -492,13 +490,13 @@
           ++settings_seen;
           if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
             {
-              settings->tb_style = xstrdup (sval);
+              dupstring (&settings->tb_style, sval);
               settings->seen |= SEEN_TB_STYLE;
             }
 #ifdef HAVE_XFT
           else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
             {
-              settings->font = xstrdup (sval);
+              dupstring (&settings->font, sval);
               settings->seen |= SEEN_FONT;
             }
           else if (strcmp (name, "Xft/Antialias") == 0)
@@ -742,10 +740,7 @@
       if (send_event_p)
         store_font_name_changed (settings.font);
       else
-        {
-          xfree (current_font);
-          current_font = xstrdup (settings.font);
-        }
+       dupstring (&current_font, settings.font);
       xfree (settings.font);
     }
 #endif
@@ -835,7 +830,7 @@
     {
       g_variant_ref_sink (val);
       if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
-        current_mono_font = xstrdup (g_variant_get_string (val, NULL));
+       dupstring (&current_mono_font, g_variant_get_string (val, NULL));
       g_variant_unref (val);
     }
 
@@ -844,7 +839,7 @@
     {
       g_variant_ref_sink (val);
       if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
-        current_font = xstrdup (g_variant_get_string (val, NULL));
+        dupstring (&current_font, g_variant_get_string (val, NULL));
       g_variant_unref (val);
     }
 #endif /* HAVE_XFT */
@@ -886,13 +881,13 @@
   s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
   if (s)
     {
-      current_mono_font = xstrdup (s);
+      dupstring (&current_mono_font, s);
       g_free (s);
     }
   s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
   if (s)
     {
-      current_font = xstrdup (s);
+      dupstring (&current_font, s);
       g_free (s);
     }
   gconf_client_add_dir (gconf_client,


reply via email to

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