From bc095d579bc6b1380080c83c4e5f6df173364b76 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 9 May 2013 10:39:39 +1000 Subject: Plug memory leaks left by xf86SetStrOption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xf86SetStrOption() returns a strdup'd string that needs to be freed after use. This requires some const char* → char* changes too, for things that were never really const char* to begin with anyway. wcmEventAutoDevProbe() can use xf86CheckStrOption so it too returns a strup that we can free lateron. Signed-off-by: Peter Hutterer Reviewed-by: Ping Cheng --- src/wcmCommon.c | 1 + src/wcmConfig.c | 11 ++++++++--- src/wcmValidateDevice.c | 42 ++++++++++++++++++++++++++++++------------ src/xf86Wacom.c | 4 ++-- src/xf86Wacom.h | 4 ++-- src/xf86WacomDefs.h | 2 +- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/wcmCommon.c b/src/wcmCommon.c index 1ab2490..a1858e2 100644 --- a/src/wcmCommon.c +++ b/src/wcmCommon.c @@ -1462,6 +1462,7 @@ void wcmFreeCommon(WacomCommonPtr *ptr) free(common->serials); common->serials = next; } + free(common->device_path); free(common); } *ptr = NULL; diff --git a/src/wcmConfig.c b/src/wcmConfig.c index cc3d9db..3ba830b 100644 --- a/src/wcmConfig.c +++ b/src/wcmConfig.c @@ -470,7 +470,9 @@ static void wcmLinkTouchAndPen(InputInfoPtr pInfo) static int wcmIsHotpluggedDevice(InputInfoPtr pInfo) { char *source = xf86CheckStrOption(pInfo->options, "_source", ""); - return !strcmp(source, "_driver/wacom"); + int matches = (strcmp(source, "_driver/wacom") == 0); + free(source); + return matches; } /* wcmPreInit - called for each input devices with the driver set to @@ -510,8 +512,8 @@ static int wcmPreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags) { WacomDevicePtr priv = NULL; WacomCommonPtr common = NULL; - const char* type; - const char* device, *oldname; + char *type, *device; + const char *oldname; int need_hotplug = 0, is_dependent = 0; gWacomModule.wcmDrv = drv; @@ -609,6 +611,8 @@ static int wcmPreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags) if (IsTouch(priv) || (IsTablet(priv) && !common->wcmTouchDevice)) wcmLinkTouchAndPen(pInfo); + free(type); + return Success; SetupProc_fail: @@ -622,6 +626,7 @@ SetupProc_fail: pInfo->fd = -1; } + free(type); return BadMatch; } diff --git a/src/wcmValidateDevice.c b/src/wcmValidateDevice.c index 8e27bb1..4e8289b 100644 --- a/src/wcmValidateDevice.c +++ b/src/wcmValidateDevice.c @@ -35,12 +35,17 @@ static Bool wcmCheckSource(InputInfoPtr pInfo, dev_t min_maj) int match = 0; InputInfoPtr pDevices = xf86FirstLocalDevice(); - for (; pDevices != NULL; pDevices = pDevices->next) + for (; !match && pDevices != NULL; pDevices = pDevices->next) { char* device = xf86CheckStrOption(pDevices->options, "Device", NULL); /* device can be NULL on some distros */ - if (!device || !strstr(pDevices->drv->driverName, "wacom")) + if (!device) + continue; + + free(device); + + if (!strstr(pDevices->drv->driverName, "wacom")) continue; if (pInfo != pDevices) @@ -55,11 +60,10 @@ static Bool wcmCheckSource(InputInfoPtr pInfo, dev_t min_maj) /* only add the new tool if the matching major/minor * was from the same source */ if (strcmp(fsource, psource)) - { match = 1; - break; - } } + free(fsource); + free(psource); } } if (match) @@ -112,6 +116,7 @@ int wcmIsDuplicate(const char* device, InputInfoPtr pInfo) isInUse = 4; } ret: + free(lsource); return isInUse; } @@ -134,7 +139,7 @@ Bool wcmIsAValidType(InputInfoPtr pInfo, const char* type) int j, k, ret = FALSE; WacomDevicePtr priv = (WacomDevicePtr)pInfo->private; WacomCommonPtr common = priv->common; - char* dsource = xf86CheckStrOption(pInfo->options, "_source", NULL); + char* dsource; if (!type) { @@ -142,6 +147,8 @@ Bool wcmIsAValidType(InputInfoPtr pInfo, const char* type) return FALSE; } + dsource = xf86CheckStrOption(pInfo->options, "_source", NULL); + /* walkthrough all types */ for (j = 0; j < ARRAY_SIZE(wcmType); j++) { @@ -175,6 +182,7 @@ Bool wcmIsAValidType(InputInfoPtr pInfo, const char* type) xf86Msg(X_ERROR, "%s: Invalid type '%s' for this device.\n", pInfo->name, type); + free(dsource); return ret; } @@ -575,19 +583,20 @@ void wcmHotplugOthers(InputInfoPtr pInfo, const char *basename) * This changes the source to _driver/wacom, all auto-hotplugged devices * will have the same source. */ -int wcmNeedAutoHotplug(InputInfoPtr pInfo, const char **type) +int wcmNeedAutoHotplug(InputInfoPtr pInfo, char **type) { char *source = xf86CheckStrOption(pInfo->options, "_source", NULL); int i; + int rc = 0; if (*type) /* type specified, don't hotplug */ - return 0; + goto out; if (!source) /* xorg.conf device, don't auto-pick type */ - return 0; + goto out; if (source && strcmp(source, "server/hal") && strcmp(source, "server/udev")) - return 0; + goto out; /* no type specified, so we need to pick the first one applicable * for our device */ @@ -595,13 +604,14 @@ int wcmNeedAutoHotplug(InputInfoPtr pInfo, const char **type) { if (wcmIsAValidType(pInfo, wcmType[i].type)) { + free(*type); *type = strdup(wcmType[i].type); break; } } if (!*type) - return 0; + goto out; xf86Msg(X_INFO, "%s: type not specified, assuming '%s'.\n", pInfo->name, *type); xf86Msg(X_INFO, "%s: other types will be automatically added.\n", pInfo->name); @@ -610,7 +620,11 @@ int wcmNeedAutoHotplug(InputInfoPtr pInfo, const char **type) pInfo->options = xf86AddNewOption(pInfo->options, "Type", *type); pInfo->options = xf86ReplaceStrOption(pInfo->options, "_source", "_driver/wacom"); - return 1; + rc = 1; + + free(source); +out: + return rc; } int wcmParseSerials (InputInfoPtr pInfo) @@ -737,6 +751,8 @@ Bool wcmPreInitParseOptions(InputInfoPtr pInfo, Bool is_primary, */ } + free(s); + /* Pad is always in absolute mode. * The pad also defaults to wheel scrolling, unlike the pens * (interesting effects happen on ArtPen and others with build-in @@ -773,6 +789,7 @@ Bool wcmPreInitParseOptions(InputInfoPtr pInfo, Bool is_primary, " device\n", pInfo->name); else wcmRotateTablet(pInfo, rotation); + free(s); } common->wcmRawSample = xf86SetIntOption(pInfo->options, "RawSample", @@ -811,6 +828,7 @@ Bool wcmPreInitParseOptions(InputInfoPtr pInfo, Bool is_primary, else wcmSetPressureCurve(priv,a,b,c,d); } + free(s); /*Serials of tools we want hotpluged*/ if (wcmParseSerials (pInfo) != 0) diff --git a/src/xf86Wacom.c b/src/xf86Wacom.c index e92f56f..5289292 100644 --- a/src/xf86Wacom.c +++ b/src/xf86Wacom.c @@ -483,7 +483,7 @@ Bool wcmIsWacomDevice (char* fname) ****************************************************************************/ #define DEV_INPUT_EVENT "/dev/input/event%d" #define EVDEV_MINORS 32 -const char *wcmEventAutoDevProbe (InputInfoPtr pInfo) +char *wcmEventAutoDevProbe (InputInfoPtr pInfo) { /* We are trying to find the right eventX device */ int i, wait = 0; @@ -506,7 +506,7 @@ const char *wcmEventAutoDevProbe (InputInfoPtr pInfo) xf86ReplaceStrOption(pInfo->options, "Device", fname); /* this assumes there is only one Wacom device on the system */ - return xf86FindOptionValue(pInfo->options, "Device"); + return xf86CheckStrOption(pInfo->options, "Device", NULL); } } wait += 100; diff --git a/src/xf86Wacom.h b/src/xf86Wacom.h index cf71c79..b9e8957 100644 --- a/src/xf86Wacom.h +++ b/src/xf86Wacom.h @@ -113,7 +113,7 @@ struct _WacomModule extern Bool wcmOpen(InputInfoPtr pInfo); /* device autoprobing */ -const char *wcmEventAutoDevProbe (InputInfoPtr pInfo); +char *wcmEventAutoDevProbe (InputInfoPtr pInfo); /* common tablet initialization regime */ int wcmInitTablet(InputInfoPtr pInfo, const char* id, float version); @@ -141,7 +141,7 @@ extern int wcmIsDuplicate(const char* device, InputInfoPtr pInfo); extern int wcmDeviceTypeKeys(InputInfoPtr pInfo); /* hotplug */ -extern int wcmNeedAutoHotplug(InputInfoPtr pInfo, const char **type); +extern int wcmNeedAutoHotplug(InputInfoPtr pInfo, char **type); extern void wcmHotplugOthers(InputInfoPtr pInfo, const char *basename); /* setup */ diff --git a/src/xf86WacomDefs.h b/src/xf86WacomDefs.h index a7cdcd4..3a64fd6 100644 --- a/src/xf86WacomDefs.h +++ b/src/xf86WacomDefs.h @@ -418,7 +418,7 @@ enum WacomProtocol { struct _WacomCommonRec { /* Do not move device_path, same offset as priv->name. Used by DBG macro */ - const char* device_path; /* device file name */ + char* device_path; /* device file name */ dev_t min_maj; /* minor/major number */ unsigned char wcmFlags; /* various flags (handle tilt) */ int debugLevel; -- cgit v1.2.3