Skip to content

Commit e8695d5

Browse files
committed
Use a pointer to end of buffer instead of tracking space left.
Fixes a problem in feedback mode where an initial backspace would reduce the effective buffer size. GitHub issue #439
1 parent 627ae4b commit e8695d5

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/tgetpass.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,22 @@ static char *
379379
getln(int fd, char *buf, size_t bufsiz, bool feedback,
380380
enum tgetpass_errval *errval)
381381
{
382-
size_t left = bufsiz;
383382
ssize_t nr = -1;
383+
const char *ep;
384384
char *cp = buf;
385385
char c = '\0';
386386
debug_decl(getln, SUDO_DEBUG_CONV);
387387

388388
*errval = TGP_ERRVAL_NOERROR;
389389

390-
if (left == 0) {
390+
if (bufsiz == 0) {
391391
*errval = TGP_ERRVAL_READERROR;
392392
errno = EINVAL;
393393
debug_return_str(NULL);
394394
}
395+
ep = buf + bufsiz - 1; /* reserve space for NUL byte */
395396

396-
while (--left) {
397+
while (cp < ep) {
397398
nr = read(fd, &c, 1);
398399
if (nr != 1 || c == '\n' || c == '\r')
399400
break;
@@ -408,13 +409,11 @@ getln(int fd, char *buf, size_t bufsiz, bool feedback,
408409
cp--;
409410
}
410411
cp = buf;
411-
left = bufsiz;
412412
continue;
413413
} else if (c == sudo_term_erase) {
414414
if (cp > buf) {
415415
ignore_result(write(fd, "\b \b", 3));
416416
cp--;
417-
left++;
418417
}
419418
continue;
420419
}
@@ -428,7 +427,7 @@ getln(int fd, char *buf, size_t bufsiz, bool feedback,
428427
while (cp > buf) {
429428
if (write(fd, "\b \b", 3) != 3)
430429
break;
431-
--cp;
430+
cp--;
432431
}
433432
}
434433

@@ -444,7 +443,7 @@ getln(int fd, char *buf, size_t bufsiz, bool feedback,
444443
debug_return_str(NULL);
445444
case 0:
446445
/* EOF is only an error if no bytes were read. */
447-
if (left == bufsiz - 1) {
446+
if (buf[0] == '\0') {
448447
*errval = TGP_ERRVAL_NOPASSWORD;
449448
debug_return_str(NULL);
450449
}

0 commit comments

Comments
 (0)