Skip to content

Commit b8110e9

Browse files
committed
Li'l bug fixes
1 parent ec430cb commit b8110e9

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

readline.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,20 @@ static char *emptyprompt = "";
297297

298298
PRIM(readline) {
299299
char *line;
300-
/* TODO: estrdup? */
301-
char *prompt = (list == NULL ? emptyprompt : strdup(getstr(list->term)));
300+
char *pr0 = getstr(list->term);
301+
char *prompt = emptyprompt;
302302
int old = dup(0), in = fdmap(0);
303-
if (dup2(in, 0) == -1)
303+
304+
if (list != NULL) {
305+
size_t psize = strlen(pr0) * sizeof(char) + 1;
306+
prompt = ealloc(psize);
307+
memcpy(prompt, pr0, psize);
308+
}
309+
if (dup2(in, 0) == -1) {
310+
if (prompt != emptyprompt)
311+
efree(prompt);
304312
fail("$&readline", "dup2: %s", esstrerror(errno));
313+
}
305314

306315
ExceptionHandler
307316

@@ -311,16 +320,16 @@ PRIM(readline) {
311320

312321
CatchException (e)
313322

314-
mvfd(old, 0);
315323
if (prompt != emptyprompt)
316324
efree(prompt);
325+
mvfd(old, 0);
317326
throw(e);
318327

319328
EndExceptionHandler
320329

321-
mvfd(old, 0);
322330
if (prompt != emptyprompt)
323331
efree(prompt);
332+
mvfd(old, 0);
324333

325334
if (line == NULL)
326335
return NULL;

0 commit comments

Comments
 (0)