Fix some error-handling logic so that ferror is called before fclose,
instead of immediately after the fclose. The previous logic did work on freebsd, but is somewhat risky practice (and causes trouble when porting to other OS's). PR: bin/22965 Reviewed by: Garrett Wollman
This commit is contained in:
parent
13dd9ba9c4
commit
6d94e37cf9
@ -647,7 +647,7 @@ cgetnext(bp, db_array)
|
||||
char **db_array;
|
||||
{
|
||||
size_t len;
|
||||
int status, i, done;
|
||||
int done, hadreaderr, i, savederrno, status;
|
||||
char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
|
||||
u_int dummy;
|
||||
|
||||
@ -665,9 +665,14 @@ cgetnext(bp, db_array)
|
||||
} else {
|
||||
line = fgetln(pfp, &len);
|
||||
if (line == NULL && pfp) {
|
||||
(void)fclose(pfp);
|
||||
if (ferror(pfp)) {
|
||||
(void)cgetclose();
|
||||
hadreaderr = ferror(pfp);
|
||||
if (hadreaderr)
|
||||
savederrno = errno;
|
||||
fclose(pfp);
|
||||
pfp = NULL;
|
||||
if (hadreaderr) {
|
||||
cgetclose();
|
||||
errno = savederrno;
|
||||
return (-1);
|
||||
} else {
|
||||
if (*++dbp == NULL) {
|
||||
@ -724,9 +729,18 @@ cgetnext(bp, db_array)
|
||||
} else { /* name field extends beyond the line */
|
||||
line = fgetln(pfp, &len);
|
||||
if (line == NULL && pfp) {
|
||||
(void)fclose(pfp);
|
||||
if (ferror(pfp)) {
|
||||
(void)cgetclose();
|
||||
/* Name extends beyond the EOF! */
|
||||
hadreaderr = ferror(pfp);
|
||||
if (hadreaderr)
|
||||
savederrno = errno;
|
||||
fclose(pfp);
|
||||
pfp = NULL;
|
||||
if (hadreaderr) {
|
||||
cgetclose();
|
||||
errno = savederrno;
|
||||
return (-1);
|
||||
} else {
|
||||
cgetclose();
|
||||
return (-1);
|
||||
}
|
||||
} else
|
||||
|
Loading…
Reference in New Issue
Block a user