sysctl: Don't crash file fopen fails

The commit referenced below put a setvbuf() before checking what
fopen() returned. If the file could not be opened then the file
handle was NULL at setvbuf() crashed.

setvbuf() is now called after checking what fopen() returns and only
when it was successful.

References:
 procps-ng/procps#76
 commit 58ae084c27
This commit is contained in:
Craig Small 2018-01-07 12:25:35 +11:00
parent cde5cf5bab
commit 272c5b0f8c
2 changed files with 4 additions and 3 deletions

1
NEWS
View File

@ -11,6 +11,7 @@ procps-ng-NEXT
* ps: recognizes SCHED_DEADLINE as valid CPU scheduler * ps: recognizes SCHED_DEADLINE as valid CPU scheduler
* ps: display NUMA node under which a thread ran issue #58 * ps: display NUMA node under which a thread ran issue #58
* sysctl: Permit empty string for value issue #74 * sysctl: Permit empty string for value issue #74
* sysctl: Don't segv when file not available issue #76
* top: add config file support for XDG specification * top: add config file support for XDG specification
* top: eliminated minor libnuma memory leak * top: eliminated minor libnuma memory leak
* top: show fewer memory decimal places (configurable) issue #50 * top: show fewer memory decimal places (configurable) issue #50

View File

@ -222,9 +222,6 @@ static int ReadSetting(const char *restrict const name)
fp = fopen(tmpname, "r"); fp = fopen(tmpname, "r");
if (iobuf)
setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
if (!fp) { if (!fp) {
switch (errno) { switch (errno) {
case ENOENT: case ENOENT:
@ -246,6 +243,9 @@ static int ReadSetting(const char *restrict const name)
break; break;
} }
} else { } else {
if (iobuf)
setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
errno = 0; errno = 0;
if (fgets(inbuf, sizeof inbuf - 1, fp)) { if (fgets(inbuf, sizeof inbuf - 1, fp)) {
/* this loop is required, see /* this loop is required, see