Skip to content

Commit

Permalink
ed_setup: do not return prematurely (re: e5c1a73)
Browse files Browse the repository at this point in the history
This incorrect thing jumped out when reading the code: when
ed_setup needs to fall back to malloc to allocate the output buffer
instead of using the Sfio stream buffer, it returns prematurely,
before handlign reedit (the histreedit shell option) and e_default
(the -v option to 'read'). Instead of returning, wrap the normal
way of reserving the Sfio buffer in an 'else' so that ed_setup
continues correctly either way.

To understand the else clause code, see the manual:
$ man src/lib/libast/man/sfio.3
See under sfreserve(), 'type == SFIO_LOCKR'.
  • Loading branch information
McDutchie committed Jan 9, 2025
1 parent e5c1a73 commit 84f6139
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/cmd/ksh93/edit/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,17 @@ void ed_setup(Edit_t *ep, int fd, int reedit)
buff = (char*)sh_malloc(MAXLINE);
ep->e_outbase = ep->e_outptr = buff;
ep->e_outlast = ep->e_outptr + MAXLINE - 1;
return;
}
qlen = sfset(sfstderr,SFIO_READ,0);
/* make sure SFIO_READ not on */
ep->e_outbase = ep->e_outptr = (char*)sfreserve(sfstderr,SFIO_UNBOUND,SFIO_LOCKR);
ep->e_outlast = ep->e_outptr + sfvalue(sfstderr) - 1;
if(qlen)
sfset(sfstderr,SFIO_READ,1);
sfwrite(sfstderr,ep->e_outptr,0);
else
{
qlen = sfset(sfstderr,SFIO_READ,0);
/* make sure SFIO_READ not on */
ep->e_outbase = ep->e_outptr = sfreserve(sfstderr,SFIO_UNBOUND,SFIO_LOCKR);
ep->e_outlast = ep->e_outptr + sfvalue(sfstderr) - 1;
if(qlen)
sfset(sfstderr,SFIO_READ,1);
sfwrite(sfstderr,ep->e_outptr,0);
}
ep->e_eol = reedit;
if(ep->e_default && (pp = nv_getval(ep->e_default)))
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/libast/man/sfio.3
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ The call \f3sfreserve(f, 0, SFIO_LOCKR)\fP is similar but also locks the stream.
.Tp
\f3n > 0\fP:
\f3sfreserve()\fP will use attempt to get \fIat most\fP \f3n\fP bytes into
the buffer. Further, if \f3type == 3SFIO_LOCKR\fP (see below), read attempts
the buffer. Further, if \f3type == SFIO_LOCKR\fP (see below), read attempts
end on a positive amount.

For a successful reservation, the argument \f3type\fP dictates treatment
Expand Down

0 comments on commit 84f6139

Please sign in to comment.