Skip to content

Commit 1958ecf

Browse files
committed
Fix some memory leak and add pointer checking
1 parent 9646282 commit 1958ecf

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

sdmsh_commands.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ int sdmsh_cmd_ref(struct shell_config *sc, char *argv[], int argc)
164164
}
165165

166166
stream_close(stream);
167+
stream_free(stream);
167168
free(data);
168169
sdm_set_idle_state(ss);
169170

shell.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@ int shell_handle(struct shell_config *sc)
125125
if (sc->shell_quit) {
126126
struct shell_input *si = shell_input_next(sc);
127127

128-
if (si == NULL)
128+
if (si == NULL) {
129+
if (sc->shell_input) {
130+
free(sc->shell_input);
131+
sc->shell_input = NULL;
132+
}
129133
return SHELL_EOF;
134+
}
130135

131136
/* Hack to show prompt after `source` */
132137
if (is_interactive_mode(sc)) {
@@ -140,7 +145,7 @@ int shell_handle(struct shell_config *sc)
140145

141146
cmd = strchopspaces(sc->shell_input);
142147
if (!cmd || cmd[0] == 0 || cmd[0] == '#')
143-
return 0;
148+
goto shell_handle_free;
144149

145150
sc->sig_cancel = 0;
146151
shell_add_history(shell_config, cmd);
@@ -152,13 +157,14 @@ int shell_handle(struct shell_config *sc)
152157
rc = shell_input_add(sc, SHELL_INPUT_PUT_HEAD|SHELL_INPUT_TYPE_STRING, cmd);
153158
if (rc == -1) {
154159
fprintf(stderr, "Error open file /dev/zero\n");
155-
return -1;
160+
goto shell_handle_free;
156161
} else if (rc == -2) {
157162
fprintf(stderr, "Too many inputs. Maximum %d", SHELL_MAX_INPUT);
158-
return -1;
163+
rc = -1;
164+
goto shell_handle_free;
159165
}
160166
shell_input_init_current(sc);
161-
return 0;
167+
goto shell_handle_free;
162168
}
163169
rc = shell_run_cmd(sc, cmd);
164170

@@ -179,6 +185,7 @@ int shell_handle(struct shell_config *sc)
179185
}
180186
}
181187

188+
shell_handle_free:
182189
free(sc->shell_input);
183190
sc->shell_input = NULL;
184191

@@ -229,6 +236,7 @@ int shell_run_cmd(struct shell_config *sc, char *shell_input)
229236

230237
if (shell_make_argv(cmd_line, &argv, &argc) == -1) {
231238
fprintf(stderr, "\rCommand syntax error: \"%s\"\n", cmd_line);
239+
free(cmd_line);
232240
return -1;
233241
}
234242

@@ -343,6 +351,9 @@ int shell_input_add(struct shell_config *sc, int flags, ...)
343351
return -2;
344352

345353
si = malloc(sizeof(struct shell_input));
354+
if (!si)
355+
return -2;
356+
346357
if (flags & SHELL_INPUT_PUT_HEAD)
347358
STAILQ_INSERT_HEAD(&sc->inputs_list, si, next_input);
348359
else

shell_help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ void shell_help_show(struct shell_config *sc, char *name)
1818
struct commands_t *cmd;
1919
for (cmd = sc->commands; cmd->name != NULL; cmd++) {
2020
if (!name || !strcmp(cmd->name, name)) {
21-
printf ("%-10s-\t%s\n", cmd->name, cmd->help);
22-
printf ("%-10s \tUsage: %s\n", " ", cmd->usage ? cmd->usage : cmd->name);
21+
printf ("%-10s - %s\n", cmd->name, cmd->help);
22+
printf ("%-10s Usage: %s\n", " ", cmd->usage ? cmd->usage : cmd->name);
2323
if (name) {
2424
if (cmd->flags & SCF_USE_DRIVER)
2525
shell_help_show_drivers(sc, "\t\t");

0 commit comments

Comments
 (0)