public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Phil Sutter <phil@nwl.cc>
Cc: netdev@vger.kernel.org
Subject: Re: [iproute PATCH v2 1/7] nstat: Avoid passing negative fd to fdopen()
Date: Mon, 21 Aug 2017 17:23:23 -0700	[thread overview]
Message-ID: <20170821172323.47c335f3@xeon-e3> (raw)
In-Reply-To: <20170821170813.29697-2-phil@nwl.cc>

On Mon, 21 Aug 2017 19:08:07 +0200
Phil Sutter <phil@nwl.cc> wrote:

> Introduce a wrapper which does the sanity checking and returns NULL
> in case fd is invalid.
> 
> Signed-off-by: Phil Sutter <phil@nwl.cc>
> ---
>  misc/nstat.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/misc/nstat.c b/misc/nstat.c
> index 1212b1f2c8128..7cdde75a56e4e 100644
> --- a/misc/nstat.c
> +++ b/misc/nstat.c
> @@ -252,9 +252,16 @@ static void load_ugly_table(FILE *fp)
>  	}
>  }
>  
> +static FILE *fdopen_null(int fd, const char *mode)
> +{
> +	if (fd < 0)
> +		return NULL;
> +	return fdopen(fd, mode);
> +}
> +
>  static void load_sctp_snmp(void)
>  {
> -	FILE *fp = fdopen(net_sctp_snmp_open(), "r");
> +	FILE *fp = fdopen_null(net_sctp_snmp_open(), "r");
>  
>  	if (fp) {
>  		load_good_table(fp);
> @@ -264,7 +271,7 @@ static void load_sctp_snmp(void)
>  
>  static void load_snmp(void)
>  {
> -	FILE *fp = fdopen(net_snmp_open(), "r");
> +	FILE *fp = fdopen_null(net_snmp_open(), "r");
>  
>  	if (fp) {
>  		load_ugly_table(fp);
> @@ -274,7 +281,7 @@ static void load_snmp(void)
>  
>  static void load_snmp6(void)
>  {
> -	FILE *fp = fdopen(net_snmp6_open(), "r");
> +	FILE *fp = fdopen_null(net_snmp6_open(), "r");
>  
>  	if (fp) {
>  		load_good_table(fp);
> @@ -284,7 +291,7 @@ static void load_snmp6(void)
>  
>  static void load_netstat(void)
>  {
> -	FILE *fp = fdopen(net_netstat_open(), "r");
> +	FILE *fp = fdopen_null(net_netstat_open(), "r");
>  
>  	if (fp) {
>  		load_ugly_table(fp);

Why not just fix it at the source of the open.
I.e 
static FILE *generic_proc_open(condt char * env, const char *name)
{
...
	return fopen(p, "r");
}

  reply	other threads:[~2017-08-22  0:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-21 17:08 [iproute PATCH v2 0/7] Covscan: Misc fixes Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 1/7] nstat: Avoid passing negative fd to fdopen() Phil Sutter
2017-08-22  0:23   ` Stephen Hemminger [this message]
2017-08-22 10:48     ` Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 2/7] ss: Make sure index variable is >= 0 Phil Sutter
2017-08-22  0:34   ` Stephen Hemminger
2017-08-21 17:08 ` [iproute PATCH v2 3/7] ss: Make sure scanned index value to unix_state_map is sane Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 4/7] netem/maketable: Check return value of fscanf() Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 5/7] lib/bpf: Check return value of write() Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 6/7] lib/fs: Fix and simplify make_path() Phil Sutter
2017-08-21 17:08 ` [iproute PATCH v2 7/7] lib/libnetlink: Don't pass NULL parameter to memcpy() Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170821172323.47c335f3@xeon-e3 \
    --to=stephen@networkplumber.org \
    --cc=netdev@vger.kernel.org \
    --cc=phil@nwl.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox