devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "André Przywara" <andre.przywara-5wv7dgnIgG8@public.gmane.org>
To: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@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 04/14] libfdt: fdt_add_string_(): Fix comparison warning
Date: Fri, 2 Oct 2020 10:31:06 +0100	[thread overview]
Message-ID: <3d6281e8-bd95-b2db-2b80-446f9a98ea66@arm.com> (raw)
In-Reply-To: <20201002000140.GA1844-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>

On 02/10/2020 01:01, David Gibson wrote:

Hi David,

> On Thu, Oct 01, 2020 at 04:33:50PM +0100, André Przywara wrote:
>> On 24/09/2020 02:01, David Gibson wrote:
>>> On Mon, Sep 21, 2020 at 05:52:53PM +0100, Andre Przywara wrote:
>>>> With -Wsign-compare, compilers warn about a mismatching signedness
>>>> in a comparison in fdt_add_string_().
>>>>
>>>> As struct_top can only be positive, just use an unsigned type for it,
>>>> and avoid the signedness difference.
>>>>
>>>> Signed-off-by: Andre Przywara <andre.przywara-5wv7dgnIgG8@public.gmane.org>
>>>
>>> I'm not sure this is right.  Well.. I'm also not sure it was right
>>> before.  Adding some more context to explain why..
>>>
>>>> ---
>>>>  libfdt/fdt_sw.c | 3 ++-
>>>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/libfdt/fdt_sw.c b/libfdt/fdt_sw.c
>>>> index d10a720..d65e9c8 100644
>>>> --- a/libfdt/fdt_sw.c
>>>> +++ b/libfdt/fdt_sw.c
>>>> @@ -249,7 +249,8 @@ 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 struct_top;
>>>> +	int offset;
>>>>  
>>>>  	offset = -strtabsize - len;
>>>>  	struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
>>>> 	if (fdt_totalsize(fdt) + offset < struct_top)
>>>> 		return 0; /* no more room :( */
>>>
>>> So strtabsize and len will always be positive (or, if they're not,
>>> that's another problem), so offset is always negative.  Which means we
>>> need the signed addition between totalsize and offset for this to be
>>> correct.
>>>
>>> So I suspect we want to make 'len' and 'offset' unsigned as well,
>>> reverse the sign on offset and make it a subtraction in the if instead
>>> of an addition-of-negative.
>>
>> Ah, yes, much better! To be honest I just did my due diligence on *this*
>> function before, because all those minus signs confused me quite a lot.
>> But your approach makes it much clearer what is going on here.
>>
>> So if I get this correctly, this function really returns the *negative*
>> offset of the new string, which will end up in the property, for now?
>> And at the end we translate all negative values into the actual offsets?
>> And there is initially a gap between the dt_struct part and the string
>> table, to allow both growing towards each other, without needing to
>> rewrite everything, every time?
> 
> Yes, that's correct.  During sequential write mode, to avoid excessive
> memmove()s, the structure block grows up from the bottom of the
> buffer, while the strings block grows down from the top.  Because the
> "start" (lowest offset) of the strings block is moving, we can't use
> offsets from that (without having to adjust all the offsets in the
> structure block all the time).  So, we use negative offsets from the
> "end" (highest offset).  I'm scare quoting "start" and "end" because
> the header records the highest offset of the string block as its
> "start", which is why the offsets are negative - that makes some of
> the code paths more common with the normal dtb case.
> 
>> Is this documented somewhere? Shall this approach be described in a
>> comment at the top of this file?
> 
> No, I don't think it is.  So, yes there probably should be a big
> explanatory comment.  Patches welcome?
Yes, that was the idea, just want to double check that I got this right.

> Note that this isn't supposed to be an externally visible format,
> you're always supposed to use fdt_finish() to convert it into a
> regular dtb before handing it off to any other piece of code.  We use
> a different magic number in the header while we're in this state to
> avoid anything mistaking this for a regular dtb.

Yeah, I understand, but the different magics and the negative offsets
were quite confusing at first, until I discovered the whole story.
Since the library is typically embedded into other projects, I think
it's a good idea to document this, in case people follow some rabbit
with ctags ...

Cheers,
Andre

  parent reply	other threads:[~2020-10-02  9:31 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-21 16:52 [PATCH 00/14] libfdt: Fix signed/unsigned comparison warnings Andre Przywara
     [not found] ` <20200921165303.9115-1-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-21 16:52   ` [PATCH 01/14] libfdt: fdt_offset_ptr(): Fix " Andre Przywara
     [not found]     ` <20200921165303.9115-2-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-23 11:47       ` David Gibson
2020-09-21 16:52   ` [PATCH 02/14] libfdt: fdt_mem_rsv(): " Andre Przywara
     [not found]     ` <20200921165303.9115-3-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-23 11:50       ` David Gibson
2020-09-21 16:52   ` [PATCH 03/14] libfdt: fdt_grab_space_(): Fix comparison warning Andre Przywara
     [not found]     ` <20200921165303.9115-4-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-23 11:50       ` David Gibson
2020-09-21 16:52   ` [PATCH 04/14] libfdt: fdt_add_string_(): " Andre Przywara
     [not found]     ` <20200921165303.9115-5-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  1:01       ` David Gibson
     [not found]         ` <20200924010145.GJ2298-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-01 15:33           ` André Przywara
     [not found]             ` <74c57a12-db86-ec9f-5c91-f0419c24de4b-5wv7dgnIgG8@public.gmane.org>
2020-10-02  0:01               ` David Gibson
     [not found]                 ` <20201002000140.GA1844-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-02  9:31                   ` André Przywara [this message]
     [not found]                     ` <3d6281e8-bd95-b2db-2b80-446f9a98ea66-5wv7dgnIgG8@public.gmane.org>
2020-10-02 12:25                       ` David Gibson
2020-09-21 16:52   ` [PATCH 05/14] libfdt: fdt_move(): Fix comparison warnings Andre Przywara
     [not found]     ` <20200921165303.9115-6-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  1:03       ` David Gibson
2020-09-21 16:52   ` [PATCH 06/14] libfdt: fdt_get_string(): " Andre Przywara
     [not found]     ` <20200921165303.9115-7-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  1:06       ` David Gibson
2020-09-21 16:52   ` [PATCH 07/14] libfdt: fdt_splice_(): Fix comparison warning Andre Przywara
     [not found]     ` <20200921165303.9115-8-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  1:07       ` David Gibson
2020-09-21 16:52   ` [PATCH 08/14] libfdt: fdt_create_with_flags(): " Andre Przywara
     [not found]     ` <20200921165303.9115-9-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  4:34       ` David Gibson
2020-09-21 16:52   ` [PATCH 09/14] libfdt: fdt_resize(): " Andre Przywara
     [not found]     ` <20200921165303.9115-10-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  4:43       ` David Gibson
2020-09-21 16:52   ` [PATCH 10/14] libfdt: libfdt_wip: " Andre Przywara
     [not found]     ` <20200921165303.9115-11-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-24  4:49       ` David Gibson
2020-09-21 16:53   ` [PATCH 11/14] libfdt: overlay: " Andre Przywara
     [not found]     ` <20200921165303.9115-12-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-25  4:05       ` David Gibson
2020-09-21 16:53   ` [PATCH 12/14] libfdt: fdt_get_string(): Fix sequential write comparison warnings Andre Przywara
     [not found]     ` <20200921165303.9115-13-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-25  4:09       ` David Gibson
     [not found]         ` <20200925040903.GX2298-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-01 15:33           ` André Przywara
2020-09-21 16:53   ` [PATCH 13/14] libfdt: fdt_node_offset_by_phandle(): Fix comparison warning Andre Przywara
     [not found]     ` <20200921165303.9115-14-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-25  4:09       ` David Gibson
2020-09-21 16:53   ` [PATCH 14/14] libfdt: fdt_strerror(): " Andre Przywara
     [not found]     ` <20200921165303.9115-15-andre.przywara-5wv7dgnIgG8@public.gmane.org>
2020-09-25  4:12       ` David Gibson
     [not found]         ` <20200925041208.GZ2298-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-10-01 15:34           ` André Przywara
2020-09-25  4:12   ` [PATCH 00/14] libfdt: Fix signed/unsigned comparison warnings David Gibson
     [not found]     ` <20200925041243.GA2298-l+x2Y8Cxqc4e6aEkudXLsA@public.gmane.org>
2020-09-25  8:44       ` 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=3d6281e8-bd95-b2db-2b80-446f9a98ea66@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 \
    --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).