summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Wiederhake <twied@gmx.net>2024-03-16 13:07:19 +0100
committerTim Wiederhake <twied@gmx.net>2024-03-16 13:07:19 +0100
commite60c2914c354d4725d170e4fe4f14456caccd3cc (patch)
tree748da23f35488aafa97b88ca57119d1217f71ff2
parent79b0987e63daff97ce9ee1dce883df13668a80c6 (diff)
Fix memory leak in SaveYourselfPhase2CB
Valgrind results before: definitely lost: 981 bytes in 37 blocks indirectly lost: 872 bytes in 6 blocks possibly lost: 0 bytes in 0 blocks still reachable: 991,316 bytes in 1,795 blocks suppressed: 0 bytes in 0 blocks Valgrind results after: definitely lost: 800 bytes in 23 blocks indirectly lost: 872 bytes in 6 blocks possibly lost: 0 bytes in 0 blocks still reachable: 991,316 bytes in 1,795 blocks suppressed: 0 bytes in 0 blocks Signed-off-by: Tim Wiederhake <twied@gmx.net> Part-of: <https://gitlab.freedesktop.org/xorg/app/twm/-/merge_requests/29>
-rw-r--r--src/session.c116
-rw-r--r--src/session.h1
-rw-r--r--src/twm.c2
3 files changed, 65 insertions, 54 deletions
diff --git a/src/session.c b/src/session.c
index 92b25bb..af2b437 100644
--- a/src/session.c
+++ b/src/session.c
@@ -67,6 +67,7 @@ static XtInputId iceInputId;
static char *twm_clientId;
static TWMWinConfigEntry *winConfigHead = NULL;
static Bool sent_save_done = 0;
+static SmProp *props[5];
#define SAVEFILE_VERSION 2
@@ -676,8 +677,6 @@ SaveYourselfPhase2CB(SmcConn smcConn2, SmPointer clientData _X_UNUSED)
const char *path;
char *filename = NULL;
Bool success = False;
- SmProp prop1, prop2, prop3, *props[3];
- SmPropValue prop1val, prop2val, prop3val;
char discardCommand[80];
int numVals, i;
static int first_time = 1;
@@ -688,36 +687,38 @@ SaveYourselfPhase2CB(SmcConn smcConn2, SmPointer clientData _X_UNUSED)
if (first_time) {
char userId[20];
- char hint = SmRestartIfRunning;
+ char *hint;
- prop1.name = strdup(SmProgram);
- prop1.type = strdup(SmARRAY8);
- prop1.num_vals = 1;
- prop1.vals = &prop1val;
- prop1val.value = Argv[0];
- prop1val.length = (int) strlen(Argv[0]);
+ props[0] = (SmProp *) calloc(1, sizeof(SmProp));
+ props[0]->name = strdup(SmProgram);
+ props[0]->type = strdup(SmARRAY8);
+ props[0]->num_vals = 1;
+ props[0]->vals = (SmPropValue *) calloc(1, sizeof(SmPropValue));
+ props[0]->vals[0].value = strdup(Argv[0]);
+ props[0]->vals[0].length = (int) strlen(Argv[0]);
snprintf(userId, sizeof(userId), "%ld", (long) getuid());
- prop2.name = strdup(SmUserID);
- prop2.type = strdup(SmARRAY8);
- prop2.num_vals = 1;
- prop2.vals = &prop2val;
- prop2val.value = (SmPointer) userId;
- prop2val.length = (int) strlen(userId);
-
- prop3.name = strdup(SmRestartStyleHint);
- prop3.type = strdup(SmCARD8);
- prop3.num_vals = 1;
- prop3.vals = &prop3val;
- prop3val.value = (SmPointer) & hint;
- prop3val.length = 1;
-
- props[0] = &prop1;
- props[1] = &prop2;
- props[2] = &prop3;
- SmcSetProperties(smcConn2, 3, props);
+ props[1] = (SmProp *) calloc(1, sizeof(SmProp));
+ props[1]->name = strdup(SmUserID);
+ props[1]->type = strdup(SmARRAY8);
+ props[1]->num_vals = 1;
+ props[1]->vals = (SmPropValue *) calloc(1, sizeof(SmPropValue));
+ props[1]->vals[0].value = strdup(userId);
+ props[1]->vals[0].length = (int) strlen(userId);
+
+ hint = (char *) malloc(1);
+ hint[0] = SmRestartIfRunning;
+
+ props[2] = (SmProp *) calloc(1, sizeof(SmProp));
+ props[2]->name = strdup(SmRestartStyleHint);
+ props[2]->type = strdup(SmCARD8);
+ props[2]->num_vals = 1;
+ props[2]->vals = (SmPropValue *) calloc(1, sizeof(SmPropValue));
+ props[2]->vals[0].value = hint;
+ props[2]->vals[0].length = 1;
+ SmcSetProperties(smcConn2, 3, props);
first_time = 0;
}
@@ -770,13 +771,12 @@ SaveYourselfPhase2CB(SmcConn smcConn2, SmPointer clientData _X_UNUSED)
}
}
- prop1.name = strdup(SmRestartCommand);
- prop1.type = strdup(SmLISTofARRAY8);
-
- prop1.vals = (SmPropValue *)
- malloc((size_t) (Argc + 4) * sizeof(SmPropValue));
+ props[3] = (SmProp *) calloc(1, sizeof(SmProp));
+ props[3]->name = strdup(SmRestartCommand);
+ props[3]->type = strdup(SmLISTofARRAY8);
+ props[3]->vals = (SmPropValue *) calloc(Argc + 4, sizeof(SmPropValue));
- if (!prop1.vals) {
+ if (!props[3]->vals) {
success = False;
goto bad;
}
@@ -789,38 +789,36 @@ SaveYourselfPhase2CB(SmcConn smcConn2, SmPointer clientData _X_UNUSED)
i++;
}
else {
- prop1.vals[numVals].value = (SmPointer) Argv[i];
- prop1.vals[numVals++].length = (int) strlen(Argv[i]);
+ props[3]->vals[numVals].value = (SmPointer) strdup(Argv[i]);
+ props[3]->vals[numVals++].length = (int) strlen(Argv[i]);
}
}
- prop1.vals[numVals].value = strdup("-clientId");
- prop1.vals[numVals++].length = 9;
+ props[3]->vals[numVals].value = strdup("-clientId");
+ props[3]->vals[numVals++].length = 9;
- prop1.vals[numVals].value = strdup(twm_clientId);
- prop1.vals[numVals++].length = (int) strlen(twm_clientId);
+ props[3]->vals[numVals].value = strdup(twm_clientId);
+ props[3]->vals[numVals++].length = (int) strlen(twm_clientId);
- prop1.vals[numVals].value = strdup("-restore");
- prop1.vals[numVals++].length = 8;
+ props[3]->vals[numVals].value = strdup("-restore");
+ props[3]->vals[numVals++].length = 8;
- prop1.vals[numVals].value = strdup(filename);
- prop1.vals[numVals++].length = (int) strlen(filename);
+ props[3]->vals[numVals].value = strdup(filename);
+ props[3]->vals[numVals++].length = (int) strlen(filename);
- prop1.num_vals = numVals;
+ props[3]->num_vals = numVals;
snprintf(discardCommand, sizeof(discardCommand), "rm %s", filename);
- prop2.name = strdup(SmDiscardCommand);
- prop2.type = strdup(SmARRAY8);
- prop2.num_vals = 1;
- prop2.vals = &prop2val;
- prop2val.value = (SmPointer) discardCommand;
- prop2val.length = (int) strlen(discardCommand);
- props[0] = &prop1;
- props[1] = &prop2;
+ props[4] = (SmProp *) calloc(1, sizeof(SmProp));
+ props[4]->name = strdup(SmDiscardCommand);
+ props[4]->type = strdup(SmARRAY8);
+ props[4]->num_vals = 1;
+ props[4]->vals = (SmPropValue *) calloc(1, sizeof(SmPropValue));
+ props[4]->vals[0].value = strdup(discardCommand);
+ props[4]->vals[0].length = (int) strlen(discardCommand);
- SmcSetProperties(smcConn2, 2, props);
- free(prop1.vals);
+ SmcSetProperties(smcConn2, 2, props + 3);
bad:
SmcSaveYourselfDone(smcConn2, success);
@@ -921,3 +919,13 @@ ConnectToSessionManager(char *previous_id, XtAppContext appContext)
(XtPointer) XtInputReadMask,
ProcessIceMsgProc, (XtPointer) iceConn);
}
+
+void
+DestroySession(void)
+{
+ for (int i = 0; i < 5; ++i) {
+ if (props[i]) {
+ SmFreeProperty(props[i]);
+ }
+ }
+}
diff --git a/src/session.h b/src/session.h
index df91469..489ec0e 100644
--- a/src/session.h
+++ b/src/session.h
@@ -38,6 +38,7 @@ extern int GetWindowConfig(TwmWindow *theWindow, short *x, short *y,
Bool *width_ever_changed_by_user,
Bool *height_ever_changed_by_user);
extern void ReadWinConfigFile(char *filename);
+extern void DestroySession(void);
extern SmcConn smcConn;
diff --git a/src/twm.c b/src/twm.c
index 4be177c..1221bf8 100644
--- a/src/twm.c
+++ b/src/twm.c
@@ -905,6 +905,8 @@ Done(XtPointer client_data _X_UNUSED, XtSignalId *si2 _X_UNUSED)
Reborder(CurrentTime);
XCloseDisplay(dpy);
}
+
+ DestroySession();
exit(EXIT_SUCCESS);
}