From e60c2914c354d4725d170e4fe4f14456caccd3cc Mon Sep 17 00:00:00 2001 From: Tim Wiederhake Date: Sat, 16 Mar 2024 13:07:19 +0100 Subject: 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 Part-of: --- src/session.c | 116 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 54 deletions(-) (limited to 'src/session.c') 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]); + } + } +} -- cgit v1.2.3