2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-30 13:58:05 +00:00

Some getdelim(3) implementations write a NUL to the buffer on EOF.

AIX and Illumos appear to have this behavior.  We now preserve the
first character of the buffer on EOF to work around this.
Fixes reporting of syntax errors on the last line of a file.
This commit is contained in:
Todd C. Miller 2021-02-09 09:06:17 -07:00
parent 1412695a4a
commit cf18011466
2 changed files with 28 additions and 4 deletions

View File

@ -5475,11 +5475,23 @@ sudoers_input(char *buf, yy_size_t max_size)
/* Refill line buffer if needed. */
if (avail == 0) {
char ch = sudolinebuf.len ? *sudolinebuf.buf : '\0';
avail = getdelim(&sudolinebuf.buf, &sudolinebuf.size, '\n', sudoersin);
if (avail == (size_t)-1) {
/* EOF or error. */
if (ferror(sudoersin) && errno != EINTR)
YY_FATAL_ERROR("input in flex scanner failed");
if (feof(sudoersin)) {
/*
* Some getdelim(3) implementations write NUL to buf on EOF.
* Restore the first character so we can report syntax errors
* on the last line.
*/
if (sudolinebuf.len != 0)
*sudolinebuf.buf = ch;
} else {
/* This matches the default YY_INPUT behavior. */
if (errno != EINTR)
YY_FATAL_ERROR("input in flex scanner failed");
}
return 0;
}

View File

@ -1280,11 +1280,23 @@ sudoers_input(char *buf, yy_size_t max_size)
/* Refill line buffer if needed. */
if (avail == 0) {
char ch = sudolinebuf.len ? *sudolinebuf.buf : '\0';
avail = getdelim(&sudolinebuf.buf, &sudolinebuf.size, '\n', sudoersin);
if (avail == (size_t)-1) {
/* EOF or error. */
if (ferror(sudoersin) && errno != EINTR)
YY_FATAL_ERROR("input in flex scanner failed");
if (feof(sudoersin)) {
/*
* Some getdelim(3) implementations write NUL to buf on EOF.
* Restore the first character so we can report syntax errors
* on the last line.
*/
if (sudolinebuf.len != 0)
*sudolinebuf.buf = ch;
} else {
/* This matches the default YY_INPUT behavior. */
if (errno != EINTR)
YY_FATAL_ERROR("input in flex scanner failed");
}
return 0;
}