btrfs-progs: fix compiler warning
PATCH 1/1 btrfs-progs: fix compiler warning
gcc 4.9.0 gives a warning: array subscript is above array bounds
Checking for "greater or equal" instead of just "equal" fixes this.
--- cmds-restore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
cmds-restore.c +1 -1
@@ -169,7 +169,7 @@ again:break ; } -if (level ==BTRFS_MAX_LEVEL ) +if (level >=BTRFS_MAX_LEVEL )return 1 ; slot = path->slots[level] +1 ;
Signature · 1 line
2.0.0
Quoted 3 lines · Christian Hesse
gcc 4.9.0 gives a warning: array subscript is above array bounds
Checking for "greater or equal" instead of just "equal" fixes this.
That fixes the warning, but I don't see the code path that leads to
level >= BTRFS_MAX_LEVEL
On the first pass, when level = 1 and the first while() reaches at most BTRFS_MAX_LEVEL, the equality test is enough. So it has to go through the second while() where level is decremented and in the range [1..BTRFS_MAX_LEVEL-1] before 'goto again' jumps to the first while again.
David Sterba <[email protected]> on Tue, 2014/06/03 18:52:
Quoted 5 lines · Christian Hesse
Quoted 3 lines
gcc 4.9.0 gives a warning: array subscript is above array bounds
Checking for "greater or equal" instead of just "equal" fixes this.
Quoted 8 lines
That fixes the warning, but I don't see the code path that leads to
level >=BTRFS_MAX_LEVELOn the first pass, when level = 1 and the first
while()reaches at mostBTRFS_MAX_LEVEL, the equality test is enough. So it has to go through the secondwhile()where level is decremented and in the range [1..BTRFS_MAX_LEVEL-1] before 'goto again' jumps to the first while again.
I suppose gcc does not know how much level can be increased within function next_leaf(). level > BTRFS_MAX_LEVEL is never met at runtime, but this way gcc knows that level can never be bigger than BTRFS_MAX_LEVEL.
Any better way to fix this?
Signature · 4 lines
Schoene Gruesse
Chris
O< ascii ribbon campaign
stop html mail - www.asciiribbon.org-------- Original Message --------
Subject: [PATCH 1/1] btrfs-progs: fix compiler warning
From: Christian Hesse <[email protected]>
To: [email protected]
Date: 2014年06月03日 19:29
Quoted 14 lines
gcc 4.9.0 gives a warning: array subscript is above array bounds
Checking for "greater or equal" instead of just "equal" fixes this.
Signed-off-by Christian Hesse <[email protected]>--- cmds-restore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)cmds-restore.c +0
-0
cmds-restore.c again: · line 175
Quoted 8 lines
cmds-restore.c +1
-1@@ -169,7 +169,7 @@ again:break ; } -if (level ==BTRFS_MAX_LEVEL ) +if (level >=BTRFS_MAX_LEVEL )return 1 ; slot = path->slots[level] +1 ;
Also I faied to reproduce the bug.
Using gcc-4.9.0-3 from Archlinux core repo.
It seems to be related to default gcc flags from distribution?
Thanks,
Qu
Qu Wenruo <[email protected]> on Wed, 2014/06/04 14:48:
Quoted 23 lines
-------- Original Message --------
Subject: [PATCH 1/1] btrfs-progs: fix compiler warning
From: Christian Hesse <[email protected]>
To: [email protected]
Date: 2014年06月03日 19:29Quoted 17 lines
gcc 4.9.0 gives a warning: array subscript is above array bounds
Checking for "greater or equal" instead of just "equal" fixes this.
Signed-off-by Christian Hesse <[email protected]>--- cmds-restore.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)cmds-restore.c +0
-0@@ -169,7 +169,7 @@ again:break ; }
Quoted 8 lines
cmds-restore.c again: · line 175
Quoted 5 lines
cmds-restore.c +1
-1-if (level ==BTRFS_MAX_LEVEL ) +if (level >=BTRFS_MAX_LEVEL )return 1 ; slot = path->slots[level] +1 ;Also I faied to reproduce the bug.
Using gcc-4.9.0-3 from Archlinux core repo.
Exactly the same here. ;)
Quoted 1 line
It seems to be related to default gcc flags from distribution?
Probably. I did compile with optimization, so adding -O2 may do the trick:
make CFLAGS="${CFLAGS} -O2" all
Signature · 4 lines
Schoene Gruesse
Chris
O< ascii ribbon campaign
stop html mail - www.asciiribbon.orgQuoted 5 lines · Christian Hesse
Quoted 1 line
It seems to be related to default gcc flags from distribution?
Probably. I did compile with optimization, so adding -O2 may do the trick:
make CFLAGS="${CFLAGS} -O2" all
The warning appears with -O2, so the question is if gcc is not able to reason about the values (ie. a false positive) or if there's a bug that I don't see.
David Sterba <[email protected]> on Wed, 2014/06/04 18:44:
Quoted 10 lines · Christian Hesse
Quoted 5 lines
Quoted 1 line
It seems to be related to default gcc flags from distribution?
Probably. I did compile with optimization, so adding -O2 may do the trick:
make CFLAGS="${CFLAGS} -O2" all
The warning appears with -O2, so the question is if gcc is not able to reason about the values (ie. a false positive) or if there's a bug that I don't see.
I do not see a bug either. So probably this is a false positive...
Looks like the warning is triggered as soon as -ftree-vrp is added to CFLAGS. From gcc man page:
-ftree-vrp
Perform Value Range Propagation on trees. This is similar to the constant propagation pass, but instead of values, ranges of values are propagated. This allows the optimizers to remove unnecessary range checks like array bound checks and null pointer checks. This is enabled by default at -O2 and higher. Null pointer check elimination is only done if -fdelete-null-pointer-checks is enabled.
Is it possibly that gcc optimized away any checks?
Signature · 4 lines
Schoene Gruesse
Chris
O< ascii ribbon campaign
stop html mail - www.asciiribbon.orgQuoted 6 lines · Christian Hesse
David Sterba <[email protected]> on Wed, 2014/06/04 18:44:
Quoted 5 lines · Christian Hesse
Quoted 4 lines
Quoted 1 line
It seems to be related to default gcc flags from distribution?
Probably. I did compile with optimization, so adding -O2 may do the trick:
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
make CFLAGS="${CFLAGS} -O2" all
The warning appears with -O2, so the question is if gcc is not able to reason about the values (ie. a false positive) or if there's a bug that I don't see.
I do not see a bug either. So probably this is a false positive...
Yeah I think so.
Quoted 1 line
Looks like the warning is triggered as soon as -ftree-vrp is added to CFLAGS.
Good catch
$ make CFLAGS="${CFLAGS} -O2 -Wall -fno-tree-vrp" alldoes not print the warning (gcc 4.8.1).
Quoted 11 lines
From gcc man page:
-ftree-vrp
Perform Value Range Propagation on trees. This is similar to the constant propagation pass, but instead of values, ranges of values are propagated. This allows the optimizers to remove unnecessary range checks like array bound checks and null pointer checks. This is enabled by default at -O2 and higher. Null pointer check elimination is only done if -fdelete-null-pointer-checks is enabled.Is it possibly that gcc optimized away any checks?
I'm not sure, that way lies gcc optimization maze.
Looking at the assembly from tree-vrp and no-tree-vrp it's probably the case, the vrp-optimized version is restructured (and shorter) and looks almost the same. Some of the bounds checks are missing (at least cannot be easily paired). Better ask gcc people.