netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: Phil Sutter <phil@nwl.cc>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>,
	NetFilter <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH nft 3/4] all: add free_const() and use it instead of xfree()
Date: Wed, 20 Sep 2023 21:48:40 +0200	[thread overview]
Message-ID: <f56d6b485b7154c8b8c24765ced9d222eabbd211.camel@redhat.com> (raw)
In-Reply-To: <ZQs4eu74k86+7FK0@orbyte.nwl.cc>

On Wed, 2023-09-20 at 20:22 +0200, Phil Sutter wrote:
> On Wed, Sep 20, 2023 at 08:03:17PM +0200, Thomas Haller wrote:
> > On Wed, 2023-09-20 at 18:49 +0200, Phil Sutter wrote:
> > > On Wed, Sep 20, 2023 at 06:06:23PM +0200, Pablo Neira Ayuso
> > > wrote:
> > > > On Wed, Sep 20, 2023 at 04:13:43PM +0200, Phil Sutter wrote:
> > > > > On Wed, Sep 20, 2023 at 03:13:40PM +0200, Thomas Haller
> > > > > wrote:
> > > > > [...]
> > > > > > There are many places that rightly cast away const during
> > > > > > free.
> > > > > > But not
> > > > > > all of them. Add a free_const() macro, which is like
> > > > > > free(),
> > > > > > but accepts
> > > > > > const pointers. We should always make an intentional choice
> > > > > > whether to
> > > > > > use free() or free_const(). Having a free_const() macro
> > > > > > makes
> > > > > > this very
> > > > > > common choice clearer, instead of adding a (void*) cast at
> > > > > > many
> > > > > > places.
> > > > > 
> > > > > I wonder whether pointers to allocated data should be const
> > > > > in
> > > > > the first
> > > > > place. Maybe I miss the point here? Looking at flow offload
> > > > > statement
> > > > > for instance, should 'table_name' not be 'char *' instead of
> > > > > using this
> > > > > free_const() to free it?
> > > > 
> > > > The const here tells us that this string is set once and it
> > > > gets
> > > > never
> > > > updated again, which provides useful information when reading
> > > > the
> > > > code IMO.
> > > 
> > > That seems like reasonable rationale. I like to declare function
> > > arguments as const too in order to mark them as not being altered
> > > by
> > > the
> > > function.
> > > 
> > > With strings, I find it odd to do:
> > > 
> > > const char *buf = strdup("foo");
> > > free((void *)buf);
> > > 
> > > > I interpret from Phil's words that it would be better to
> > > > consolidate
> > > > this to have one single free call, in that direction, I agree.
> > > 
> > > No, I was just wondering why we have this need for free_const()
> > > in
> > > the
> > > first place (i.e., why we declare pointers as const if we
> > > allocate/free
> > > them).
> > 
> > 
> > I think that we use free_const() is correct.
> > 
> > 
> > Look at "struct datatype", which are either immutable global
> > instances,
> > or heap allocated (and ref-counted). For the most part, we want to
> > treat these instances (both constant and allocated) as immutable,
> > and
> > the "const" specifier expresses that well.
> 
> So why doesn't datatype_get() return a const pointer then?

Good point.

Also compare with 

  char *strchr(const char *s, int c);

where it makes sense.

For datatype_get() it makes less sense. I will send a patch.


>  I don't find
> struct datatype a particularly good example here: datatype_free()
> does
> not require free_const() at all.

datatype_free() in the patch uses+requires free_const() twice:

»·······free_const(dtype->name);
»·······free_const(dtype->desc);
»·······free(dtype);


Maybe instead it should do:


void datatype_free(const struct datatype *dtype)
{
»·······if (!dtype)
»·······»·······return; 
»·······if (!(dtype->flags & DTYPE_F_ALLOC))
»·······»·······return;

»·······assert(dtype->refcnt != 0);

»·······if (--((struct datatype *)dtype)->refcnt > 0)
»·······»·······return;

»·······free_const(dtype->name);
»·······free_const(dtype->desc);
»·······free_const(dtype);
}

but the principle is still the same.

> 
> BTW: I found two lines in src/netlink.c reading:
> 
> > datatype_free(datatype_get(dtype));
> 
> Aren't those just fancy nops?

Indeed. Already fixed by
https://git.netfilter.org/nftables/commit/src/netlink.c?id=8519ab031d8022999603a69ee9f18e8cfb06645d




Thomas


  reply	other threads:[~2023-09-20 19:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-20 13:13 [PATCH nft 0/4] remove xfree() and add free_const()+nft_gmp_free() Thomas Haller
2023-09-20 13:13 ` [PATCH nft 1/4] datatype: don't return a const string from cgroupv2_get_path() Thomas Haller
2023-09-20 13:13 ` [PATCH nft 2/4] gmputil: add nft_gmp_free() to free strings from mpz_get_str() Thomas Haller
2023-09-20 14:05   ` Phil Sutter
2023-09-20 14:46     ` Thomas Haller
2023-09-20 16:04       ` Phil Sutter
2023-09-20 13:13 ` [PATCH nft 3/4] all: add free_const() and use it instead of xfree() Thomas Haller
2023-09-20 14:13   ` Phil Sutter
2023-09-20 16:06     ` Pablo Neira Ayuso
2023-09-20 16:49       ` Phil Sutter
2023-09-20 16:52         ` Pablo Neira Ayuso
2023-09-20 18:03         ` Thomas Haller
2023-09-20 18:22           ` Phil Sutter
2023-09-20 19:48             ` Thomas Haller [this message]
2023-09-20 22:50               ` Phil Sutter
2023-09-21  9:08                 ` Thomas Haller
2023-09-20 13:13 ` [PATCH nft 4/4] all: remove xfree() and use plain free() Thomas Haller

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=f56d6b485b7154c8b8c24765ced9d222eabbd211.camel@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.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;
as well as URLs for NNTP newsgroup(s).