Skip to content

Commit 3f5b928

Browse files
committed
shell: pty: fix leak in shell pty plugin
Problem: The string and JSON object containing pty options is leaked in pty_getopt() in the shell pty plugin. Ensure these temporary variables are freed on exit from the function to avoid the leak.
1 parent d9ab558 commit 3f5b928

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/shell/pty.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ static int pty_getopt (flux_shell_t *shell,
155155
int *capture,
156156
int *interactive)
157157
{
158-
char *s;
158+
char *s = NULL;
159159
const char *ranks;
160160
char rbuf [21];
161-
json_t *o;
161+
json_t *o = NULL;
162+
*targets = NULL;
162163

163164
/* Only create a session for rank 0 if the pty option was specified
164165
*/
@@ -174,7 +175,7 @@ static int pty_getopt (flux_shell_t *shell,
174175

175176
if (!(o = json_loads (s, JSON_DECODE_ANY, NULL))) {
176177
shell_log_error ("Unable to parse pty shell option: %s", s);
177-
return -1;
178+
goto error;
178179
}
179180
if (json_is_object (o)) {
180181
json_error_t error;
@@ -188,7 +189,7 @@ static int pty_getopt (flux_shell_t *shell,
188189
"capture", capture,
189190
"interactive", interactive) < 0) {
190191
shell_die (1, "invalid shell pty option: %s", error.text);
191-
return -1;
192+
goto error;
192193
}
193194

194195
if (*interactive) {
@@ -223,7 +224,7 @@ static int pty_getopt (flux_shell_t *shell,
223224
}
224225
if (!(*targets = shell_taskids_intersect (shell, shell_rank, ranks))) {
225226
shell_log_error ("pty: shell_taskids_intersect");
226-
return -1;
227+
goto error;
227228
}
228229

229230
/* If interactive, then always ensure rank 0 is in the set of targets
@@ -235,7 +236,14 @@ static int pty_getopt (flux_shell_t *shell,
235236
shell_warn ("pty: adding pty to rank 0 for interactive support");
236237
idset_set (*targets, 0);
237238
}
239+
free (s);
240+
json_decref (o);
238241
return 1;
242+
error:
243+
free (s);
244+
json_decref (o);
245+
idset_destroy (*targets);
246+
return -1;
239247
}
240248

241249
static void server_empty (struct flux_terminus_server *ts, void *arg)
@@ -301,7 +309,6 @@ static int pty_init (flux_plugin_t *p,
301309
}
302310
}
303311

304-
305312
/* Create a pty session for each local target
306313
*/
307314
rank = idset_first (targets);

0 commit comments

Comments
 (0)