Merge branch 'mpinjr-fix-ioerrors' into staging

This commit is contained in:
ozan yigit 2021-12-26 13:44:58 -05:00
commit b9c01f5122
3 changed files with 31 additions and 13 deletions

5
FIXES
View File

@ -25,6 +25,11 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987.
December 8, 2021:
The error handling in closefile and closeall was mangled. Long
standing warnings had been made fatal and some fatal errors went
undetected. Thanks to Miguel Pineiro Jr. <mpj@pineiro.cc>.
Nov 03, 2021:
getline accesses uninitialized data after getrec()
returns 0 on EOF and leaves the contents of buf unchanged.

2
main.c
View File

@ -22,7 +22,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
const char *version = "version 20211103";
const char *version = "version 20211208";
#define DEBUG
#include <stdio.h>

37
run.c
View File

@ -1860,8 +1860,8 @@ const char *filename(FILE *fp)
return "???";
}
Cell *closefile(Node **a, int n)
{
Cell *closefile(Node **a, int n)
{
Cell *x;
size_t i;
bool stat;
@ -1872,8 +1872,15 @@ const char *filename(FILE *fp)
for (i = 0; i < nfiles; i++) {
if (!files[i].fname || strcmp(x->sval, files[i].fname) != 0)
continue;
if (ferror(files[i].fp))
FATAL("i/o error occurred on %s", files[i].fname);
if (files[i].mode == GT || files[i].mode == '|')
fflush(files[i].fp);
if (ferror(files[i].fp)) {
if ((files[i].mode == GT && files[i].fp != stderr)
|| files[i].mode == '|')
FATAL("write error on %s", files[i].fname);
else
WARNING("i/o error occurred on %s", files[i].fname);
}
if (files[i].fp == stdin || files[i].fp == stdout ||
files[i].fp == stderr)
stat = freopen("/dev/null", "r+", files[i].fp) == NULL;
@ -1882,7 +1889,7 @@ const char *filename(FILE *fp)
else
stat = fclose(files[i].fp) == EOF;
if (stat)
FATAL("i/o error occurred closing %s", files[i].fname);
WARNING("i/o error occurred closing %s", files[i].fname);
if (i > 2) /* don't do /dev/std... */
xfree(files[i].fname);
files[i].fname = NULL; /* watch out for ref thru this */
@ -1893,7 +1900,7 @@ const char *filename(FILE *fp)
x = gettemp();
setfval(x, (Awkfloat) (stat ? -1 : 0));
return(x);
}
}
void closeall(void)
{
@ -1903,18 +1910,24 @@ void closeall(void)
for (i = 0; i < nfiles; i++) {
if (! files[i].fp)
continue;
if (ferror(files[i].fp))
FATAL( "i/o error occurred on %s", files[i].fname );
if (files[i].fp == stdin)
if (files[i].mode == GT || files[i].mode == '|')
fflush(files[i].fp);
if (ferror(files[i].fp)) {
if ((files[i].mode == GT && files[i].fp != stderr)
|| files[i].mode == '|')
FATAL("write error on %s", files[i].fname);
else
WARNING("i/o error occurred on %s", files[i].fname);
}
if (files[i].fp == stdin || files[i].fp == stdout ||
files[i].fp == stderr)
continue;
if (files[i].mode == '|' || files[i].mode == LE)
stat = pclose(files[i].fp) == -1;
else if (files[i].fp == stdout || files[i].fp == stderr)
stat = fflush(files[i].fp) == EOF;
else
stat = fclose(files[i].fp) == EOF;
if (stat)
FATAL( "i/o error occurred while closing %s", files[i].fname );
WARNING("i/o error occurred while closing %s", files[i].fname);
}
}