From: Phil Sutter Date: Thu, 24 Aug 2017 09:46:31 +0000 (+0200) Subject: ifstat, nstat: Check fdopen() return value X-Git-Tag: v4.13.0~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d02518fdc37eb12abff67b6f8c741fbd81dce72;p=thirdparty%2Fiproute2.git ifstat, nstat: Check fdopen() return value 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 --- diff --git a/misc/ifstat.c b/misc/ifstat.c index 1be21703b..ac3eff6b8 100644 --- a/misc/ifstat.c +++ b/misc/ifstat.c @@ -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); diff --git a/misc/nstat.c b/misc/nstat.c index 1212b1f2c..a4dd405d4 100644 --- a/misc/nstat.c +++ b/misc/nstat.c @@ -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);