[PATCH 1/6] xserver: Option memory leak fixes

Magnus Vigerlöf Magnus.Vigerlof at home.se
Wed Mar 28 15:03:17 PDT 2007


Option memory leak fixes

Correction of a few small memory leaks in the option handling functions
and the places where these are called in a faulty way.

--
 hw/xfree86/common/xf86Config.c       |    6 +++---
 hw/xfree86/common/xf86Configure.c    |   15 +++++++++------
 hw/xfree86/common/xf86Option.c       |    4 ++--
 hw/xfree86/parser/Flags.c            |   28 ++++++++++++++--------------
 hw/xfree86/utils/xorgcfg/text-mode.c |    4 ++--
 5 files changed, 30 insertions(+), 27 deletions(-)

--
diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c
index 4db844e..32c270a 100644
--- a/hw/xfree86/common/xf86Config.c
+++ b/hw/xfree86/common/xf86Config.c
@@ -1271,7 +1271,7 @@ checkCoreInputDevices(serverLayoutPtr se
 			      (count + 1) * sizeof(IDevRec));
 	    indp[count - 1] = Pointer;
 	    indp[count - 1].extraOptions =
-				xf86addNewOption(NULL, "CorePointer", NULL);
+				xf86addNewOption(NULL, xnfstrdup("CorePointer"), NULL);
 	    indp[count].identifier = NULL;
 	    servlayoutp->inputs = indp;
 	}
@@ -1306,7 +1306,7 @@ checkCoreInputDevices(serverLayoutPtr se
 			      (count + 1) * sizeof(IDevRec));
 	    indp[count - 1] = Pointer;
 	    indp[count - 1].extraOptions =
-				xf86addNewOption(NULL, "AlwaysCore", NULL);
+				xf86addNewOption(NULL, xnfstrdup("AlwaysCore"), NULL);
 	    indp[count].identifier = NULL;
 	    servlayoutp->inputs = indp;
 	}
@@ -1397,7 +1397,7 @@ checkCoreInputDevices(serverLayoutPtr se
 			      (count + 1) * sizeof(IDevRec));
 	    indp[count - 1] = Keyboard;
 	    indp[count - 1].extraOptions =
-				xf86addNewOption(NULL, "CoreKeyboard", NULL);
+				xf86addNewOption(NULL, xnfstrdup("CoreKeyboard"), NULL);
 	    indp[count].identifier = NULL;
 	    servlayoutp->inputs = indp;
 	}
diff --git a/hw/xfree86/common/xf86Configure.c 
b/hw/xfree86/common/xf86Configure.c
index 4b482df..7b04bd6 100644
--- a/hw/xfree86/common/xf86Configure.c
+++ b/hw/xfree86/common/xf86Configure.c
@@ -309,13 +309,16 @@ #endif
     mouse->inp_identifier = "Mouse0";
     mouse->inp_driver = "mouse";
     mouse->inp_option_lst = 
-		xf86addNewOption(mouse->inp_option_lst, "Protocol", DFLT_MOUSE_PROTO);
+		xf86addNewOption(mouse->inp_option_lst, xstrdup("Protocol"),
+				xstrdup(DFLT_MOUSE_PROTO));
 #ifndef __SCO__
     mouse->inp_option_lst = 
-		xf86addNewOption(mouse->inp_option_lst, "Device", DFLT_MOUSE_DEV);
+		xf86addNewOption(mouse->inp_option_lst, xstrdup("Device"),
+				xstrdup(DFLT_MOUSE_DEV));
 #endif
     mouse->inp_option_lst = 
-		xf86addNewOption(mouse->inp_option_lst, "ZAxisMapping", "4 5 6 7");
+		xf86addNewOption(mouse->inp_option_lst, xstrdup("ZAxisMapping"),
+				xstrdup("4 5 6 7"));
     ptr = (XF86ConfInputPtr)xf86addListItem((glp)ptr, (glp)mouse);
     return ptr;
 }
@@ -519,7 +522,7 @@ configureLayoutSection (void)
 	iptr->iref_option_lst = NULL;
 	iptr->iref_inputdev_str = "Mouse0";
 	iptr->iref_option_lst =
-		xf86addNewOption (iptr->iref_option_lst, "CorePointer", NULL);
+		xf86addNewOption (iptr->iref_option_lst, xstrdup("CorePointer"), NULL);
 	ptr->lay_input_lst = (XF86ConfInputrefPtr)
 		xf86addListItem ((glp) ptr->lay_input_lst, (glp) iptr);
     }
@@ -532,7 +535,7 @@ configureLayoutSection (void)
 	iptr->iref_option_lst = NULL;
 	iptr->iref_inputdev_str = "Keyboard0";
 	iptr->iref_option_lst =
-		xf86addNewOption (iptr->iref_option_lst, "CoreKeyboard", NULL);
+		xf86addNewOption (iptr->iref_option_lst, xstrdup("CoreKeyboard"), NULL);
 	ptr->lay_input_lst = (XF86ConfInputrefPtr)
 		xf86addListItem ((glp) ptr->lay_input_lst, (glp) iptr);
     }
@@ -751,7 +754,7 @@ #endif /* def CONFIGURE_DISPLAYSIZE */
     }
 
     if (ConfiguredMonitor->features.dpms) {
-      ptr->mon_option_lst = xf86addNewOption(ptr->mon_option_lst, "DPMS", 
NULL);
+      ptr->mon_option_lst = xf86addNewOption(ptr->mon_option_lst, 
xstrdup("DPMS"), NULL);
     }
 
     return ptr;
diff --git a/hw/xfree86/common/xf86Option.c b/hw/xfree86/common/xf86Option.c
index 7afb63a..50a7d9c 100644
--- a/hw/xfree86/common/xf86Option.c
+++ b/hw/xfree86/common/xf86Option.c
@@ -289,7 +289,7 @@ xf86CheckBoolOption(pointer optlist, con
 _X_EXPORT pointer
 xf86ReplaceIntOption(pointer optlist, const char *name, const int val)
 {
-    char *tmp = xnfalloc(16);
+    char tmp[16];
     sprintf(tmp,"%i",val);
     return xf86AddNewOption(optlist,name,tmp);
 }
@@ -297,7 +297,7 @@ xf86ReplaceIntOption(pointer optlist, co
 _X_EXPORT pointer
 xf86ReplaceRealOption(pointer optlist, const char *name, const double val)
 {
-    char *tmp = xnfalloc(32);
+    char tmp[32];
     snprintf(tmp,32,"%f",val);
     return xf86AddNewOption(optlist,name,tmp);
 }
diff --git a/hw/xfree86/parser/Flags.c b/hw/xfree86/parser/Flags.c
index 4adea1a..5b60a51 100644
--- a/hw/xfree86/parser/Flags.c
+++ b/hw/xfree86/parser/Flags.c
@@ -198,21 +198,21 @@ addNewOption2 (XF86OptionPtr head, char 
 {
 	XF86OptionPtr new, old = NULL;
 
-	/* Don't allow duplicates */
- 	if (head != NULL && (old = xf86findOption(head, name)) != NULL)
- 		new = old;
- 	else {
+	/* Don't allow duplicates, free old strings */
+	if (head != NULL && (old = xf86findOption(head, name)) != NULL) {
+		new = old;
+		xf86conffree(new->opt_name);
+		xf86conffree(new->opt_val);
+	}
+	else
 		new = xf86confcalloc (1, sizeof (XF86OptionRec));
- 		new->list.next = NULL;
- 	}
- 	new->opt_name = name;
- 	new->opt_val = val;
- 	new->opt_used = used;
-	
-  	if (old == NULL)
-		return ((XF86OptionPtr) xf86addListItem ((glp) head, (glp) new));
- 	else 
- 		return head;
+	new->opt_name = name;
+	new->opt_val = val;
+	new->opt_used = used;
+
+	if (old)
+		return head;
+	return ((XF86OptionPtr) xf86addListItem ((glp) head, (glp) new));
 }
 
 XF86OptionPtr
diff --git a/hw/xfree86/utils/xorgcfg/text-mode.c 
b/hw/xfree86/utils/xorgcfg/text-mode.c
index 9797ca2..8800c29 100644
--- a/hw/xfree86/utils/xorgcfg/text-mode.c
+++ b/hw/xfree86/utils/xorgcfg/text-mode.c
@@ -2111,7 +2111,7 @@ LayoutConfig(void)
 		    else
 			iref->iref_option_lst =
 			    xf86addNewOption(iref->iref_option_lst,
-					  "CorePointer", NULL);
+					  XtNewString("CorePointer"), NULL);
 		    option = xf86findOption(mref->iref_option_lst,
 					    "CorePointer");
 		    XtFree(option->opt_name);
@@ -2209,7 +2209,7 @@ LayoutConfig(void)
 		    else
 			iref->iref_option_lst =
 			    xf86addNewOption(iref->iref_option_lst,
-					  "CoreKeyboard", NULL);
+					  XtNewString("CoreKeyboard"), NULL);
 		    option = xf86findOption(kref->iref_option_lst,
 					    "CoreKeyboard");
 		    XtFree(option->opt_name);



More information about the xorg mailing list