devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
To: David Gibson
	<david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>,
	devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
Subject: [PATCH v2 0/8] dtc: Fix signed/unsigned comparison warnings
Date: Fri, 11 Jun 2021 18:10:32 +0100	[thread overview]
Message-ID: <20210611171040.25524-1-andre.przywara@arm.com> (raw)

Hi,

sorry for dropping the ball on this, but here in an update for the half
finished series from last year to fix signedness comparison warnings in
the tools (dtc/tests) - the libfdt fixes were already merged.
I addressed all (but one) of David's comments back from October, hopefully
this is good to go now.

Changelog v1 .. v2:
- dropped 2/11, 6/11 and 8/11, which are already merged
- tests: add explicit tests for negative len values
- use size_t instead of "unsigned int", where appropriate
- fdtget: expand hard-to-read conditional operator into switch/case
- dtc: use static inline fdt_is_valid_phandle() instead of macro
- use strtoul() to parse unsigned value

-----------------------

When dtc and the other tools in the tree are compiled with -Wsign-compare
or -Wextra, GCC emits quite some warnings about the signedness of the
operands not matching:
=================
In file included from dtc.h:15:0,
                 from checks.c:6:
checks.c: In function ‘fixup_phandle_references’:
checks.c:591:38: error: comparison between signed and unsigned integer
expressions [-Werror=sign-compare]
    assert(m->offset + sizeof(cell_t) <= prop->val.len);
.....
=================

libfdt has just been fixed in this regard recently; this adds the missing
bits to cover the rest of the source tree, namely dtc, the various smaller
tools, checks, and tests.

Those warnings do not show under normal conditions in the dtc repo at the
moment, because we avoid to enable the warning option.

The underlying issue is mostly due to C's promotion behaviour (ANSI C
section 6.1.3.8) when dealing with operands of different signedness
(but same size): Signed values get implictly casted to unsigned, which
is not typically what we want if they could have been negative.

The Internet(TM) suggests that blindly applying casts is probably doing
more harm than it helps, so this series tries to fix the underlying
issues properly.
Many types in dtc are somewhat suboptimal, they hold a size (which should
be non-negative), but are "int" anyway. Loop counters just declared as
"int i;" are another frequent issue.

The main strategy to fix those issues is to make the types right, which
mostly means to make variables "unsigned".
If that is not easily possible, we cast the signed expression to
"unsigned", provided this is safe, because it cannot be negative.

This series gathers multiple similar fixes in one patch, splitting them
mostly by the tool or source file. This is just to simplify review, the
patches could also be merged later on.

I tried to put some rationale in most of the patches, but explaining
every single instance becomes really tedious (so some patches paper over
that).

The final patch eventually enables the warning option in question, that
should avoid those kind of errors creeping back in again.

Compile tested with "make clean && make all tests check", after every
patch, on x86-64 and arm64.

Please have a look, happy to discuss the invididual cases.

Cheers,
Andre

Andre Przywara (8):
  tests: Fix signedness comparisons warnings
  fdtdump: Fix signedness comparisons warnings
  fdtget: Fix signedness comparisons warnings
  dtc: Wrap phandle validity check
  dtc: Fix signedness comparisons warnings: reservednum
  dtc: Fix signedness comparisons warnings: pointer diff
  checks: Fix signedness comparisons warnings
  Makefile: add -Wsign-compare to warning options

 Makefile                    |  2 +-
 checks.c                    | 31 ++++++++++++++++---------------
 dtc.c                       |  4 ++--
 dtc.h                       |  7 ++++++-
 fdtdump.c                   |  6 +++---
 fdtget.c                    | 10 ++++++++--
 flattree.c                  |  2 +-
 livetree.c                  |  6 +++---
 tests/dumptrees.c           |  2 +-
 tests/fs_tree1.c            |  2 +-
 tests/get_name.c            |  4 +++-
 tests/integer-expressions.c |  2 +-
 tests/nopulate.c            |  3 ++-
 tests/overlay.c             |  4 +++-
 tests/phandle_format.c      |  2 +-
 tests/property_iterate.c    |  2 +-
 tests/references.c          |  2 +-
 tests/set_name.c            |  6 ++++--
 tests/subnode_iterate.c     |  2 +-
 tests/tests.h               |  2 +-
 tests/testutils.c           | 12 +++++++++---
 21 files changed, 69 insertions(+), 44 deletions(-)

-- 
2.17.5


             reply	other threads:[~2021-06-11 17:10 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-11 17:10 Andre Przywara [this message]
     [not found] ` <20210611171040.25524-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-11 17:10   ` [PATCH v2 1/8] tests: Fix signedness comparisons warnings Andre Przywara
     [not found]     ` <20210611171040.25524-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:35       ` David Gibson
2021-06-18 17:00         ` Andre Przywara
     [not found]           ` <20210618180036.2ef17c4c-KTS7eRBhINUXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2021-06-19  5:45             ` David Gibson
2021-06-11 17:10   ` [PATCH v2 2/8] fdtdump: " Andre Przywara
     [not found]     ` <20210611171040.25524-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:36       ` David Gibson
2021-06-11 17:10   ` [PATCH v2 3/8] fdtget: " Andre Przywara
     [not found]     ` <20210611171040.25524-4-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:44       ` David Gibson
2021-06-15 21:51         ` Andre Przywara
     [not found]           ` <20210615225133.4a597e7d-KTS7eRBhINUXU02nzanrWNbf9cGiqdzd@public.gmane.org>
2021-06-19  5:40             ` David Gibson
2021-06-11 17:10   ` [PATCH v2 4/8] dtc: Wrap phandle validity check Andre Przywara
     [not found]     ` <20210611171040.25524-5-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:46       ` David Gibson
2021-06-11 17:10   ` [PATCH v2 5/8] dtc: Fix signedness comparisons warnings: reservednum Andre Przywara
     [not found]     ` <20210611171040.25524-6-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:48       ` David Gibson
2021-06-11 17:10   ` [PATCH v2 6/8] dtc: Fix signedness comparisons warnings: pointer diff Andre Przywara
     [not found]     ` <20210611171040.25524-7-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:49       ` David Gibson
2021-06-11 17:10   ` [PATCH v2 7/8] checks: Fix signedness comparisons warnings Andre Przywara
     [not found]     ` <20210611171040.25524-8-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2021-06-15  2:55       ` David Gibson
2021-06-11 17:10   ` [PATCH v2 8/8] Makefile: add -Wsign-compare to warning options Andre Przywara
2021-06-15  2:56   ` [PATCH v2 0/8] dtc: Fix signed/unsigned comparison warnings 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=20210611171040.25524-1-andre.przywara@arm.com \
    --to=andre.przywara-5wv7dgnigg8@public.gmane.org \
    --cc=david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).