From patchwork Thu Aug 17 17:09:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 802747 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xYCQ06Vmhz9t4g for ; Fri, 18 Aug 2017 03:10:00 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753359AbdHQRJ5 (ORCPT ); Thu, 17 Aug 2017 13:09:57 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:59035 "EHLO mail.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753311AbdHQRJ4 (ORCPT ); Thu, 17 Aug 2017 13:09:56 -0400 Received: from mail.nwl.cc (orbyte.nwl.cc [127.0.0.1]) by mail.nwl.cc (Postfix) with ESMTP id 130F2644C0; Thu, 17 Aug 2017 19:09:55 +0200 (CEST) Received: from xsao (localhost [IPv6:::1]) by mail.nwl.cc (Postfix) with ESMTP id EB8C363864; Thu, 17 Aug 2017 19:09:54 +0200 (CEST) From: Phil Sutter To: Stephen Hemminger Cc: netdev@vger.kernel.org Subject: [iproute PATCH v2 1/5] ifstat, nstat: Check fdopen() return value Date: Thu, 17 Aug 2017 19:09:27 +0200 Message-Id: <20170817170931.24545-2-phil@nwl.cc> X-Mailer: git-send-email 2.13.1 In-Reply-To: <20170817170931.24545-1-phil@nwl.cc> References: <20170817170931.24545-1-phil@nwl.cc> X-Virus-Scanned: ClamAV using ClamSMTP Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 --- misc/ifstat.c | 16 +++++++++++----- misc/nstat.c | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/misc/ifstat.c b/misc/ifstat.c index 1be21703bf14c..ac3eff6b870a9 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 1212b1f2c8128..a4dd405d43a93 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);