public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: Laurent Vivier <lvivier@redhat.com>, passt-dev@passt.top
Subject: Re: [PATCH 3/3] fwd, conf: Probe host's ephemeral ports
Date: Thu, 29 Aug 2024 04:59:55 +0200	[thread overview]
Message-ID: <20240829045955.7f83a9e5@elisabeth> (raw)
In-Reply-To: <Zs_O-uO8-59921GR@zatzit.fritz.box>

On Thu, 29 Aug 2024 11:29:30 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Wed, Aug 28, 2024 at 12:22:18PM +0200, Laurent Vivier wrote:
> > On 28/08/2024 07:56, David Gibson wrote:  
> > >
> > > [...]
> > >
> > > +	lineread_init(&lr, fd);
> > > +	len = lineread_get(&lr, &line);
> > > +	if (len < 0)
> > > +		goto parse_err;
> > > +
> > > +	tab = strchr(line, '\t');
> > > +	if (!tab)
> > > +		goto parse_err;
> > > +	*tab = '\0';
> > > +
> > > +	errno = 0;
> > > +	min = strtol(line, &end, 10);
> > > +	if (*end || errno)
> > > +		goto parse_err;
> > > +
> > > +	errno = 0;
> > > +	max = strtol(tab + 1, &end, 10);
> > > +	if (*end || errno)
> > > +		goto parse_err;  
> > 
> > As /proc files are well formated, why don't you use fscanf()?
> > Something like:
> > 
> >         FILE *f;
> > 
> >         f = fopen(PORT_RANGE_SYSCTL, "r");
> > 	if (f == NULL) {
> > 		warn("Unable to parse %s", PORT_RANGE_SYSCTL);
> > 		return;
> > 	}
> >         ret = fscanf(f, "%d %d", &min, &max);
> >         fclose(f);
> >         if (ret != 2)
> >                 goto parse_error;  
> 
> Hm, maybe.  I never feel like I know exactly what the parse rules for
> scanf() are, so I tend to avoid it.  Stefano, any thoughts?

The issue with fopen() and ffscanf() is that they need dynamic
allocation, see also 32d07f5e59f2 ("passt, pasta: Completely avoid
dynamic memory allocation").

Still, here, we could use lineread_get() and sscanf() as we do in fwd.c,
procfs_scan_listen(), but I don't really have a preference. If you find
that scanf() is confusing for you, perhaps it would be better to use
something simpler as you did. On the other hand it would be 2 lines of
code instead of (somewhat bug-prone) 14 lines.

I'm not sure, I'd say whatever you feel most comfortable with...

-- 
Stefano


  reply	other threads:[~2024-08-29  3:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-28  5:56 [PATCH 0/3] Probe host's ephemeral ports, rather than using RFC values David Gibson
2024-08-28  5:56 ` [PATCH 1/3] conf, fwd: Make ephemeral port logic more flexible David Gibson
2024-08-28 10:01   ` Laurent Vivier
2024-08-28  5:56 ` [PATCH 2/3] conf, fwd: Don't attempt to forward port 0 David Gibson
2024-08-28 10:03   ` Laurent Vivier
2024-08-28  5:56 ` [PATCH 3/3] fwd, conf: Probe host's ephemeral ports David Gibson
2024-08-28 10:22   ` Laurent Vivier
2024-08-29  1:29     ` David Gibson
2024-08-29  2:59       ` Stefano Brivio [this message]
2024-08-29  4:14         ` David Gibson

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=20240829045955.7f83a9e5@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).