public inbox for devicetree-compiler@vger.kernel.org
 help / color / mirror / Atom feed
From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
Subject: Re: [PATCH v3 2/5] fdtget: Fix signedness comparisons warnings
Date: Mon, 21 Jun 2021 15:27:51 +1000	[thread overview]
Message-ID: <YNAjV/BQI9mNYKxT@yekko> (raw)
In-Reply-To: <20210618172030.9684-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>

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

On Fri, Jun 18, 2021 at 06:20:27PM +0100, Andre Przywara wrote:
> With -Wsign-compare, compilers warn about a mismatching signedness in
> the different legs of the conditional operator, in fdtget.c.
> 
> In the questionable expression, we are constructing a 16-bit value out of
> two unsigned 8-bit values, however are relying on the compiler's
> automatic expansion of the uint8_t to a larger type, to survive the left
> shift. This larger type happens to be an "int", so this part of the
> expression becomes signed.
> 
> Fix this by explicitly blowing up the uint8_t to a larger *unsigned* type,
> before doing the left shift. And while we are at it, convert the hardly
> readable conditional operator usage into a sane switch/case expression.
> 
> This fixes "make fdtget", when compiled with -Wsign-compare.
> 
> Signed-off-by: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>

Applied, thanks.

> ---
>  fdtget.c        | 10 ++++++++--
>  libfdt/libfdt.h |  7 +++++++
>  2 files changed, 15 insertions(+), 2 deletions(-)
> 
> diff --git a/fdtget.c b/fdtget.c
> index 777582e..54fc6a0 100644
> --- a/fdtget.c
> +++ b/fdtget.c
> @@ -62,8 +62,14 @@ static int show_cell_list(struct display_info *disp, const char *data, int len,
>  	for (i = 0; i < len; i += size, p += size) {
>  		if (i)
>  			printf(" ");
> -		value = size == 4 ? fdt32_ld((const fdt32_t *)p) :
> -			size == 2 ? (*p << 8) | p[1] : *p;
> +		switch (size) {
> +		case 4: value = fdt32_ld((const fdt32_t *)p); break;
> +		case 2: value = fdt16_ld((const fdt16_t *)p); break;
> +		case 1:
> +		default:
> +			value = *p;
> +			break;
> +		}
>  		printf(fmt, value);
>  	}
>  
> diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h
> index 73467f7..7f117e8 100644
> --- a/libfdt/libfdt.h
> +++ b/libfdt/libfdt.h
> @@ -131,6 +131,13 @@ uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
>   * to work even with unaligned pointers on platforms (such as ARMv5) that don't
>   * like unaligned loads and stores.
>   */
> +static inline uint16_t fdt16_ld(const fdt16_t *p)
> +{
> +	const uint8_t *bp = (const uint8_t *)p;
> +
> +	return ((uint16_t)bp[0] << 8) | bp[1];
> +}
> +
>  static inline uint32_t fdt32_ld(const fdt32_t *p)
>  {
>  	const uint8_t *bp = (const uint8_t *)p;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

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

  parent reply	other threads:[~2021-06-21  5:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-18 17:20 [PATCH v3 0/5] dtc: Fix signed/unsigned comparison warnings Andre Przywara
     [not found] ` <20210618172030.9684-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-18 17:20   ` [PATCH v3 1/5] tests: Fix signedness comparisons warnings Andre Przywara
     [not found]     ` <20210618172030.9684-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-21  5:26       ` David Gibson
2021-06-18 17:20   ` [PATCH v3 2/5] fdtget: " Andre Przywara
     [not found]     ` <20210618172030.9684-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-21  5:27       ` David Gibson [this message]
2021-06-18 17:20   ` [PATCH v3 3/5] dtc: Wrap phandle validity check Andre Przywara
     [not found]     ` <20210618172030.9684-4-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-21  5:28       ` David Gibson
2021-06-18 17:20   ` [PATCH v3 4/5] checks: Fix signedness comparisons warnings Andre Przywara
     [not found]     ` <20210618172030.9684-5-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-21  5:34       ` David Gibson
2021-06-18 17:20   ` [PATCH v3 5/5] Makefile: add -Wsign-compare to warning options Andre Przywara
     [not found]     ` <20210618172030.9684-6-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-21  5:35       ` David Gibson

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=YNAjV/BQI9mNYKxT@yekko \
    --to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
    --cc=andre.przywara-5wv7dgnIgG8@public.gmane.org \
    --cc=devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.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