Blame SOURCES/0007-Fix-leaks-detected-by-covscan.patch

cf0dd1
From a35bef8c333f3fcf12d66e38ad769bc5f1df16a3 Mon Sep 17 00:00:00 2001
cf0dd1
From: Benjamin Tissoires <benjamin.tissoires@gmail.com>
cf0dd1
Date: Thu, 11 Apr 2019 17:26:58 +0200
cf0dd1
Subject: [PATCH 7/9] Fix leaks detected by covscan
cf0dd1
cf0dd1
The following leaks are reported by covscan:
cf0dd1
cf0dd1
Error: RESOURCE_LEAK (CWE-772):
cf0dd1
libXt-20190411/src/ResConfig.c:542: alloc_arg: "_get_part" allocates memory that is stored into "part".
cf0dd1
libXt-20190411/src/ResConfig.c:544: noescape: Resource "part" is not freed or pointed-to in "_match_resource_to_widget".
cf0dd1
libXt-20190411/src/ResConfig.c:560: leaked_storage: Variable "part" going out of scope leaks the storage it points to.
cf0dd1
cf0dd1
Error: RESOURCE_LEAK (CWE-772):
cf0dd1
libXt-20190411/src/TMgrab.c:108: alloc_arg: "XtKeysymToKeycodeList" allocates memory that is stored into "keycodes".
cf0dd1
libXt-20190411/src/TMgrab.c:115: var_assign: Assigning: "keycodeP" = "keycodes".
cf0dd1
libXt-20190411/src/TMgrab.c:124: leaked_storage: Variable "keycodeP" going out of scope leaks the storage it points to.
cf0dd1
libXt-20190411/src/TMgrab.c:124: leaked_storage: Variable "keycodes" going out of scope leaks the storage it points to.
cf0dd1
cf0dd1
Error: RESOURCE_LEAK (CWE-772):
cf0dd1
libXt-20190411/src/TMparse.c:1544: alloc_fn: Storage is returned from allocation function "XtMalloc".
cf0dd1
libXt-20190411/src/TMparse.c:1544: var_assign: Assigning: "event" = storage returned from "XtMalloc(88U)".
cf0dd1
libXt-20190411/src/TMparse.c:1549: noescape: Resource "event" is not freed or pointed-to in "ParseQuotedStringEvent".
cf0dd1
libXt-20190411/src/TMparse.c:1555: leaked_storage: Variable "event" going out of scope leaks the storage it points to.
cf0dd1
cf0dd1
Error: RESOURCE_LEAK (CWE-772):
cf0dd1
libXt-20190411/src/TMparse.c:1779: alloc_fn: Storage is returned from allocation function "XtMalloc".
cf0dd1
libXt-20190411/src/TMparse.c:1779: var_assign: Assigning: "action" = storage returned from "XtMalloc(32U)".
cf0dd1
libXt-20190411/src/TMparse.c:1784: noescape: Resource "action" is not freed or pointed-to in "ParseAction".
cf0dd1
libXt-20190411/src/TMparse.c:1785: leaked_storage: Variable "action" going out of scope leaks the storage it points to.
cf0dd1
cf0dd1
In addition to this legitimate leaks, covscan can get confused by
cf0dd1
the allocated memory in XtKeysymToKeycodeList:
cf0dd1
cf0dd1
Error: RESOURCE_LEAK (CWE-772):
cf0dd1
libXt-20190411/src/TMgrab.c:108: alloc_arg: "XtKeysymToKeycodeList" allocates memory that is stored into "keycodes".
cf0dd1
libXt-20190411/src/TMgrab.c:114: leaked_storage: Variable "keycodes" going out of scope leaks the storage it points to.
cf0dd1
cf0dd1
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
cf0dd1
---
cf0dd1
 src/ResConfig.c |  1 +
cf0dd1
 src/TMgrab.c    | 10 ++++++++--
cf0dd1
 src/TMparse.c   |  6 +++++-
cf0dd1
 3 files changed, 14 insertions(+), 3 deletions(-)
cf0dd1
cf0dd1
diff --git a/src/ResConfig.c b/src/ResConfig.c
cf0dd1
index 5a7f6d2..bd6792c 100644
cf0dd1
--- a/src/ResConfig.c
cf0dd1
+++ b/src/ResConfig.c
cf0dd1
@@ -557,6 +557,7 @@ _set_and_search (
cf0dd1
 			} else
cf0dd1
 				_search_child (w, local_index, remainder,
cf0dd1
 					resource, value, last_token, last_part);
cf0dd1
+			XtFree (part);
cf0dd1
 			return;
cf0dd1
 		}
cf0dd1
 		if (token == '*') {
cf0dd1
diff --git a/src/TMgrab.c b/src/TMgrab.c
cf0dd1
index 08cb486..4e7d20d 100644
cf0dd1
--- a/src/TMgrab.c
cf0dd1
+++ b/src/TMgrab.c
cf0dd1
@@ -105,13 +105,17 @@ static void GrabAllCorrectKeys(
cf0dd1
     careOn |= modMatch->modifiers;
cf0dd1
     careMask |= modMatch->modifierMask;
cf0dd1
 
cf0dd1
+    keycodes = NULL;
cf0dd1
     XtKeysymToKeycodeList(
cf0dd1
 	    dpy,
cf0dd1
 	    (KeySym)typeMatch->eventCode,
cf0dd1
 	    &keycodes,
cf0dd1
 	    &keycount
cf0dd1
 			 );
cf0dd1
-    if (keycount == 0) return;
cf0dd1
+    if (keycount == 0) {
cf0dd1
+	XtFree((char *)keycodes);
cf0dd1
+	return;
cf0dd1
+    }
cf0dd1
     for (keycodeP = keycodes; keycount--; keycodeP++) {
cf0dd1
 	if (modMatch->standard) {
cf0dd1
 	    /* find standard modifiers that produce this keysym */
cf0dd1
@@ -120,8 +124,10 @@ static void GrabAllCorrectKeys(
cf0dd1
 	    Modifiers modifiers_return;
cf0dd1
 	    XtTranslateKeycode( dpy, *keycodeP, (Modifiers)0,
cf0dd1
 			        &modifiers_return, &keysym );
cf0dd1
-	    if (careOn & modifiers_return)
cf0dd1
+	    if (careOn & modifiers_return) {
cf0dd1
+		XtFree((char *)keycodes);
cf0dd1
 		return;
cf0dd1
+	    }
cf0dd1
 	    if (keysym == typeMatch->eventCode) {
cf0dd1
 		XtGrabKey(widget, *keycodeP, careOn,
cf0dd1
 			  grabP->owner_events,
cf0dd1
diff --git a/src/TMparse.c b/src/TMparse.c
cf0dd1
index df94181..086f53d 100644
cf0dd1
--- a/src/TMparse.c
cf0dd1
+++ b/src/TMparse.c
cf0dd1
@@ -1551,6 +1551,7 @@ static String ParseEventSeq(
cf0dd1
 			XtCXtToolkitError,
cf0dd1
 			"... probably due to non-Latin1 character in quoted string",
cf0dd1
 			(String*)NULL, (Cardinal*)NULL);
cf0dd1
+		    XtFree((char *)event);
cf0dd1
 		    return PanicModeRecovery(str);
cf0dd1
 		}
cf0dd1
 		*nextEvent = event;
cf0dd1
@@ -1781,7 +1782,10 @@ static String ParseActionSeq(
cf0dd1
         action->next = NULL;
cf0dd1
 
cf0dd1
 	str = ParseAction(str, action, &quark, error);
cf0dd1
-	if (*error) return PanicModeRecovery(str);
cf0dd1
+	if (*error) {
cf0dd1
+	    XtFree((char *)action);
cf0dd1
+	    return PanicModeRecovery(str);
cf0dd1
+	}
cf0dd1
 
cf0dd1
 	action->idx = _XtGetQuarkIndex(parseTree, quark);
cf0dd1
 	ScanWhitespace(str);
cf0dd1
-- 
cf0dd1
2.19.2
cf0dd1