]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ifstat, nstat: Check fdopen() return value
authorPhil Sutter <phil@nwl.cc>
Thu, 24 Aug 2017 09:46:31 +0000 (11:46 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Thu, 24 Aug 2017 21:49:43 +0000 (14:49 -0700)
Prevent passing NULL FILE pointer to fgets() later.

Fix both tools in a single patch since the code changes are basically
identical.

Signed-off-by: Phil Sutter <phil@nwl.cc>
misc/ifstat.c
misc/nstat.c

index 1be21703bf14c2ea0a7971ec00e73d5df5545071..ac3eff6b870a9fc7f496a1e0e2ffe673b3930e48 100644 (file)
@@ -992,12 +992,18 @@ int main(int argc, char *argv[])
            && verify_forging(fd) == 0) {
                FILE *sfp = fdopen(fd, "r");
 
-               load_raw_table(sfp);
-               if (hist_db && source_mismatch) {
-                       fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
-                       hist_db = NULL;
+               if (!sfp) {
+                       fprintf(stderr, "ifstat: fdopen failed: %s\n",
+                               strerror(errno));
+                       close(fd);
+               } else  {
+                       load_raw_table(sfp);
+                       if (hist_db && source_mismatch) {
+                               fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
+                               hist_db = NULL;
+                       }
+                       fclose(sfp);
                }
-               fclose(sfp);
        } else {
                if (fd >= 0)
                        close(fd);
index 1212b1f2c81287772ace0b24d4530757e23c84f3..a4dd405d43a93e86e712475eb3b9e3e733bee839 100644 (file)
@@ -706,12 +706,18 @@ int main(int argc, char *argv[])
            && verify_forging(fd) == 0) {
                FILE *sfp = fdopen(fd, "r");
 
-               load_good_table(sfp);
-               if (hist_db && source_mismatch) {
-                       fprintf(stderr, "nstat: history is stale, ignoring it.\n");
-                       hist_db = NULL;
+               if (!sfp) {
+                       fprintf(stderr, "nstat: fdopen failed: %s\n",
+                               strerror(errno));
+                       close(fd);
+               } else {
+                       load_good_table(sfp);
+                       if (hist_db && source_mismatch) {
+                               fprintf(stderr, "nstat: history is stale, ignoring it.\n");
+                               hist_db = NULL;
+                       }
+                       fclose(sfp);
                }
-               fclose(sfp);
        } else {
                if (fd >= 0)
                        close(fd);