From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
Cc: Simon Glass <sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>,
Devicetree Compiler
<devicetree-compiler-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
Varun Wadekar <vwadekar-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
Subject: Re: [PATCH v2 1/6] libfdt: fdt_add_string_(): Fix comparison warning
Date: Fri, 2 Oct 2020 10:27:33 +1000 [thread overview]
Message-ID: <20201002002733.GB1844@yekko.fritz.box> (raw)
In-Reply-To: <20201001164630.4980-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
[-- Attachment #1: Type: text/plain, Size: 2164 bytes --]
On Thu, Oct 01, 2020 at 05:46:25PM +0100, Andre Przywara wrote:
> With -Wsign-compare, compilers warn about a mismatching signedness
> in a comparison in fdt_add_string_().
>
> Make all variables unsigned, and express the negative offset trick via
> subtractions in the code.
>
> Signed-off-by: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
Applied. I think there are some followup improvements we could make
here, though.
> ---
> libfdt/fdt_sw.c | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/libfdt/fdt_sw.c b/libfdt/fdt_sw.c
> index 8de18fd..354f466 100644
> --- a/libfdt/fdt_sw.c
> +++ b/libfdt/fdt_sw.c
> @@ -250,18 +250,18 @@ int fdt_end_node(void *fdt)
> static int fdt_add_string_(void *fdt, const char *s)
> {
> char *strtab = (char *)fdt + fdt_totalsize(fdt);
> - int strtabsize = fdt_size_dt_strings(fdt);
> - int len = strlen(s) + 1;
> - int struct_top, offset;
> + unsigned int strtabsize = fdt_size_dt_strings(fdt);
> + unsigned int len = strlen(s) + 1;
In both the old and new versions, there's an implicit cast from size_t
here, which I think could theoretically overflow (with a colossal
string, 32-bit ints and 64-bit pointers/size_t). So we probably
should actually check that this is <= INT_MAX.
> + unsigned int struct_top, offset;
>
> - offset = -strtabsize - len;
> + offset = strtabsize + len;
> struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
> - if (fdt_totalsize(fdt) + offset < struct_top)
> + if (fdt_totalsize(fdt) - offset < struct_top)
Likewise we should check that totalisize - offset doesn't overflow
(underflow?).
> return 0; /* no more room :( */
>
> - memcpy(strtab + offset, s, len);
> + memcpy(strtab - offset, s, len);
> fdt_set_size_dt_strings(fdt, strtabsize + len);
> - return offset;
> + return -offset;
> }
>
> /* Must only be used to roll back in case of error */
--
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 --]
next prev parent reply other threads:[~2020-10-02 0:27 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-01 16:46 [PATCH v2 0/6] libfdt: Fix signed/unsigned comparison warnings Andre Przywara
[not found] ` <20201001164630.4980-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-01 16:46 ` [PATCH v2 1/6] libfdt: fdt_add_string_(): Fix comparison warning Andre Przywara
[not found] ` <20201001164630.4980-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:27 ` David Gibson [this message]
2020-10-01 16:46 ` [PATCH v2 2/6] libfdt: fdt_move(): Fix comparison warnings Andre Przywara
[not found] ` <20201001164630.4980-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:28 ` David Gibson
2020-10-01 16:46 ` [PATCH v2 3/6] libfdt: fdt_create_with_flags(): Fix comparison warning Andre Przywara
[not found] ` <20201001164630.4980-4-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:29 ` David Gibson
2020-10-01 16:46 ` [PATCH v2 4/6] libfdt: libfdt_wip: " Andre Przywara
[not found] ` <20201001164630.4980-5-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:30 ` David Gibson
2020-10-01 16:46 ` [PATCH v2 5/6] libfdt: fdt_get_string(): Fix sequential write comparison warnings Andre Przywara
[not found] ` <20201001164630.4980-6-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:33 ` David Gibson
2020-10-01 16:46 ` [PATCH v2 6/6] libfdt: fdt_strerror(): Fix comparison warning Andre Przywara
[not found] ` <20201001164630.4980-7-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-10-02 0:34 ` David Gibson
2020-10-02 1:03 ` [PATCH v2 0/6] libfdt: Fix signed/unsigned comparison warnings David Gibson
[not found] ` <20201002010324.GH1844-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-02 9:25 ` André Przywara
[not found] ` <316e6e0f-e15e-89ee-3008-d2ed038ffd79-5wv7dgnIgG8@public.gmane.org>
2020-10-02 12:32 ` David Gibson
[not found] ` <20201002123209.GA442245-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-02 14:43 ` André Przywara
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=20201002002733.GB1844@yekko.fritz.box \
--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 \
--cc=vwadekar-DDmLM1+adcrQT0dZR+AlfA@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 NNTP newsgroup(s).