libfdt: Fix signed/unsigned comparison warnings
v2 cover libfdt: Fix signed/unsigned comparison warnings
Those are the six remaining patches of the initial post to fix the C comparison warnings.
I reworked the fixes according to David's comments, and took quite a different approach for some of them.
Changelog below.
The series is against https://github.com/dgibson/dtc/commits/main
------------------------------------
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.
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.
Please have a look, happy to discuss the invididual cases.
Cheers,
Andre
Changelog v1 .. v2:
- Omit the 8 patches that have already been applied.
- fdt_add_string_ (was 04/14): make every type unsigned; use subtractions
- fdt_move (was 05/14): add can_assume()
- fdt_create_with_flags (was 08/14): just make hdrsize signed
- libfdt_wip (was 10/14): drop redundant check for proplen being non-negative
- fdt_get_string (was 12/14): introduce unsigned variable (instead of casting)
- fdt_strerror (was 14/14): move int cast into FDT_ERRTABSIZE definition
Andre Przywara (6):
libfdt: fdt_add_string_(): Fix comparison warning
libfdt: fdt_move(): Fix comparison warnings
libfdt: fdt_create_with_flags(): Fix comparison warning
libfdt: libfdt_wip: Fix comparison warning
libfdt: fdt_get_string(): Fix sequential write comparison warnings
libfdt: fdt_strerror(): Fix comparison warning
libfdt/fdt.c | 5 ++++- libfdt/fdt_ro.c | 10 ++++++---- libfdt/fdt_strerror.c | 4 ++-- libfdt/fdt_sw.c | 18 +++++++++--------- libfdt/fdt_wip.c | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-)
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20201001164630.4980-1-andre.przywara-5wv7dgnIgG8@public.gmane.org
PATCH v2 1/6 libfdt: fdt_add_string_(): Fix comparison warning
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.
--- libfdt/fdt_sw.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
libfdt/fdt_sw.c +7 -7
@@ -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 ; +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)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 */
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20201001164630.4980-2-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 1/6] 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_().Make all variables unsigned, and express the negative offset trick via subtractions in the code.
Signed-off-by Andre Przywara <[email protected]>
Applied. I think there are some followup improvements we could make here, though.
Quoted 9 lines
--- libfdt/fdt_sw.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)libfdt/fdt_sw.c +0
-0
libfdt/fdt_sw.c int fdt_end_node(void *fdt) · line 254
Quoted 8 lines
libfdt/fdt_sw.c +2
-3@@ -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.
libfdt/fdt_sw.c int fdt_end_node(void *fdt) · line 259
Quoted 7 lines
libfdt/fdt_sw.c +3
-2+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?).
Quoted 10 lines
libfdt/fdt_sw.c +2
-2return 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 */
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 v2 2/6 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 (!can_assume (VALID_INPUT ) && 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): 20201001164630.4980-3-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 2/6] 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]>
Applied, thanks.
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 (!can_assume (VALID_INPUT ) && 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 v2 3/6 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().
By making hdrsize a signed integer (we are sure it's a very small number), we avoid all the casts and have matching types.
--- libfdt/fdt_sw.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_sw.c +2 -2
@@ -108,8 +108,8 @@ static void *fdt_grab_space_(void *fdt, size_t len)int fdt_create_with_flags (void *buf,int bufsize,uint32_t flags) { -const size_t hdrsize =FDT_ALIGN (sizeof (struct fdt_header ), -sizeof (struct fdt_reserve_entry )); +const int hdrsize =FDT_ALIGN (sizeof (struct fdt_header ), +sizeof (struct fdt_reserve_entry ));void *fdt = buf;if (bufsize < hdrsize)
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20201001164630.4980-4-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 3/6] 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().By making hdrsize a signed integer (we are sure it's a very small number), we avoid all the casts and have matching types.
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@@ -108,8 +108,8 @@ static void *fdt_grab_space_(void *fdt, size_t len)int fdt_create_with_flags (void *buf,int bufsize,uint32_t flags) { -const size_t hdrsize =FDT_ALIGN (sizeof (struct fdt_header ), -sizeof (struct fdt_reserve_entry )); +const int hdrsize =FDT_ALIGN (sizeof (struct fdt_header ), +sizeof (struct fdt_reserve_entry ));void *fdt = buf;if (bufsize < hdrsize)
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 v2 4/6 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().
fdt_getprop_namelen() will only return negative error values in "proplen" if the return value is NULL. So we can rely on "proplen" being positive in our case and can safely cast it to an unsigned type.
--- libfdt/fdt_wip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_wip.c +1 -1
@@ -23,7 +23,7 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if (!propval)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): 20201001164630.4980-5-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 4/6] libfdt: libfdt_wip: Fix comparison warning
Quoted 8 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in a comparison in
fdt_setprop_inplace_namelen_partial().
fdt_getprop_namelen()will only return negative error values in "proplen" if the return value is NULL. So we can rely on "proplen" being positive in our case and can safely cast it to an unsigned type.Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 17 lines
--- libfdt/fdt_wip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_wip.c +1
-1@@ -23,7 +23,7 @@ int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,if (!propval)return proplen; -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 v2 5/6 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().
Introduce a new usigned variable, which holds the actual (negated) stroffset value, so we avoid negating all the other variables and have proper types everywhere.
--- libfdt/fdt_ro.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +6 -4
@@ -67,11 +67,13 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) len =fdt_size_dt_strings (fdt) - stroffset; } }else if (fdt_magic (fdt) ==FDT_SW_MAGIC ) { -if ((stroffset >=0 ) - || (stroffset < -fdt_size_dt_strings (fdt))) +unsigned int sw_stroffset = -stroffset; + +if ((stroffset >=0 ) || + (sw_stroffset >fdt_size_dt_strings (fdt))) goto fail; -if ((-stroffset) < len) - len = -stroffset; +if (sw_stroffset < len) + len = sw_stroffset; }else { err = -FDT_ERR_INTERNAL ; goto fail;
Signature · 1 line
2.17.5
A message that is not in the archive (sent off-list or not yet received): 20201001164630.4980-6-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 5/6] libfdt: fdt_get_string(): Fix sequential write comparison warnings
Quoted 8 lines · Andre Przywara
With -Wsign-compare, compilers warn about a mismatching signedness in comparisons in
fdt_get_string().Introduce a new usigned variable, which holds the actual (negated) stroffset value, so we avoid negating all the other variables and have proper types everywhere.
Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 26 lines
--- libfdt/fdt_ro.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)libfdt/fdt_ro.c +6
-4@@ -67,11 +67,13 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) len =fdt_size_dt_strings (fdt) - stroffset; } }else if (fdt_magic (fdt) ==FDT_SW_MAGIC ) { -if ((stroffset >=0 ) - || (stroffset < -fdt_size_dt_strings (fdt))) +unsigned int sw_stroffset = -stroffset; + +if ((stroffset >=0 ) || + (sw_stroffset >fdt_size_dt_strings (fdt))) goto fail; -if ((-stroffset) < len) - len = -stroffset; +if (sw_stroffset < len) + len = sw_stroffset; }else { err = -FDT_ERR_INTERNAL ; goto fail;
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 v2 6/6 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().
Force FDT_ERRTABSIZE to be signed (it's surely small enough to fit), so that the types match. Also move the minus sign to errval, as this is actually what we use in the next line.
--- libfdt/fdt_strerror.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_strerror.c +2 -2
@@ -40,7 +40,7 @@ static struct fdt_errtabent fdt_errtable[] = {FDT_ERRTABENT (FDT_ERR_NOPHANDLES ), FDT_ERRTABENT(FDT_ERR_BADFLAGS), }; -#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0])) +#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))const char *fdt_strerror (int errval) {
@@ -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 < 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): 20201001164630.4980-7-andre.przywara-5wv7dgnIgG8@public.gmane.org
David Gibson
AppliedRe: [PATCH v2 6/6] 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().Force
FDT_ERRTABSIZEto be signed (it's surely small enough to fit), so that the types match. Also move the minus sign to errval, as this is actually what we use in the next line.Signed-off-by Andre Przywara <[email protected]>
Applied, thanks.
Quoted 26 lines
--- libfdt/fdt_strerror.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_strerror.c +2
-2@@ -40,7 +40,7 @@ static struct fdt_errtabent fdt_errtable[] = {FDT_ERRTABENT (FDT_ERR_NOPHANDLES ), FDT_ERRTABENT(FDT_ERR_BADFLAGS), }; -#define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0])) +#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))const char *fdt_strerror (int errval) {@@ -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 < 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
Quoted 38 lines · Andre Przywara
Those are the six remaining patches of the initial post to fix the C comparison warnings.
I reworked the fixes according to David's comments, and took quite a different approach for some of them.
Changelog below.The series is against https://github.com/dgibson/dtc/commits/main
------------------------------------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.
Quoted 7 lines
I split up the fixes in small chunks, to make them easier to review.
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.
Please have a look, happy to discuss the invididual cases.
Thanks again for this work. I've applied all the remaining patches, although I have some comments for some followups I think would be good.
At this point can we turn on -Wsign-compare by default? That sounds like a good idea to stop these problems creeping back in.
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 02/10/2020 02:03, David Gibson wrote:
Hi David,
Quoted 45 lines · Andre Przywara
Quoted 44 lines
Those are the six remaining patches of the initial post to fix the C comparison warnings.
I reworked the fixes according to David's comments, and took quite a different approach for some of them.
Changelog below.The series is against https://github.com/dgibson/dtc/commits/main
------------------------------------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.
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 5 lines
Quoted 1 line
Please have a look, happy to discuss the invididual cases.
Thanks again for this work. I've applied all the remaining patches, although I have some comments for some followups I think would be good.
Thanks, that's much appreciated! Happy to discuss any further comments.
Quoted 2 lines
At this point can we turn on -Wsign-compare by default? That sounds like a good idea to stop these problems creeping back in.
Not quite yet: the patches were only covering "make libfdt". I see more reports for dtc and the tests. I should probably address those as long as this is still in my "L1 cache" ...
Unless you want to turn on -Wsign-compare just for libfdt.
Cheers,
Andre
A message that is not in the archive (sent off-list or not yet received): 316e6e0f-e15e-89ee-3008-d2ed038ffd79-5wv7dgnIgG8@public.gmane.org
Quoted 57 lines · André Przywara
On 02/10/2020 02:03, David Gibson wrote:
Hi David,
Quoted 45 lines · Andre Przywara
Quoted 44 lines
Those are the six remaining patches of the initial post to fix the C comparison warnings.
I reworked the fixes according to David's comments, and took quite a different approach for some of them.
Changelog below.The series is against https://github.com/dgibson/dtc/commits/main
------------------------------------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.
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 5 lines
Quoted 1 line
Please have a look, happy to discuss the invididual cases.
Thanks again for this work. I've applied all the remaining patches, although I have some comments for some followups I think would be good.
Thanks, that's much appreciated! Happy to discuss any further comments.
Quoted 7 lines
Quoted 2 lines
At this point can we turn on -Wsign-compare by default? That sounds like a good idea to stop these problems creeping back in.
Not quite yet: the patches were only covering "make libfdt". I see more reports for dtc and the tests. I should probably address those as long as this is still in my "L1 cache" ...
Unless you want to turn on -Wsign-compare just for libfdt.
If you think you can get us there for the whole project reasonably soon, then wait for that. If it looks like you might need to shelve this for a while, then it would be good to get it enabled for just libfdt.
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]
Quoted 66 lines · David Gibson
Quoted 57 lines · André Przywara
On 02/10/2020 02:03, David Gibson wrote:
Hi David,
Quoted 45 lines · Andre Przywara
Quoted 44 lines
Those are the six remaining patches of the initial post to fix the C comparison warnings.
I reworked the fixes according to David's comments, and took quite a different approach for some of them.
Changelog below.The series is against https://github.com/dgibson/dtc/commits/main
------------------------------------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.
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 5 lines
Quoted 1 line
Please have a look, happy to discuss the invididual cases.
Thanks again for this work. I've applied all the remaining patches, although I have some comments for some followups I think would be good.
Thanks, that's much appreciated! Happy to discuss any further comments.
Quoted 7 lines
Quoted 2 lines
At this point can we turn on -Wsign-compare by default? That sounds like a good idea to stop these problems creeping back in.
Not quite yet: the patches were only covering "make libfdt". I see more reports for dtc and the tests. I should probably address those as long as this is still in my "L1 cache" ...
Unless you want to turn on -Wsign-compare just for libfdt.
Quoted 4 lines
If you think you can get us there for the whole project reasonably soon, then wait for that. If it looks like you might need to shelve this for a while, then it would be good to get it enabled for just libfdt.
So I have "make tests" covered now, that looks mostly reasonable. The rest is more nasty, I needed to paper over a few of them to make some progress. But "make all check" comes out clean now.
I will try to make reasonable patches out this mess next week.
Cheers,
Andre