libfdt: Fix signed/unsigned comparison warnings
cover libfdt: Fix signed/unsigned comparison warnings
When libfdt is compiled with -Wsign-compare or -Wextra, GCC emits quite
some warnings about the signedness of the operands not matching:
=================libfdt/fdt.c:140:18: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
if ((absoffset < offset)
.....
=================
This does not occur under normal conditions in the dtc repo, but might show up when libfdt is embedded in another project. There have been reports from U-Boot and Trusted-Firmware-A.
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.
In libfdt, some types are somewhat suboptimal ("int bufsize" comes to mind); some signed types are due to them being returned along wih error values in other functions (node offsets).
So these fixes here have been based on the following assumptions:
- We cannot change the prototype of exported functions.
- It's better to change types (for local variables) than to cast.
- If we have established that a signed value is not negative, we can safely cast it to an unsigned type.
I split up the fixes in small chunks, to make them easier to review. The first four patches change types, the next six ones use casts after we made sure the values are not negative.
This is only covering libfdt for now (which is what those other projects care about). There are more issues with dtc, but they can be addressed later.
Not sure if some of these checks should be gated by can_assume() calls.
Please have a look, happy to discuss the invididual cases.
Cheers,
Andre
Andre Przywara (13):
libfdt: fdt_offset_ptr(): Fix comparison warnings
libfdt: fdt_mem_rsv(): Fix comparison warnings
libfdt: fdt_grab_space_(): Fix comparison warning
libfdt: fdt_add_string_(): Fix comparison warning
libfdt: fdt_move(): Fix comparison warnings
libfdt: fdt_splice_(): Fix comparison warning
libfdt: fdt_create_with_flags(): Fix comparison warning
libfdt: fdt_resize(): Fix comparison warning
libfdt: libfdt_wip: Fix comparison warning
libfdt: overlay: Fix comparison warning
libfdt: fdt_get_string(): Fix sequential write comparison warnings
libfdt: fdt_node_offset_by_phandle(): Fix comparison warning
libfdt: fdt_strerror(): Fix comparison warning
Simon Glass (1):
libfdt: fdt_get_string(): Fix comparison warnings
libfdt/fdt.c | 15 +++++++++++---- libfdt/fdt_overlay.c | 3 ++- libfdt/fdt_ro.c | 14 +++++++------- libfdt/fdt_rw.c | 2 +- libfdt/fdt_strerror.c | 2 +- libfdt/fdt_sw.c | 14 +++++++++----- libfdt/fdt_wip.c | 4 ++-- 7 files changed, 33 insertions(+), 21 deletions(-)
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-1-andre.przywara-5wv7dgnIgG8@public.gmane.org
PATCH 1/14 libfdt: fdt_offset_ptr(): Fix comparison warnings
With -Wsign-compare, compilers warn about mismatching signedness in comparisons in fdt_offset_ptr().
This mostly stems from "offset" being passed in as a signed integer, even though the function would not really tolerate negative values.
Short of changing the prototype, check that offset is not negative, and use an unsigned type internally.
--- libfdt/fdt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
libfdt/fdt.c +7 -3
@@ -134,16 +134,20 @@ int fdt_check_header(const void *fdt)const void *fdt_offset_ptr (const void *fdt,int offset,unsigned int len) { -unsigned absoffset = offset +fdt_off_dt_struct (fdt); +unsigned int uoffset = offset; +unsigned int absoffset = offset +fdt_off_dt_struct (fdt); + +if (offset <0 ) +return NULL ;if (!can_assume (VALID_INPUT )) -if ((absoffset < offset) +if ((absoffset < uoffset) || ((absoffset + len) < absoffset) || (absoffset + len) >fdt_totalsize (fdt))return NULL ;if (can_assume (LATEST ) ||fdt_version (fdt) >=0x11 ) -if (((offset + len) < offset) +if (((uoffset + len) < uoffset) || ((offset + len) >fdt_size_dt_struct (fdt)))return NULL ;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-2-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 01/14] libfdt: fdt_offset_ptr(): Fix comparison warnings
Quoted 10 lines · Andre Przywara
With -Wsign-compare, compilers warn about mismatching signedness in comparisons in
fdt_offset_ptr().This mostly stems from "offset" being passed in as a signed integer, even though the function would not really tolerate negative values.
Short of changing the prototype, check that offset is not negative, and use an unsigned type internally.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 32 lines
--- libfdt/fdt.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)libfdt/fdt.c +7
-3@@ -134,16 +134,20 @@ int fdt_check_header(const void *fdt)const void *fdt_offset_ptr (const void *fdt,int offset,unsigned int len) { -unsigned absoffset = offset +fdt_off_dt_struct (fdt); +unsigned int uoffset = offset; +unsigned int absoffset = offset +fdt_off_dt_struct (fdt); + +if (offset <0 ) +return NULL ;if (!can_assume (VALID_INPUT )) -if ((absoffset < offset) +if ((absoffset < uoffset) || ((absoffset + len) < absoffset) || (absoffset + len) >fdt_totalsize (fdt))return NULL ;if (can_assume (LATEST ) ||fdt_version (fdt) >=0x11 ) -if (((offset + len) < offset) +if (((uoffset + len) < uoffset) || ((offset + len) >fdt_size_dt_struct (fdt)))return NULL ;
Signature · 4 lines
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
PATCH 2/14 libfdt: fdt_mem_rsv(): Fix comparison warnings
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in comparisons in fdt_mem_rsv().
Since all involved values must be positive, change the used types to be unsigned.
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +2 -2
@@ -157,8 +157,8 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)static const struct fdt_reserve_entry *fdt_mem_rsv (const void *fdt,int n) { -int offset = n *sizeof (struct fdt_reserve_entry ); -int absoffset =fdt_off_mem_rsvmap (fdt) + offset; +unsigned int offset = n *sizeof (struct fdt_reserve_entry ); +unsigned int absoffset =fdt_off_mem_rsvmap (fdt) + offset;if (!can_assume (VALID_INPUT )) {if (absoffset <fdt_off_mem_rsvmap (fdt))
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-3-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 02/14] libfdt: fdt_mem_rsv(): Fix comparison warnings
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_mem_rsv().Since all involved values must be positive, change the used types to be unsigned.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 19 lines
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_ro.c +2
-2@@ -157,8 +157,8 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)static const struct fdt_reserve_entry *fdt_mem_rsv (const void *fdt,int n) { -int offset = n *sizeof (struct fdt_reserve_entry ); -int absoffset =fdt_off_mem_rsvmap (fdt) + offset; +unsigned int offset = n *sizeof (struct fdt_reserve_entry ); +unsigned int absoffset =fdt_off_mem_rsvmap (fdt) + offset;if (!can_assume (VALID_INPUT )) {if (absoffset <fdt_off_mem_rsvmap (fdt))
Signature · 4 lines
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
PATCH 3/14 libfdt: fdt_grab_space_(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_grab_space_().
All the involved values cannot be negative, so let's switch the types of the local variables to unsigned to make the compiler happy.
--- libfdt/fdt_sw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_sw.c +2 -2
@@ -93,8 +93,8 @@ static inline uint32_t sw_flags(void *fdt)static void *fdt_grab_space_ (void *fdt,size_t len) { -int offset =fdt_size_dt_struct (fdt); -int spaceleft; +unsigned int offset =fdt_size_dt_struct (fdt); +unsigned int spaceleft; spaceleft =fdt_totalsize (fdt) -fdt_off_dt_struct (fdt) -fdt_size_dt_strings (fdt);
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-4-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 03/14] libfdt: fdt_grab_space_(): Fix comparison warning
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_grab_space_().All the involved values cannot be negative, so let's switch the types of the local variables to unsigned to make the compiler happy.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 19 lines
--- libfdt/fdt_sw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_sw.c +2
-2@@ -93,8 +93,8 @@ static inline uint32_t sw_flags(void *fdt)static void *fdt_grab_space_ (void *fdt,size_t len) { -int offset =fdt_size_dt_struct (fdt); -int spaceleft; +unsigned int offset =fdt_size_dt_struct (fdt); +unsigned int spaceleft; spaceleft =fdt_totalsize (fdt) -fdt_off_dt_struct (fdt) -fdt_size_dt_strings (fdt);
Signature · 4 lines
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
PATCH 4/14 libfdt: fdt_add_string_(): Fix comparison warning
In reply to a message not in the archiveWith -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.
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
libfdt/fdt_sw.c +2 -1
@@ -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);
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-5-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 04/14] libfdt: fdt_add_string_(): Fix comparison warning
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_add_string_().As
struct_topcan only be positive, just use an unsigned type for it, and avoid the signedness difference.Signed-off-by Andre Przywara <[email protected]>
I'm not sure this is right. Well.. I'm also not sure it was right before. Adding some more context to explain why..
Quoted 18 lines
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +2
-1@@ -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);
Quoted 2 lines
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.
We might then need to explicitly check for offset < totalsize as well, to cover the overflow case.
Signature · 4 lines
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 04/14] libfdt: fdt_add_string_(): Fix comparison warning
Quoted 38 lines · David Gibson
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_add_string_().As
struct_topcan only be positive, just use an unsigned type for it, and avoid the signedness difference.Signed-off-by Andre Przywara <[email protected]>I'm not sure this is right. Well.. I'm also not sure it was right before. Adding some more context to explain why..
Quoted 18 lines
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +2
-1@@ -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);Quoted 2 lines
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.
Quoted 3 lines
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?
Is this documented somewhere? Shall this approach be described in a comment at the top of this file?
Thanks!
Andre.
Quoted 4 lines
We might then need to explicitly check for offset < totalsize as well, to cover the overflow case.
A message that is not in the archive (sent off-list or not yet received): 74c57a12-db86-ec9f-5c91-f0419c24de4b-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 04/14] libfdt: fdt_add_string_(): Fix comparison warning
Quoted 47 lines · André Przywara
Quoted 38 lines · David Gibson
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_add_string_().As
struct_topcan only be positive, just use an unsigned type for it, and avoid the signedness difference.Signed-off-by Andre Przywara <[email protected]>I'm not sure this is right. Well.. I'm also not sure it was right before. Adding some more context to explain why..
Quoted 18 lines
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +2
-1@@ -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);Quoted 2 lines
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.
Quoted 3 lines
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.
Quoted 6 lines
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_structpart 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.
Quoted 2 lines
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?
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.
Signature · 4 lines
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 04/14] libfdt: fdt_add_string_(): Fix comparison warning
On 02/10/2020 01:01, David Gibson wrote:
Hi David,
Quoted 66 lines · André Przywara
Quoted 47 lines · David Gibson
Quoted 38 lines · Andre Przywara
Quoted 7 lines
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_add_string_().As
struct_topcan only be positive, just use an unsigned type for it, and avoid the signedness difference.Signed-off-by Andre Przywara <[email protected]>I'm not sure this is right. Well.. I'm also not sure it was right before. Adding some more context to explain why..
Quoted 18 lines
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +2
-1@@ -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);Quoted 2 lines
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.
Quoted 3 lines
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.
Quoted 6 lines
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_structpart 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.
Quoted 5 lines
Quoted 2 lines
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.
Quoted 5 lines
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
A message that is not in the archive (sent off-list or not yet received): 3d6281e8-bd95-b2db-2b80-446f9a98ea66-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 04/14] libfdt: fdt_add_string_(): Fix comparison warning
Quoted 83 lines · André Przywara
On 02/10/2020 01:01, David Gibson wrote:
Hi David,
Quoted 66 lines · André Przywara
Quoted 47 lines · David Gibson
Quoted 38 lines · Andre Przywara
Quoted 7 lines
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_add_string_().As
struct_topcan only be positive, just use an unsigned type for it, and avoid the signedness difference.Signed-off-by Andre Przywara <[email protected]>I'm not sure this is right. Well.. I'm also not sure it was right before. Adding some more context to explain why..
Quoted 18 lines
--- libfdt/fdt_sw.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +2
-1@@ -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);Quoted 2 lines
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.
Quoted 3 lines
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.
Quoted 6 lines
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_structpart 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.
Quoted 5 lines
Quoted 2 lines
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.
Quoted 5 lines
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.
Quoted 5 lines
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 ...
Yes, that makes sense.
Signature · 4 lines
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
PATCH 5/14 libfdt: fdt_move(): Fix comparison warnings
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in comparisons in fdt_move().
This stems from "bufsize" being passed in as a signed integer, even though we would expect a buffer size to be positive.
Short of changing the prototype, check that bufsize is not negative, and cast it to an unsigned type in the comparison.
--- libfdt/fdt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
libfdt/fdt.c +4 -1
@@ -314,9 +314,12 @@ const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)int fdt_move (const void *fdt,void *buf,int bufsize) { +if (bufsize <0 ) +return -FDT_ERR_NOSPACE ; +FDT_RO_PROBE (fdt); -if (fdt_totalsize (fdt) > bufsize) +if (fdt_totalsize (fdt) > (unsigned int )bufsize)return -FDT_ERR_NOSPACE ;memmove (buf, fdt,fdt_totalsize (fdt));
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-6-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 05/14] libfdt: fdt_move(): Fix comparison warnings
Quoted 10 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_move().This stems from "bufsize" being passed in as a signed integer, even though we would expect a buffer size to be positive.
Short of changing the prototype, check that bufsize is not negative, and cast it to an unsigned type in the comparison.
Signed-off-by Andre Przywara <[email protected]>
Logic looks good, but the libfdt-in-tiny-bootloaders people would probably appreciate a !can_assume(VALID_INPUT) test to elide the check on bufsize.
Quoted 22 lines
--- libfdt/fdt.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)libfdt/fdt.c +4
-1@@ -314,9 +314,12 @@ const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)int fdt_move (const void *fdt,void *buf,int bufsize) { +if (bufsize <0 ) +return -FDT_ERR_NOSPACE ; +FDT_RO_PROBE (fdt); -if (fdt_totalsize (fdt) > bufsize) +if (fdt_totalsize (fdt) > (unsigned int )bufsize)return -FDT_ERR_NOSPACE ;memmove (buf, fdt,fdt_totalsize (fdt));
Signature · 4 lines
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
PATCH 6/14 libfdt: fdt_get_string(): Fix comparison warnings
In reply to a message not in the archiveFrom: Simon Glass <[email protected]>
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in fdt_get_string().
In the first two cases, we have just established that the signed values are not negative, so it's safe to cast the values to an unsigned type.
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +2 -2
@@ -53,7 +53,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) err = -FDT_ERR_BADOFFSET ; absoffset = stroffset +fdt_off_dt_strings (fdt); -if (absoffset >= totalsize) +if (absoffset >= (unsigned )totalsize) goto fail; len = totalsize - absoffset;
@@ -61,7 +61,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)if (stroffset <0 ) goto fail;if (can_assume (LATEST ) ||fdt_version (fdt) >=17 ) { -if (stroffset >=fdt_size_dt_strings (fdt)) +if ((unsigned )stroffset >=fdt_size_dt_strings (fdt)) goto fail;if ((fdt_size_dt_strings (fdt) - stroffset) < len) len =fdt_size_dt_strings (fdt) - stroffset;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-7-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 06/14] libfdt: fdt_get_string(): Fix comparison warnings
Quoted 10 lines · Andre Przywara
From: Simon Glass <[email protected]>
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_get_string().In the first two cases, we have just established that the signed values are not negative, so it's safe to cast the values to an unsigned type.
Signed-off-by Simon Glass <[email protected]>Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 26 lines
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_ro.c +2
-2@@ -53,7 +53,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) err = -FDT_ERR_BADOFFSET ; absoffset = stroffset +fdt_off_dt_strings (fdt); -if (absoffset >= totalsize) +if (absoffset >= (unsigned )totalsize) goto fail; len = totalsize - absoffset;@@ -61,7 +61,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp)if (stroffset <0 ) goto fail;if (can_assume (LATEST ) ||fdt_version (fdt) >=17 ) { -if (stroffset >=fdt_size_dt_strings (fdt)) +if ((unsigned )stroffset >=fdt_size_dt_strings (fdt)) goto fail;if ((fdt_size_dt_strings (fdt) - stroffset) < len) len =fdt_size_dt_strings (fdt) - stroffset;
Signature · 4 lines
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
PATCH 7/14 libfdt: fdt_splice_(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_splice_().
Since we just established that oldlen is not negative, we can safely cast it to an unsigned type.
--- libfdt/fdt_rw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_rw.c +1 -1
@@ -59,7 +59,7 @@ static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)if ((oldlen <0 ) || (soff + oldlen < soff) || (soff + oldlen > dsize))return -FDT_ERR_BADOFFSET ; -if ((p < (char *)fdt) || (dsize + newlen < oldlen)) +if ((p < (char *)fdt) || (dsize + newlen < (unsigned )oldlen))return -FDT_ERR_BADOFFSET ;if (dsize - oldlen + newlen >fdt_totalsize (fdt))return -FDT_ERR_NOSPACE ;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-8-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 07/14] libfdt: fdt_splice_(): Fix comparison warning
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_splice_().Since we just established that oldlen is not negative, we can safely cast it to an unsigned type.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 17 lines
--- libfdt/fdt_rw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_rw.c +1
-1@@ -59,7 +59,7 @@ static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)if ((oldlen <0 ) || (soff + oldlen < soff) || (soff + oldlen > dsize))return -FDT_ERR_BADOFFSET ; -if ((p < (char *)fdt) || (dsize + newlen < oldlen)) +if ((p < (char *)fdt) || (dsize + newlen < (unsigned )oldlen))return -FDT_ERR_BADOFFSET ;if (dsize - oldlen + newlen >fdt_totalsize (fdt))return -FDT_ERR_NOSPACE ;
Signature · 4 lines
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
PATCH 8/14 libfdt: fdt_create_with_flags(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_create_with_flags().
We have just established that bufsize is not negative, so a cast to an unsigned type is safe.
--- libfdt/fdt_sw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_sw.c +1 -1
@@ -112,7 +112,7 @@ int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags) sizeof(struct fdt_reserve_entry));void *fdt = buf; -if (bufsize < hdrsize) +if (bufsize <0 || (unsigned )bufsize < hdrsize)return -FDT_ERR_NOSPACE ;if (flags & ~FDT_CREATE_FLAGS_ALL )
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-9-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 08/14] libfdt: fdt_create_with_flags(): Fix comparison warning
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_create_with_flags().We have just established that bufsize is not negative, so a cast to an unsigned type is safe.
Signed-off-by Andre Przywara <[email protected]>
This looks correct, but I think we might be able to do it a bit more neatly by changing the type of hdrsize to signed instead. It's a small value, so we know that's a valid conversion, and that would let your two tests be merged into one here.
Quoted 17 lines
--- libfdt/fdt_sw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_sw.c +1
-1@@ -112,7 +112,7 @@ int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags) sizeof(struct fdt_reserve_entry));void *fdt = buf; -if (bufsize < hdrsize) +if (bufsize <0 || (unsigned )bufsize < hdrsize)return -FDT_ERR_NOSPACE ;if (flags & ~FDT_CREATE_FLAGS_ALL )
Signature · 4 lines
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
PATCH 9/14 libfdt: fdt_resize(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_resize().
A negative buffer size will surely do us no good, so let's rule this case out first.
In the actual comparison we then know that a cast to an unsigned type is safe.
--- libfdt/fdt_sw.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
libfdt/fdt_sw.c +4 -1
@@ -152,6 +152,9 @@ int fdt_resize(void *fdt, void *buf, int bufsize)FDT_SW_PROBE (fdt); +if (bufsize <0 ) +return -FDT_ERR_NOSPACE ; + headsize =fdt_off_dt_struct (fdt) +fdt_size_dt_struct (fdt); tailsize =fdt_size_dt_strings (fdt);
@@ -159,7 +162,7 @@ int fdt_resize(void *fdt, void *buf, int bufsize) headsize + tailsize > fdt_totalsize(fdt))return -FDT_ERR_INTERNAL ; -if ((headsize + tailsize) > bufsize) +if ((headsize + tailsize) > (unsigned )bufsize)return -FDT_ERR_NOSPACE ; oldtail = (char *)fdt +fdt_totalsize (fdt) - tailsize;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-10-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 09/14] libfdt: fdt_resize(): Fix comparison warning
Quoted 9 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_resize().A negative buffer size will surely do us no good, so let's rule this case out first.
In the actual comparison we then know that a cast to an unsigned type is safe.Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 27 lines
--- libfdt/fdt_sw.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)libfdt/fdt_sw.c +4
-1@@ -152,6 +152,9 @@ int fdt_resize(void *fdt, void *buf, int bufsize)FDT_SW_PROBE (fdt); +if (bufsize <0 ) +return -FDT_ERR_NOSPACE ; + headsize =fdt_off_dt_struct (fdt) +fdt_size_dt_struct (fdt); tailsize =fdt_size_dt_strings (fdt);@@ -159,7 +162,7 @@ int fdt_resize(void *fdt, void *buf, int bufsize) headsize + tailsize > fdt_totalsize(fdt))return -FDT_ERR_INTERNAL ; -if ((headsize + tailsize) > bufsize) +if ((headsize + tailsize) > (unsigned )bufsize)return -FDT_ERR_NOSPACE ; oldtail = (char *)fdt +fdt_totalsize (fdt) - tailsize;
Signature · 4 lines
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
PATCH 10/14 libfdt: libfdt_wip: Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_setprop_inplace_namelen_partial().
Since fdt_getprop_namelen() can return negative error values, check for those first and bail out early.
Later on we can then safely cast the value to an unsigned type.
--- libfdt/fdt_wip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_wip.c +2 -2
@@ -20,10 +20,10 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, propval =fdt_getprop_namelen_w (fdt, nodeoffset, name, namelen, &proplen); -if (!propval) +if (!propval || proplen <0 )return proplen; -if (proplen < (len + idx)) +if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ;memcpy ((char *)propval + idx, val, len);
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-11-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 10/14] libfdt: libfdt_wip: Fix comparison warning
Quoted 17 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_setprop_inplace_namelen_partial().Since
fdt_getprop_namelen()can return negative error values, check for those first and bail out early.
Later on we can then safely cast the value to an unsigned type.Signed-off-by Andre Przywara <[email protected]>--- libfdt/fdt_wip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_wip.c +0
-0
libfdt/fdt_wip.c int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, · line 24
Quoted 6 lines
libfdt/fdt_wip.c +1
-1@@ -20,10 +20,10 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, propval =fdt_getprop_namelen_w (fdt, nodeoffset, name, namelen, &proplen); -if (!propval) +if (!propval || proplen <0 )return proplen;
We don't actually need that test - if proplen < 0 with propval non-NULL, that's a bug in fdt_getprop_namelen_w().
Quoted 5 lines
libfdt/fdt_wip.c +1
-1-if (proplen < (len + idx)) +if ((unsigned )proplen < (len + idx))return -FDT_ERR_NOSPACE ;memcpy ((char *)propval + idx, val, len);
Signature · 4 lines
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
PATCH 11/14 libfdt: overlay: Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in overlay_update_local_node_references().
This happens because the division of a signed int by an unsigned int promotes the dividend to unsigned first (ANSI C standard 6.1.3.8).
As in this case we basically just divide by 4, we can do the division separately earlier, which preserves the original type.
--- libfdt/fdt_overlay.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
libfdt/fdt_overlay.c +2 -1
@@ -241,6 +241,7 @@ static int overlay_update_local_node_references(void *fdto,if (fixup_len %sizeof (uint32_t ))return -FDT_ERR_BADOVERLAY ; + fixup_len /=sizeof (uint32_t ); tree_val =fdt_getprop (fdto, tree_node, name, &tree_len);if (!tree_val) {
@@ -250,7 +251,7 @@ static int overlay_update_local_node_references(void *fdto,return tree_len; } -for (i =0 ; i < (fixup_len /sizeof (uint32_t )); i++) { +for (i =0 ; i < fixup_len; i++) {fdt32_t adj_val;uint32_t poffset;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-12-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 11/14] libfdt: overlay: Fix comparison warning
Quoted 10 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
overlay_update_local_node_references().This happens because the division of a signed int by an unsigned int promotes the dividend to unsigned first (ANSI C standard 6.1.3.8).
As in this case we basically just divide by 4, we can do the division separately earlier, which preserves the original type.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 25 lines
--- libfdt/fdt_overlay.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)libfdt/fdt_overlay.c +2
-1@@ -241,6 +241,7 @@ static int overlay_update_local_node_references(void *fdto,if (fixup_len %sizeof (uint32_t ))return -FDT_ERR_BADOVERLAY ; + fixup_len /=sizeof (uint32_t ); tree_val =fdt_getprop (fdto, tree_node, name, &tree_len);if (!tree_val) {@@ -250,7 +251,7 @@ static int overlay_update_local_node_references(void *fdto,return tree_len; } -for (i =0 ; i < (fixup_len /sizeof (uint32_t )); i++) { +for (i =0 ; i < fixup_len; i++) {fdt32_t adj_val;uint32_t poffset;
Signature · 4 lines
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
PATCH 12/14 libfdt: fdt_get_string(): Fix sequential write comparison warnings
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in comparisons in fdt_get_string().
The two occassions dealing with the sequential write case are a bit tricky, since we deliberately abuse the negative values here.
As we have just established that stroffset is negative, we can use casts to appease the compiler.
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +2 -2
@@ -68,9 +68,9 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) } }else if (fdt_magic (fdt) ==FDT_SW_MAGIC ) {if ((stroffset >=0 ) - || (stroffset < -fdt_size_dt_strings (fdt))) + || (stroffset < -(int )fdt_size_dt_strings (fdt))) goto fail; -if ((-stroffset) < len) +if ((unsigned )(-stroffset) < len) len = -stroffset; }else { err = -FDT_ERR_INTERNAL ;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-13-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 12/14] libfdt: fdt_get_string(): Fix sequential write comparison warnings
Quoted 19 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_get_string().The two occassions dealing with the sequential write case are a bit tricky, since we deliberately abuse the negative values here.
As we have just established that stroffset is negative, we can use casts to appease the compiler.
Signed-off-by Andre Przywara <[email protected]>--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_ro.c +0
-0
libfdt/fdt_ro.c const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) · line 71
Quoted 5 lines
libfdt/fdt_ro.c +1
-1@@ -68,9 +68,9 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) } }else if (fdt_magic (fdt) ==FDT_SW_MAGIC ) {if ((stroffset >=0 ) - || (stroffset < -fdt_size_dt_strings (fdt))) + || (stroffset < -(int )fdt_size_dt_strings (fdt)))
So, I'm pretty sure at this point we've tested that fdt_size_dt_strings < INT_MAX. But to make it more obvious that it's safe, I think it would be preferable to case (-stroffset) to unsigned rather than casting size_dt_strings to signed.
Quoted 6 lines
libfdt/fdt_ro.c +1
-1goto fail; -if ((-stroffset) < len) +if ((unsigned )(-stroffset) < len) len = -stroffset; }else { err = -FDT_ERR_INTERNAL ;
Signature · 4 lines
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 12/14] libfdt: fdt_get_string(): Fix sequential write comparison warnings
Quoted 15 lines · David Gibson
Quoted 14 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_get_string().The two occassions dealing with the sequential write case are a bit tricky, since we deliberately abuse the negative values here.
As we have just established that stroffset is negative, we can use casts to appease the compiler.
Signed-off-by Andre Przywara <[email protected]>--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines
libfdt/fdt_ro.c const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) · line 71
Quoted 10 lines
libfdt/fdt_ro.c +1
-1@@ -68,9 +68,9 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) } }else if (fdt_magic (fdt) ==FDT_SW_MAGIC ) {if ((stroffset >=0 ) - || (stroffset < -fdt_size_dt_strings (fdt))) + || (stroffset < -(int )fdt_size_dt_strings (fdt)))So, I'm pretty sure at this point we've tested that
fdt_size_dt_strings<INT_MAX. But to make it more obvious that it's safe, I think it would be preferable to case (-stroffset) to unsigned rather than castingsize_dt_stringsto signed.
Ah, indeed, and this makes the whole thing suddenly much more readable. I took the freedom of adding: "unsigned int sw_stroffset = -stroffset;", to avoid those ugly casts altogether.
Cheers,
Andre
Quoted 8 lines
Quoted 7 lines
libfdt/fdt_ro.c +1
-1goto fail; -if ((-stroffset) < len) +if ((unsigned )(-stroffset) < len) len = -stroffset; }else { err = -FDT_ERR_INTERNAL ;
PATCH 13/14 libfdt: fdt_node_offset_by_phandle(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_node_offset_by_phandle().
Uses a better suited bitwise NOT operator to denote the special value of -1, which automatically results in an unsigned type.
--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_ro.c +1 -1
@@ -680,7 +680,7 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) {int offset; -if ((phandle ==0 ) || (phandle ==-1 )) +if ((phandle ==0 ) || (phandle == ~0U ))return -FDT_ERR_BADPHANDLE ;FDT_RO_PROBE (fdt);
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-14-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH 13/14] libfdt: fdt_node_offset_by_phandle(): Fix comparison warning
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_node_offset_by_phandle().Uses a better suited bitwise NOT operator to denote the special value of -1, which automatically results in an unsigned type.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 17 lines
--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_ro.c +1
-1@@ -680,7 +680,7 @@ int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) {int offset; -if ((phandle ==0 ) || (phandle ==-1 )) +if ((phandle ==0 ) || (phandle == ~0U ))return -FDT_ERR_BADPHANDLE ;FDT_RO_PROBE (fdt);
Signature · 4 lines
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
PATCH 14/14 libfdt: fdt_strerror(): Fix comparison warning
In reply to a message not in the archiveWith -Wsign-compare, compilers warn about a mismatching signedness in a comparison in fdt_strerror().
At this point we are sure that errval is negative, and since FDT_ERRTABSIZE is surely positive, we can do some casting to make the compiler happy.
--- libfdt/fdt_strerror.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_strerror.c +1 -1
@@ -48,7 +48,7 @@ const char *fdt_strerror(int errval)return "<valid offset/length>" ; else if (errval == 0) return "<no error>"; - else if (errval > -FDT_ERRTABSIZE) { + else if (errval > -(int)FDT_ERRTABSIZE) { const char *s = fdt_errtable[-errval].str; if (s)
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20200921165303.9115-15-andre.przywara-5wv7dgnIgG8@public.gmane.org
Re: [PATCH 14/14] libfdt: fdt_strerror(): Fix comparison warning
Quoted 8 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_strerror().At this point we are sure that errval is negative, and since
FDT_ERRTABSIZEis surely positive, we can do some casting to make the compiler happy.Signed-off-by Andre Przywara <[email protected]>
Since error values are typically conveyed as negative numbers (which is kind of the whole reason we have all these signed values in the first place), I think it would be slightly nicer to actually fold the case to signed into the definition of FDT_ERRTABSIZE.
Quoted 17 lines
--- libfdt/fdt_strerror.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_strerror.c +1
-1@@ -48,7 +48,7 @@ const char *fdt_strerror(int errval)return "<valid offset/length>" ; else if (errval == 0) return "<no error>"; - else if (errval > -FDT_ERRTABSIZE) { + else if (errval > -(int)FDT_ERRTABSIZE) { const char *s = fdt_errtable[-errval].str; if (s)
Signature · 4 lines
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 14/14] libfdt: fdt_strerror(): Fix comparison warning
Quoted 8 lines · David Gibson
Quoted 7 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_strerror().At this point we are sure that errval is negative, and since
FDT_ERRTABSIZEis surely positive, we can do some casting to make the compiler happy.
Quoted 6 lines
Quoted 1 line
Signed-off-by Andre Przywara <[email protected]>Since error values are typically conveyed as negative numbers (which is kind of the whole reason we have all these signed values in the first place), I think it would be slightly nicer to actually fold the case to signed into the definition of
FDT_ERRTABSIZE.
Right, makes more sense. I changed the comparison to:
if (-errval < FDT_ERRTABSIZE)
which is more in line with what we actually do below.
Cheers,
Andre
Quoted 18 lines
Quoted 17 lines
--- libfdt/fdt_strerror.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_strerror.c +1
-1@@ -48,7 +48,7 @@ const char *fdt_strerror(int errval)return "<valid offset/length>" ; else if (errval == 0) return "<no error>"; - else if (errval > -FDT_ERRTABSIZE) { + else if (errval > -(int)FDT_ERRTABSIZE) { const char *s = fdt_errtable[-errval].str; if (s)
Quoted 33 lines · Andre Przywara
When libfdt is compiled with -Wsign-compare or -Wextra, GCC emits quite
some warnings about the signedness of the operands not matching:
=================libfdt/fdt.c:140:18: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
if ((absoffset < offset)
.....
=================This does not occur under normal conditions in the dtc repo, but might show up when libfdt is embedded in another project. There have been reports from U-Boot and Trusted-Firmware-A.
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.
In libfdt, some types are somewhat suboptimal ("int bufsize" comes to mind); some signed types are due to them being returned along wih error values in other functions (node offsets).
So these fixes here have been based on the following assumptions:
- We cannot change the prototype of exported functions.
- It's better to change types (for local variables) than to cast.
- If we have established that a signed value is not negative, we can safely cast it to an unsigned type.I split up the fixes in small chunks, to make them easier to review. The first four patches change types, the next six ones use casts after we made sure the values are not negative.
Quoted 7 lines
This is only covering libfdt for now (which is what those other projects care about). There are more issues with dtc, but they can be addressed later.
Not sure if some of these checks should be gated by
can_assume()calls.Please have a look, happy to discuss the invididual cases.
Thanks so much for these. I've applied about half of them, the rest I have mostly minor comments for.
Signature · 4 lines
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
A message that is not in the archive (sent off-list or not yet received): [email protected]
On 25/09/2020 05:12, David Gibson wrote:
Hi David,
Quoted 38 lines · Andre Przywara
Quoted 37 lines
When libfdt is compiled with -Wsign-compare or -Wextra, GCC emits quite
some warnings about the signedness of the operands not matching:
=================libfdt/fdt.c:140:18: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
if ((absoffset < offset)
.....
=================This does not occur under normal conditions in the dtc repo, but might show up when libfdt is embedded in another project. There have been reports from U-Boot and Trusted-Firmware-A.
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.
In libfdt, some types are somewhat suboptimal ("int bufsize" comes to mind); some signed types are due to them being returned along wih error values in other functions (node offsets).
So these fixes here have been based on the following assumptions:
- We cannot change the prototype of exported functions.
- It's better to change types (for local variables) than to cast.
- If we have established that a signed value is not negative, we can safely cast it to an unsigned type.I split up the fixes in small chunks, to make them easier to review. The first four patches change types, the next six ones use casts after we made sure the values are not negative.
This is only covering libfdt for now (which is what those other projects care about). There are more issues with dtc, but they can be addressed later.
Quoted 6 lines
Quoted 3 lines
Not sure if some of these checks should be gated by
can_assume()calls.Please have a look, happy to discuss the invididual cases.
Thanks so much for these. I've applied about half of them, the rest I have mostly minor comments for.
Many thanks for reviewing (and applying)! I will send the remaining patches ASAP, fixed accordingly.
Cheers,
Andre