Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 7f47470

Browse files
author
patacongo
committed
Correct a memory leak in NSH
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5600 42af7a65-404d-4744-a932-0658087f49c3
1 parent cca7618 commit 7f47470

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

builtin/exec_builtin.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
140140

141141
/* Initialize attributes for task_spawn(). */
142142

143-
ret = posix_spawn_file_actions_init(&file_actions);
143+
ret = posix_spawnattr_init(&attr);
144144
if (ret != 0)
145145
{
146146
goto errout_with_errno;
147147
}
148148

149-
ret = posix_spawnattr_init(&attr);
149+
ret = posix_spawn_file_actions_init(&file_actions);
150150
if (ret != 0)
151151
{
152-
goto errout_with_errno;
152+
goto errout_with_attrs;
153153
}
154154

155155
/* Set the correct task size and priority */
@@ -158,13 +158,13 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
158158
ret = posix_spawnattr_setschedparam(&attr, &param);
159159
if (ret != 0)
160160
{
161-
goto errout_with_errno;
161+
goto errout_with_actions;
162162
}
163163

164164
ret = task_spawnattr_setstacksize(&attr, builtin->stacksize);
165165
if (ret != 0)
166166
{
167-
goto errout_with_errno;
167+
goto errout_with_actions;
168168
}
169169

170170
/* If robin robin scheduling is enabled, then set the scheduling policy
@@ -175,21 +175,21 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
175175
ret = posix_spawnattr_setschedpolicy(&attr, SCHED_RR);
176176
if (ret != 0)
177177
{
178-
goto errout_with_errno;
178+
goto errout_with_actions;
179179
}
180180

181181
ret = posix_spawnattr_setflags(&attr,
182182
POSIX_SPAWN_SETSCHEDPARAM |
183183
POSIX_SPAWN_SETSCHEDULER);
184184
if (ret != 0)
185185
{
186-
goto errout_with_errno;
186+
goto errout_with_actions;
187187
}
188188
#else
189189
ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDPARAM);
190190
if (ret != 0)
191191
{
192-
goto errout_with_errno;
192+
goto errout_with_actions;
193193
}
194194
#endif
195195

@@ -204,7 +204,7 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
204204
if (ret != 0)
205205
{
206206
sdbg("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
207-
goto errout_with_errno;
207+
goto errout_with_actions;
208208
}
209209
}
210210

@@ -216,16 +216,28 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
216216
if (ret != 0)
217217
{
218218
sdbg("ERROR: task_spawn failed: %d\n", ret);
219-
goto errout_with_errno;
219+
goto errout_with_actions;
220220
}
221221

222+
/* Free attibutes and file actions. Ignoring return values in the case
223+
* of an error.
224+
*/
225+
222226
/* Return the task ID of the new task if the task was sucessfully
223227
* started. Otherwise, ret will be ERROR (and the errno value will
224228
* be set appropriately).
225229
*/
226230

231+
(void)posix_spawn_file_actions_destroy(&file_actions);
232+
(void)posix_spawnattr_destroy(&attr);
227233
return pid;
228234

235+
errout_with_actions:
236+
(void)posix_spawn_file_actions_destroy(&file_actions);
237+
238+
errout_with_attrs:
239+
(void)posix_spawnattr_destroy(&attr);
240+
229241
errout_with_errno:
230242
set_errno(ret);
231243
return ERROR;

nshlib/nsh_parse.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,13 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline)
14241424
* successfully). So certainly it is not an NSH command.
14251425
*/
14261426

1427+
/* Free the redirected output file path */
1428+
1429+
nsh_freefullpath(redirfile);
1430+
redirfile = NULL;
1431+
1432+
/* Save the result: success if 0; failure if 1 */
1433+
14271434
return nsh_saveresult(vtbl, ret != OK);
14281435
}
14291436

@@ -1458,6 +1465,13 @@ int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline)
14581465
* successfully). So certainly it is not an NSH command.
14591466
*/
14601467

1468+
/* Free the redirected output file path */
1469+
1470+
nsh_freefullpath(redirfile);
1471+
redirfile = NULL;
1472+
1473+
/* Save the result: success if 0; failure if 1 */
1474+
14611475
return nsh_saveresult(vtbl, ret != OK);
14621476
}
14631477

0 commit comments

Comments
 (0)