]> granicus.if.org Git - procps-ng/commitdiff
sysctl: Don't crash file fopen fails
authorCraig Small <csmall@enc.com.au>
Sun, 7 Jan 2018 01:25:35 +0000 (12:25 +1100)
committerCraig Small <csmall@enc.com.au>
Sun, 7 Jan 2018 01:31:37 +0000 (12:31 +1100)
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 58ae084c2737cdee395915d45dbcb364648ac615

NEWS
sysctl.c

diff --git a/NEWS b/NEWS
index 251a1fec1c35c39647791f157cf9df48c69fed88..7ffe74a9cc8db7d3875c55df0dc4c293d262a462 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,7 @@ procps-ng-NEXT
   * ps: recognizes SCHED_DEADLINE as valid CPU scheduler
   * ps: display NUMA node under which a thread ran         issue #58
   * 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: eliminated minor libnuma memory leak
   * top: show fewer memory decimal places (configurable)   issue #50
index 70042215ad1b95da8eb94a2b50856584e19f50c5..9356910f91fff8f3df2ea73af0f19c38757c0c45 100644 (file)
--- a/sysctl.c
+++ b/sysctl.c
@@ -220,9 +220,6 @@ static int ReadSetting(const char *restrict const name)
 
        fp = fopen(tmpname, "r");
 
-       if (iobuf)
-               setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
-
        if (!fp) {
                switch (errno) {
                case ENOENT:
@@ -244,6 +241,9 @@ static int ReadSetting(const char *restrict const name)
                        break;
                }
        } else {
+           if (iobuf)
+            setvbuf(fp, iobuf, _IOFBF, IOBUFSIZ);
+
                errno = 0;
                if (fgets(inbuf, sizeof inbuf - 1, fp)) {
                        /* this loop is required, see