@@ -125,8 +125,13 @@ int shell_handle(struct shell_config *sc)
125
125
if (sc -> shell_quit ) {
126
126
struct shell_input * si = shell_input_next (sc );
127
127
128
- if (si == NULL )
128
+ if (si == NULL ) {
129
+ if (sc -> shell_input ) {
130
+ free (sc -> shell_input );
131
+ sc -> shell_input = NULL ;
132
+ }
129
133
return SHELL_EOF ;
134
+ }
130
135
131
136
/* Hack to show prompt after `source` */
132
137
if (is_interactive_mode (sc )) {
@@ -140,7 +145,7 @@ int shell_handle(struct shell_config *sc)
140
145
141
146
cmd = strchopspaces (sc -> shell_input );
142
147
if (!cmd || cmd [0 ] == 0 || cmd [0 ] == '#' )
143
- return 0 ;
148
+ goto shell_handle_free ;
144
149
145
150
sc -> sig_cancel = 0 ;
146
151
shell_add_history (shell_config , cmd );
@@ -152,13 +157,14 @@ int shell_handle(struct shell_config *sc)
152
157
rc = shell_input_add (sc , SHELL_INPUT_PUT_HEAD |SHELL_INPUT_TYPE_STRING , cmd );
153
158
if (rc == -1 ) {
154
159
fprintf (stderr , "Error open file /dev/zero\n" );
155
- return -1 ;
160
+ goto shell_handle_free ;
156
161
} else if (rc == -2 ) {
157
162
fprintf (stderr , "Too many inputs. Maximum %d" , SHELL_MAX_INPUT );
158
- return -1 ;
163
+ rc = -1 ;
164
+ goto shell_handle_free ;
159
165
}
160
166
shell_input_init_current (sc );
161
- return 0 ;
167
+ goto shell_handle_free ;
162
168
}
163
169
rc = shell_run_cmd (sc , cmd );
164
170
@@ -179,6 +185,7 @@ int shell_handle(struct shell_config *sc)
179
185
}
180
186
}
181
187
188
+ shell_handle_free :
182
189
free (sc -> shell_input );
183
190
sc -> shell_input = NULL ;
184
191
@@ -229,6 +236,7 @@ int shell_run_cmd(struct shell_config *sc, char *shell_input)
229
236
230
237
if (shell_make_argv (cmd_line , & argv , & argc ) == -1 ) {
231
238
fprintf (stderr , "\rCommand syntax error: \"%s\"\n" , cmd_line );
239
+ free (cmd_line );
232
240
return -1 ;
233
241
}
234
242
@@ -343,6 +351,9 @@ int shell_input_add(struct shell_config *sc, int flags, ...)
343
351
return -2 ;
344
352
345
353
si = malloc (sizeof (struct shell_input ));
354
+ if (!si )
355
+ return -2 ;
356
+
346
357
if (flags & SHELL_INPUT_PUT_HEAD )
347
358
STAILQ_INSERT_HEAD (& sc -> inputs_list , si , next_input );
348
359
else
0 commit comments