All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/8] dtc: Fix signed/unsigned comparison warnings
@ 2021-06-11 17:10 Andre Przywara
       [not found] ` <20210611171040.25524-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
  0 siblings, 1 reply; 21+ messages in thread
From: Andre Przywara @ 2021-06-11 17:10 UTC (permalink / raw)
  To: David Gibson, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA; +Cc: Simon Glass

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


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

end of thread, other threads:[~2021-06-19  5:45 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11 17:10 [PATCH v2 0/8] dtc: Fix signed/unsigned comparison warnings Andre Przywara
     [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

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.