Fix an overflow case in fdt_offset_ptr() detected by GCC 4.3.
PATCH 1/1 Fix an overflow case in fdt_offset_ptr() detected by GCC 4.3.
Using Gcc 4.3 detected this problem:
../dtc/libfdt/fdt.c: In function 'fdt_next_tag':
../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false
To fix the problem, treat the offset as an unsigned int.
The problem report and proposed fix were provided
by Steve Papacharalambous <[email protected]>.
--- libfdt/fdt.c | 2 +- libfdt/libfdt.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt.c +1 -1
@@ -74,7 +74,7 @@ int fdt_check_header(const void *fdt)return 0 ; } -const void *fdt_offset_ptr (const void *fdt,int offset,int len) +const void *fdt_offset_ptr (const void *fdt,int offset,unsigned int len) {const char *p;
libfdt/libfdt.h +1 -1
@@ -122,7 +122,7 @@/* Low-level functions (you probably don't need these) */ /**********************************************************************/ -const void *fdt_offset_ptr (const void *fdt,int offset,int checklen); +const void *fdt_offset_ptr (const void *fdt,int offset,unsigned int checklen);static inline void *fdt_offset_ptr_w (void *fdt,int offset,int checklen) {return (void *)(uintptr_t )fdt_offset_ptr (fdt, offset, checklen);
Signature · 1 line
1.6.0.2.g2ebc0
Quoted 6 lines · Jon Loeliger
Using Gcc 4.3 detected this problem:
../dtc/libfdt/fdt.c: In function 'fdt_next_tag':
../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false
Um... I'm a little baffled by this warning, given that the line in question is actually testing that (X+c) < X is false..
Quoted 1 line
To fix the problem, treat the offset as an unsigned int.
Nor am I entirely sure how that's supposed to help, since an unsigned overflow is also possible.
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
Jon Loeliger
AppliedQuoted 7 lines
Using Gcc 4.3 detected this problem:
../dtc/libfdt/fdt.c: In function 'fdt_next_tag':
../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false
Quoted 6 lines
To fix the problem, treat the offset as an unsigned int.
The problem report and proposed fix were provided
by Steve Papacharalambous <[email protected]>.Signed-off-by Jon Loeliger <[email protected]>
Applied.
jdl
A message that is not in the archive (sent off-list or not yet received): E1KkQ3b-0000uT-6H-CYoMK+44s/[email protected]
Quoted 7 lines · Jon Loeliger
Quoted 7 lines
Using Gcc 4.3 detected this problem:
../dtc/libfdt/fdt.c: In function 'fdt_next_tag':
../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur when assuming that (X + c) < X is always false
Quoted 8 lines
Quoted 6 lines
To fix the problem, treat the offset as an unsigned int.
The problem report and proposed fix were provided
by Steve Papacharalambous <[email protected]>.Signed-off-by Jon Loeliger <[email protected]>Applied.
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
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 7 lines · Jon Loeliger
Quoted 1 line
Quoted 1 line
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
I saw your reply, and you are welcome to read up
on the fist fight over in GCC land too.
In the meantime, it fixes a real problem that we
have at Freescale.
Steve, can you remind us the URL of the GCC discussion?
Thanks,
jdl
A message that is not in the archive (sent off-list or not yet received): E1KkUOE-0001OX-EF-CYoMK+44s/[email protected]
Quoted 8 lines · Jon Loeliger
Quoted 7 lines · Jon Loeliger
Quoted 1 line
Quoted 1 line
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
Quoted 5 lines
I saw your reply, and you are welcome to read up
on the fist fight over in GCC land too.In the meantime, it fixes a real problem that we
have at Freescale.
Ok.. and can you explain this real problem?
If this is just to work around a silly gcc warning that appears in some circumstances and not others, that's fine, but the commit message should say so.
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 12 lines · David Gibson
Quoted 11 lines · Jon Loeliger
Quoted 7 lines · Jon Loeliger
Quoted 1 line
Quoted 1 line
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
I saw your reply, and you are welcome to read up
on the fist fight over in GCC land too.
Quoted 9 lines
Quoted 2 lines
In the meantime, it fixes a real problem that we
have at Freescale.Ok.. and can you explain this real problem?
If this is just to work around a silly gcc warning that appears in some circumstances and not others, that's fine, but the commit message should say so.
Hi David,
The error that is generated when using gcc-4.3.2 20080819 is:
[snip]/opt/freescale/usr/local/gcc-4.3.8-eglibc-2.8.8/powerpc-linux-gnu/bin/powerpc-linux-gnu-gcc -include config/autoconf.h -m32 -Wa,-me500 -nostdinc -I /opt/freescale/usr/local/gcc-4.3.8-eglibc-2.8.8/powerpc-linux-gnu/lib/gcc/powerpc-linux-gnu/4.3.2/include -I /opt/freescale/usr/local/gcc-4.3.8-eglibc-2.8.8/powerpc-linux-gnu/lib/gcc/powerpc-linux-gnu/4.3.2/include-fixed -Iinclude -Ibin/include -I../dtc/libfdt -I../libos/include -g -std=gnu99 -include include/libos-client.h -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -O2 -msoft-float -pipe -ffixed-r2 -mmultiple -mno-altivec -funit-at-a-time -mno-string -fomit-frame-pointer -Werror -include include/libfdt_env.h -c -o bin/libfdt/fdt.o ../dtc/libfdt/fdt.c
cc1: warnings being treated as errors../dtc/libfdt/fdt.c: In function 'fdt_next_tag':../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur
when assuming that (X + c) < X is always false../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur
when assuming that (X + c) < X is always false../dtc/libfdt/fdt.c:82: error: assuming signed overflow does not occur
when assuming that (X + c) < X is always false
[/snip]
which does not occur with earlier versions of gcc. For example the same command builds without errors with gcc-4.2.3.
A good starting point for the discussion on this subject is:
http://gcc.gnu.org/ml/gcc/2008-04/msg00618.html
I suspect that you're right and this error doesn't occur in all circumstances, so perhaps the commit message should be changed,
Best regards,
Steve
Quoted 9 lines · David Gibson
Quoted 8 lines · Jon Loeliger
Quoted 5 lines · Jon Loeliger
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
I saw your reply, and you are welcome to read up
on the fist fight over in GCC land too.
Quoted 8 lines
Quoted 2 lines
In the meantime, it fixes a real problem that we
have at Freescale.Ok.. and can you explain this real problem?
If this is just to work around a silly gcc warning that appears in some circumstances and not others, that's fine, but the commit message should say so.
It seems that the C standard says that signed overflow is undefined, but not unsigned overflow. Presumably that was to accomodate non-twos-complement machines (which we don't really care about), but GCC abuses it to completely optimize away any paths that depend on such overflow, and the fact that they had to add a warning about it didn't strike them as a reason why they should maybe not go ahead with such an optimization by default, even if it's permitted by the spec.
-Scott
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 19 lines · Scott Wood
Quoted 9 lines · David Gibson
Quoted 8 lines · Jon Loeliger
Quoted 5 lines · Jon Loeliger
Uh.. Jon.. did you see my reply to this. I'm not at all convinced this patches a real problem. I suspect it's just replacing a problem that gcc could detect with a similar one that gcc can't (and for which we already had a test to deal with, anyway).
I saw your reply, and you are welcome to read up
on the fist fight over in GCC land too.Quoted 8 lines
Quoted 2 lines
In the meantime, it fixes a real problem that we
have at Freescale.Ok.. and can you explain this real problem?
If this is just to work around a silly gcc warning that appears in some circumstances and not others, that's fine, but the commit message should say so.
Quoted 7 lines
It seems that the C standard says that signed overflow is undefined, but not unsigned overflow. Presumably that was to accomodate non-twos-complement machines (which we don't really care about), but GCC abuses it to completely optimize away any paths that depend on such overflow, and the fact that they had to add a warning about it didn't strike them as a reason why they should maybe not go ahead with such an optimization by default, even if it's permitted by the spec.
Ah, ok! The old C undefined behaviour problem. Ok, now this patch makes sense - we were indeed assuming that overflow behaved as you'd expect for a two's complement machine
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