All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon@kernel.org>
To: Dan Carpenter <dan.carpenter@linaro.org>
Cc: Guy Levi <guyle@mellanox.com>, Yishai Hadas <yishaih@nvidia.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	linux-rdma@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] RDMA/mlx4: Make check for invalid flags stricter
Date: Tue, 4 Jul 2023 20:19:09 +0300	[thread overview]
Message-ID: <20230704171909.GE6455@unreal> (raw)
In-Reply-To: <359dc6de-2b08-4baa-99cc-d5e5f6e6ce43@kadam.mountain>

On Tue, Jul 04, 2023 at 05:07:17PM +0300, Dan Carpenter wrote:
> On Tue, Jul 04, 2023 at 04:38:41PM +0300, Leon Romanovsky wrote:
> > On Thu, Jun 29, 2023 at 09:07:37AM +0300, Dan Carpenter wrote:
> > > This code is trying to ensure that only the flags specified in the list
> > > are allowed.  The problem is that ucmd->rx_hash_fields_mask is a u64 and
> > > the flags are an enum which is treated as a u32 in this context.  That
> > > means the test doesn't check whether the highest 32 bits are zero.
> > > 
> > > Fixes: 4d02ebd9bbbd ("IB/mlx4: Fix RSS hash fields restrictions")
> > > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> > > ---
> > > The MLX4_IB_RX_HASH_INNER value is declared as
> > > "MLX4_IB_RX_HASH_INNER           = 1ULL << 31," which suggests that it
> > > should be type ULL but that doesn't work.  It will still be basically a
> > > u32.  (Enum types are weird).
> > 
> > Can you please elaborate more why enum left to be int? It is surprise to me.
> 
> Enum types are not defined very strictly in C so it's up to the
> compiler.
> 
> Clang, GCC and Sparse implement them in the same way.  They default
> to u32 unless the values can't fit, then they become whatever type fits.
> So if you have a negative, it becomes an int or a big value changes the
> type to unsigned long.
> 
> Since 1ULL < 31 fits in u32 the type is just u32.

Thanks for an explanation, I found the relevant sentence in the C
standard as well.

"The choice of type is implementation-defined, 128) but shall be
capable of representing the values of all the members of the enumeration."

Thanks

> 
> regards,
> dan carpenter
> 
> #include <stdio.h>
> 
> enum example_one {
> 	VAL = 1ULL << 31,
> };
> 
> enum example_two {
> 	NEGATIVE = -2,
> };
> 
> enum example_three {
> 	BIG = 1ULL << 32,
> };
> 
> int main(void)
> {
> 	enum example_one one = -1;
> 	enum example_two two = -1;
> 	enum example_three three = -1;
> 
> 	printf("%lu\n", sizeof(enum example_one));
> 
> 	if (one > 0)
> 		printf("one unsigned\n");
> 	if (two < 0)
> 		printf("two signed\n");
> 
> 	printf("%lu\n", sizeof(enum example_three));
> 	if (three > 0)
> 		printf("three unsigned\n");
> 
> 	return 0;
> }
> 
> 

  reply	other threads:[~2023-07-04 17:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-29  6:07 [PATCH] RDMA/mlx4: Make check for invalid flags stricter Dan Carpenter
2023-07-04 13:38 ` Leon Romanovsky
2023-07-04 14:07   ` Dan Carpenter
2023-07-04 17:19     ` Leon Romanovsky [this message]
2023-07-10 18:49     ` Jason Gunthorpe
2023-07-12 12:41 ` Leon Romanovsky

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=20230704171909.GE6455@unreal \
    --to=leon@kernel.org \
    --cc=dan.carpenter@linaro.org \
    --cc=guyle@mellanox.com \
    --cc=jgg@ziepe.ca \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=yishaih@nvidia.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.