netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: NetFilter <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup()
Date: Tue, 29 Aug 2023 21:58:53 +0200	[thread overview]
Message-ID: <c452805919f688f15a95e52139c8686e1a6571a1.camel@redhat.com> (raw)
In-Reply-To: <ZO5DsA4eCnYkEWxC@calendula>

On Tue, 2023-08-29 at 21:14 +0200, Pablo Neira Ayuso wrote:
> On Tue, Aug 29, 2023 at 09:10:26PM +0200, Pablo Neira Ayuso wrote:
> > On Tue, Aug 29, 2023 at 08:54:11PM +0200, Thomas Haller wrote:
> > > An enum can be either signed or unsigned (implementation
> > > defined).
> > > 
> > > datatype_lookup() checks for invalid type arguments. Also check,
> > > whether
> > > the argument is not negative (which, depending on the compiler it
> > > may
> > > never be).
> > > 
> > > Signed-off-by: Thomas Haller <thaller@redhat.com>
> > > ---
> > >  src/datatype.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/src/datatype.c b/src/datatype.c
> > > index ba1192c83595..91735ff8b360 100644
> > > --- a/src/datatype.c
> > > +++ b/src/datatype.c
> > > @@ -87,7 +87,7 @@ const struct datatype *datatype_lookup(enum
> > > datatypes type)
> > >  {
> > >         BUILD_BUG_ON(TYPE_MAX & ~TYPE_MASK);
> > >  
> > > -       if (type > TYPE_MAX)
> > > +       if ((uintmax_t) type > TYPE_MAX)
> > 
> >             uint32_t ?

The more straight forward way would be

    if (type < 0 || type > TYPE_MAX)

However, if the enum is unsigned, then the compiler might see that the
condition is never true and warn against that. It does warn, if "type"
were just an "unsigned int". I cannot actually reproduce a compiler
warning with the enum (for now).

The size of the enum is most likely int/unsigned (or smaller, with "-
fshort-enums" or packed). Is it on POSIX/Linux always guaranteed that
an int is 32bit? I think not, but I cannot find an architecture where
int is larger either. Also, if someone would add an enum value larger
than the 32 bit range, then the behavior is compiler dependent, but
most likely the enum type would be a 64 bit integer and
"uint"/"uint32_t" would not be the right check.

All of this is highly theoretical. But "uintmax_t" avoids all those
problems and makes fewer assumptions on what the enum actually is. Is
there a hypothetical scenario where it wouldn't work correctly?

> 
> Another question: What warning does clang print on this one?
> Description does not specify.

this one isn't about a compiler warning. Sorry, I should not have
included it in this set.


Thomas


  reply	other threads:[~2023-08-29 20:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 18:54 [PATCH nft 0/5] fix compiler warnings with clang and "-Wextra" Thomas Haller
2023-08-29 18:54 ` [PATCH nft 1/5] rule: fix "const static" declaration Thomas Haller
2023-08-29 18:54 ` [PATCH nft 2/5] utils: call abort() after BUG() macro Thomas Haller
2023-08-29 18:54 ` [PATCH nft 3/5] src: silence "implicit-fallthrough" warnings Thomas Haller
2023-08-29 18:54 ` [PATCH nft 4/5] xt: avoid "-Wmissing-field-initializers" for "original_opts" Thomas Haller
2023-08-29 18:54 ` [PATCH nft 5/5] datatype: check against negative "type" argument in datatype_lookup() Thomas Haller
2023-08-29 19:10   ` Pablo Neira Ayuso
2023-08-29 19:14     ` Pablo Neira Ayuso
2023-08-29 19:58       ` Thomas Haller [this message]
2023-08-30  7:46         ` Pablo Neira Ayuso
2023-08-30  8:08           ` Thomas Haller
2023-08-30  8:23             ` Pablo Neira Ayuso

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=c452805919f688f15a95e52139c8686e1a6571a1.camel@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /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).