qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [QEMU 1/1] nvme: Fix misleading macro when mixed with ternary operator
@ 2022-07-07 13:36 Darren Kenny
  2022-07-12 12:11 ` Stefan Hajnoczi
  0 siblings, 1 reply; 3+ messages in thread
From: Darren Kenny @ 2022-07-07 13:36 UTC (permalink / raw)
  To: qemu-block, qemu-devel
  Cc: Hanna Reitz, Kevin Wolf, Philippe Mathieu-Daudé, Fam Zheng,
	Stefan Hajnoczi, Klaus Jensen, Keith Busch, darren.kenny

Using the Parfait source code analyser and issue was found in
hw/nvme/ctrl.c where the macros NVME_CAP_SET_CMBS and NVME_CAP_SET_PMRS
are called with a ternary operatore in the second parameter, resulting
in a potentially unexpected expansion of the form:

  x ? a: b & FLAG_TEST

which will result in a different result to:

  (x ? a: b) & FLAG_TEST.

The macros should wrap each of the parameters in brackets to ensure the
correct result on expansion.

Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
---
 include/block/nvme.h | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/include/block/nvme.h b/include/block/nvme.h
index 373c70b5ca7f..b35f31a9f958 100644
--- a/include/block/nvme.h
+++ b/include/block/nvme.h
@@ -98,28 +98,28 @@ enum NvmeCapMask {
 #define NVME_CAP_PMRS(cap)  (((cap) >> CAP_PMRS_SHIFT)   & CAP_PMRS_MASK)
 #define NVME_CAP_CMBS(cap)  (((cap) >> CAP_CMBS_SHIFT)   & CAP_CMBS_MASK)
 
-#define NVME_CAP_SET_MQES(cap, val)   (cap |= (uint64_t)(val & CAP_MQES_MASK)  \
-                                                           << CAP_MQES_SHIFT)
-#define NVME_CAP_SET_CQR(cap, val)    (cap |= (uint64_t)(val & CAP_CQR_MASK)   \
-                                                           << CAP_CQR_SHIFT)
-#define NVME_CAP_SET_AMS(cap, val)    (cap |= (uint64_t)(val & CAP_AMS_MASK)   \
-                                                           << CAP_AMS_SHIFT)
-#define NVME_CAP_SET_TO(cap, val)     (cap |= (uint64_t)(val & CAP_TO_MASK)    \
-                                                           << CAP_TO_SHIFT)
-#define NVME_CAP_SET_DSTRD(cap, val)  (cap |= (uint64_t)(val & CAP_DSTRD_MASK) \
-                                                           << CAP_DSTRD_SHIFT)
-#define NVME_CAP_SET_NSSRS(cap, val)  (cap |= (uint64_t)(val & CAP_NSSRS_MASK) \
-                                                           << CAP_NSSRS_SHIFT)
-#define NVME_CAP_SET_CSS(cap, val)    (cap |= (uint64_t)(val & CAP_CSS_MASK)   \
-                                                           << CAP_CSS_SHIFT)
-#define NVME_CAP_SET_MPSMIN(cap, val) (cap |= (uint64_t)(val & CAP_MPSMIN_MASK)\
-                                                           << CAP_MPSMIN_SHIFT)
-#define NVME_CAP_SET_MPSMAX(cap, val) (cap |= (uint64_t)(val & CAP_MPSMAX_MASK)\
-                                                           << CAP_MPSMAX_SHIFT)
-#define NVME_CAP_SET_PMRS(cap, val)   (cap |= (uint64_t)(val & CAP_PMRS_MASK)  \
-                                                           << CAP_PMRS_SHIFT)
-#define NVME_CAP_SET_CMBS(cap, val)   (cap |= (uint64_t)(val & CAP_CMBS_MASK)  \
-                                                           << CAP_CMBS_SHIFT)
+#define NVME_CAP_SET_MQES(cap, val)   \
+    ((cap) |= (uint64_t)((val) & CAP_MQES_MASK)   << CAP_MQES_SHIFT)
+#define NVME_CAP_SET_CQR(cap, val)    \
+    ((cap) |= (uint64_t)((val) & CAP_CQR_MASK)    << CAP_CQR_SHIFT)
+#define NVME_CAP_SET_AMS(cap, val)    \
+    ((cap) |= (uint64_t)((val) & CAP_AMS_MASK)    << CAP_AMS_SHIFT)
+#define NVME_CAP_SET_TO(cap, val)     \
+    ((cap) |= (uint64_t)((val) & CAP_TO_MASK)     << CAP_TO_SHIFT)
+#define NVME_CAP_SET_DSTRD(cap, val)  \
+    ((cap) |= (uint64_t)((val) & CAP_DSTRD_MASK)  << CAP_DSTRD_SHIFT)
+#define NVME_CAP_SET_NSSRS(cap, val)  \
+    ((cap) |= (uint64_t)((val) & CAP_NSSRS_MASK)  << CAP_NSSRS_SHIFT)
+#define NVME_CAP_SET_CSS(cap, val)    \
+    ((cap) |= (uint64_t)((val) & CAP_CSS_MASK)    << CAP_CSS_SHIFT)
+#define NVME_CAP_SET_MPSMIN(cap, val) \
+    ((cap) |= (uint64_t)((val) & CAP_MPSMIN_MASK) << CAP_MPSMIN_SHIFT)
+#define NVME_CAP_SET_MPSMAX(cap, val) \
+    ((cap) |= (uint64_t)((val) & CAP_MPSMAX_MASK) << CAP_MPSMAX_SHIFT)
+#define NVME_CAP_SET_PMRS(cap, val)   \
+    ((cap) |= (uint64_t)((val) & CAP_PMRS_MASK)   << CAP_PMRS_SHIFT)
+#define NVME_CAP_SET_CMBS(cap, val)   \
+    ((cap) |= (uint64_t)((val) & CAP_CMBS_MASK)   << CAP_CMBS_SHIFT)
 
 enum NvmeCapCss {
     NVME_CAP_CSS_NVM        = 1 << 0,
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [QEMU 1/1] nvme: Fix misleading macro when mixed with ternary operator
  2022-07-07 13:36 [QEMU 1/1] nvme: Fix misleading macro when mixed with ternary operator Darren Kenny
@ 2022-07-12 12:11 ` Stefan Hajnoczi
  2022-07-12 12:22   ` Klaus Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Hajnoczi @ 2022-07-12 12:11 UTC (permalink / raw)
  To: its
  Cc: qemu-block, qemu-devel, Hanna Reitz, Kevin Wolf,
	Philippe Mathieu-Daudé, Fam Zheng, Keith Busch, darren.kenny

[-- Attachment #1: Type: text/plain, Size: 822 bytes --]

On Thu, Jul 07, 2022 at 01:36:21PM +0000, Darren Kenny wrote:
> Using the Parfait source code analyser and issue was found in
> hw/nvme/ctrl.c where the macros NVME_CAP_SET_CMBS and NVME_CAP_SET_PMRS
> are called with a ternary operatore in the second parameter, resulting
> in a potentially unexpected expansion of the form:
> 
>   x ? a: b & FLAG_TEST
> 
> which will result in a different result to:
> 
>   (x ? a: b) & FLAG_TEST.
> 
> The macros should wrap each of the parameters in brackets to ensure the
> correct result on expansion.
> 
> Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
> ---
>  include/block/nvme.h | 44 ++++++++++++++++++++++----------------------
>  1 file changed, 22 insertions(+), 22 deletions(-)

Klaus: ping

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [QEMU 1/1] nvme: Fix misleading macro when mixed with ternary operator
  2022-07-12 12:11 ` Stefan Hajnoczi
@ 2022-07-12 12:22   ` Klaus Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Klaus Jensen @ 2022-07-12 12:22 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: qemu-block, qemu-devel, Hanna Reitz, Kevin Wolf,
	Philippe Mathieu-Daudé, Fam Zheng, Keith Busch, darren.kenny

[-- Attachment #1: Type: text/plain, Size: 1011 bytes --]

On Jul 12 13:11, Stefan Hajnoczi wrote:
> On Thu, Jul 07, 2022 at 01:36:21PM +0000, Darren Kenny wrote:
> > Using the Parfait source code analyser and issue was found in
> > hw/nvme/ctrl.c where the macros NVME_CAP_SET_CMBS and NVME_CAP_SET_PMRS
> > are called with a ternary operatore in the second parameter, resulting
> > in a potentially unexpected expansion of the form:
> > 
> >   x ? a: b & FLAG_TEST
> > 
> > which will result in a different result to:
> > 
> >   (x ? a: b) & FLAG_TEST.
> > 
> > The macros should wrap each of the parameters in brackets to ensure the
> > correct result on expansion.
> > 
> > Signed-off-by: Darren Kenny <darren.kenny@oracle.com>
> > ---
> >  include/block/nvme.h | 44 ++++++++++++++++++++++----------------------
> >  1 file changed, 22 insertions(+), 22 deletions(-)
> 
> Klaus: ping
> 
> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

Sorry,

Thanks Darren, applied to nvme-next!

Reviewed-by: Klaus Jensen <k.jensen@samsung.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-07-12 12:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-07 13:36 [QEMU 1/1] nvme: Fix misleading macro when mixed with ternary operator Darren Kenny
2022-07-12 12:11 ` Stefan Hajnoczi
2022-07-12 12:22   ` Klaus Jensen

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).