From 4771fa8747791498e504d73afccfb5833499a38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Vigerl=C3=B6f?= Date: Tue, 10 Apr 2007 23:48:00 +0300 Subject: [PATCH] XFree86: Fix memory leak in option parsing Fix option parsing functions and callers thereof to not leak memory. --- 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 cbeda0bd1..0421bf94b 100644 --- a/hw/xfree86/common/xf86Config.c +++ b/hw/xfree86/common/xf86Config.c @@ -1271,7 +1271,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout) (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; } @@ -1310,7 +1310,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout) (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; } @@ -1401,7 +1401,7 @@ checkCoreInputDevices(serverLayoutPtr servlayoutp, Bool implicitLayout) (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 4b482dfdc..7b04bd6bd 100644 --- a/hw/xfree86/common/xf86Configure.c +++ b/hw/xfree86/common/xf86Configure.c @@ -309,13 +309,16 @@ configureInputSection (void) 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 @@ configureDDCMonitorSection (int screennum) } 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 7afb63a4d..50a7d9cb9 100644 --- a/hw/xfree86/common/xf86Option.c +++ b/hw/xfree86/common/xf86Option.c @@ -289,7 +289,7 @@ xf86CheckBoolOption(pointer optlist, const char *name, int deflt) _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, const char *name, const int val) _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 4adea1ade..5b60a5155 100644 --- a/hw/xfree86/parser/Flags.c +++ b/hw/xfree86/parser/Flags.c @@ -198,21 +198,21 @@ addNewOption2 (XF86OptionPtr head, char *name, char *val, int used) { 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 9797ca283..8800c29d7 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);