2015-06-04 10:39:55

by Sergei Zviagintsev

[permalink] [raw]
Subject: [PATCH] kdbus: fix type conversion in item macroses

KDBUS_ITEM_NEXT and KDBUS_ITEMS_END macroses do not enclose _i argument
into parentheses when applying the cast operator. If complex expression
passed, e.g.

KDBUS_ITEM_NEXT(condition ? a : b)

then type conversion to (u8 *) will go wrong due to precedence rules.

Use parentheses properly. Update documentation.

Signed-off-by: Sergei Zviagintsev <[email protected]>
---
Documentation/kdbus/kdbus.item.xml | 2 +-
ipc/kdbus/item.h | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/Documentation/kdbus/kdbus.item.xml b/Documentation/kdbus/kdbus.item.xml
index 09f8b903116f..b0eeeef995af 100644
--- a/Documentation/kdbus/kdbus.item.xml
+++ b/Documentation/kdbus/kdbus.item.xml
@@ -69,7 +69,7 @@
#define KDBUS_ALIGN8(val) (((val) + 7) & ~7)

#define KDBUS_ITEM_NEXT(item) \
- (typeof(item))(((uint8_t *)item) + KDBUS_ALIGN8((item)->size))
+ (typeof(item))((uint8_t *)(item) + KDBUS_ALIGN8((item)->size))

#define KDBUS_ITEM_FOREACH(item, head, first) \
for (item = (head)->first; \
diff --git a/ipc/kdbus/item.h b/ipc/kdbus/item.h
index eeefd8beac3b..03612368b3bb 100644
--- a/ipc/kdbus/item.h
+++ b/ipc/kdbus/item.h
@@ -21,7 +21,7 @@
#include "util.h"

/* generic access and iterators over a stream of items */
-#define KDBUS_ITEM_NEXT(_i) (typeof(_i))(((u8 *)_i) + KDBUS_ALIGN8((_i)->size))
+#define KDBUS_ITEM_NEXT(_i) (typeof(_i))((u8 *)(_i) + KDBUS_ALIGN8((_i)->size))
#define KDBUS_ITEMS_SIZE(_h, _is) ((_h)->size - offsetof(typeof(*_h), _is))
#define KDBUS_ITEM_HEADER_SIZE offsetof(struct kdbus_item, data)
#define KDBUS_ITEM_SIZE(_s) KDBUS_ALIGN8(KDBUS_ITEM_HEADER_SIZE + (_s))
@@ -40,7 +40,7 @@
(u8 *)(_i) >= (u8 *)(_is))

#define KDBUS_ITEMS_END(_i, _is, _s) \
- ((u8 *)_i == ((u8 *)(_is) + KDBUS_ALIGN8(_s)))
+ ((u8 *)(_i) == ((u8 *)(_is) + KDBUS_ALIGN8(_s)))

/**
* struct kdbus_item_header - Describes the fix part of an item
--
1.8.3.1


2015-06-04 11:26:15

by David Herrmann

[permalink] [raw]
Subject: Re: [PATCH] kdbus: fix type conversion in item macroses

Hi

On Thu, Jun 4, 2015 at 12:39 PM, Sergei Zviagintsev <[email protected]> wrote:
> KDBUS_ITEM_NEXT and KDBUS_ITEMS_END macroses do not enclose _i argument
> into parentheses when applying the cast operator. If complex expression
> passed, e.g.
>
> KDBUS_ITEM_NEXT(condition ? a : b)
>
> then type conversion to (u8 *) will go wrong due to precedence rules.
>
> Use parentheses properly. Update documentation.
>
> Signed-off-by: Sergei Zviagintsev <[email protected]>
> ---
> Documentation/kdbus/kdbus.item.xml | 2 +-
> ipc/kdbus/item.h | 4 ++--
> 2 files changed, 3 insertions(+), 3 deletions(-)

Reviewed-by: David Herrmann <[email protected]>

Thanks
David

> diff --git a/Documentation/kdbus/kdbus.item.xml b/Documentation/kdbus/kdbus.item.xml
> index 09f8b903116f..b0eeeef995af 100644
> --- a/Documentation/kdbus/kdbus.item.xml
> +++ b/Documentation/kdbus/kdbus.item.xml
> @@ -69,7 +69,7 @@
> #define KDBUS_ALIGN8(val) (((val) + 7) & ~7)
>
> #define KDBUS_ITEM_NEXT(item) \
> - (typeof(item))(((uint8_t *)item) + KDBUS_ALIGN8((item)->size))
> + (typeof(item))((uint8_t *)(item) + KDBUS_ALIGN8((item)->size))
>
> #define KDBUS_ITEM_FOREACH(item, head, first) \
> for (item = (head)->first; \
> diff --git a/ipc/kdbus/item.h b/ipc/kdbus/item.h
> index eeefd8beac3b..03612368b3bb 100644
> --- a/ipc/kdbus/item.h
> +++ b/ipc/kdbus/item.h
> @@ -21,7 +21,7 @@
> #include "util.h"
>
> /* generic access and iterators over a stream of items */
> -#define KDBUS_ITEM_NEXT(_i) (typeof(_i))(((u8 *)_i) + KDBUS_ALIGN8((_i)->size))
> +#define KDBUS_ITEM_NEXT(_i) (typeof(_i))((u8 *)(_i) + KDBUS_ALIGN8((_i)->size))
> #define KDBUS_ITEMS_SIZE(_h, _is) ((_h)->size - offsetof(typeof(*_h), _is))
> #define KDBUS_ITEM_HEADER_SIZE offsetof(struct kdbus_item, data)
> #define KDBUS_ITEM_SIZE(_s) KDBUS_ALIGN8(KDBUS_ITEM_HEADER_SIZE + (_s))
> @@ -40,7 +40,7 @@
> (u8 *)(_i) >= (u8 *)(_is))
>
> #define KDBUS_ITEMS_END(_i, _is, _s) \
> - ((u8 *)_i == ((u8 *)(_is) + KDBUS_ALIGN8(_s)))
> + ((u8 *)(_i) == ((u8 *)(_is) + KDBUS_ALIGN8(_s)))
>
> /**
> * struct kdbus_item_header - Describes the fix part of an item
> --
> 1.8.3.1
>

2015-06-04 11:49:18

by Frans Klaver

[permalink] [raw]
Subject: Re: [PATCH] kdbus: fix type conversion in item macroses

On Thu, Jun 4, 2015 at 12:39 PM, Sergei Zviagintsev <[email protected]> wrote:
> macroses

Sounds like a kind of rash or so ;-). 'macros' would probably sound better.

Cheers,
Frans

2015-06-04 12:17:23

by Sergei Zviagintsev

[permalink] [raw]
Subject: Re: [PATCH] kdbus: fix type conversion in item macroses

Hi,

On Thu, Jun 04, 2015 at 01:49:15PM +0200, Frans Klaver wrote:
> On Thu, Jun 4, 2015 at 12:39 PM, Sergei Zviagintsev <[email protected]> wrote:
> > macroses
>
> Sounds like a kind of rash or so ;-). 'macros' would probably sound better.

Oh, now I see the difference :) Thank you for correction, I'll resend
with proper subject.

>
> Cheers,
> Frans

2015-06-04 15:24:29

by Sergei Zviagintsev

[permalink] [raw]
Subject: Re: [PATCH] kdbus: fix type conversion in item macroses

Hi,

On Thu, Jun 04, 2015 at 01:26:03PM +0200, David Herrmann wrote:
> Hi
>
> On Thu, Jun 4, 2015 at 12:39 PM, Sergei Zviagintsev <[email protected]> wrote:
> > KDBUS_ITEM_NEXT and KDBUS_ITEMS_END macroses do not enclose _i argument
> > into parentheses when applying the cast operator. If complex expression
> > passed, e.g.
> >
> > KDBUS_ITEM_NEXT(condition ? a : b)
> >
> > then type conversion to (u8 *) will go wrong due to precedence rules.
> >
> > Use parentheses properly. Update documentation.
> >
> > Signed-off-by: Sergei Zviagintsev <[email protected]>
> > ---
> > Documentation/kdbus/kdbus.item.xml | 2 +-
> > ipc/kdbus/item.h | 4 ++--
> > 2 files changed, 3 insertions(+), 3 deletions(-)
>
> Reviewed-by: David Herrmann <[email protected]>

It seems that I missed the same issue with KDBUS_ITEMS_SIZE (and
samples/kdbus as well, but that would be another patch), so I'd better
fix that and merge with updated version of

http://permalink.gmane.org/gmane.linux.kernel/1969533

into single patch.