nfsd: avoid undefined signed overflow
Subsystems (2)
PATCH v2 1/1 nfsd: avoid undefined signed overflow
In C, signed integer overflow results in undefined behavior, but unsigned overflow wraps around. So do the subtraction first, then cast to signed.
--- fs/nfsd/nfs4state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/nfs4state.c +1 -1
@@ -3427,7 +3427,7 @@ grace_disallows_io(struct net *net, struct inode *inode)/* Returns true iff a is later than b: */ static bool stateid_generation_after (stateid_t *a,stateid_t *b) { -return (s32 )a->si_generation - (s32 )b->si_generation >0 ; +return (s32)(a->si_generation - b->si_generation) >0 ; }static __be32 check_stateid_generation (stateid_t *in,stateid_t *ref,bool has_session)
Signature · 1 line
1.8.2.3
Quoted 2 lines · Jim Rees
In C, signed integer overflow results in undefined behavior, but unsigned overflow wraps around. So do the subtraction first, then cast to signed.
Thanks! Applying for 3.11.
(I wonder if it actually bites anyone in practice? Are there common compilers or architectures where this makes a difference? Even if so I suppose that many generations is probably unlikely enough not to make this worth backporting to stable branches.)
--b.
Quoted 27 lines
Suggested-by Joakim Tjernlund <[email protected]>Signed-off-by Jim Rees <[email protected]>--- fs/nfsd/nfs4state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/nfsd/nfs4state.c +1
-1@@ -3427,7 +3427,7 @@ grace_disallows_io(struct net *net, struct inode *inode)/* Returns true iff a is later than b: */ static bool stateid_generation_after (stateid_t *a,stateid_t *b) { -return (s32 )a->si_generation - (s32 )b->si_generation >0 ; +return (s32)(a->si_generation - b->si_generation) >0 ; }static __be32 check_stateid_generation (stateid_t *in,stateid_t *ref,bool has_session)Signature · 6 lines
1.8.2.3 -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Bruce Fields wrote:
Quoted 2 lines · Jim Rees
In C, signed integer overflow results in undefined behavior, but unsigned overflow wraps around. So do the subtraction first, then cast to signed.
Thanks! Applying for 3.11.
(I wonder if it actually bites anyone in practice? Are there common compilers or architectures where this makes a difference? Even if so I suppose that many generations is probably unlikely enough not to make this worth backporting to stable branches.)
I tested on the architectures I have at my fingertips and it made no difference. My guess is you might need a one's complement architecture. Of course we'll never see wraparound in our lifetimes, so no sense backporting this.
I actually used a one's complement machine many years ago, a CDC 6600. I don't think linux has been ported to it.
Quoted 14 lines · Jim Rees
In C, signed integer overflow results in undefined behavior, but unsigned overflow wraps around. So do the subtraction first, then cast to signed.
Suggested-by Joakim Tjernlund <[email protected]>Signed-off-by Jim Rees <[email protected]>--- fs/nfsd/nfs4state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/nfsd/nfs4state.c +0
-0
fs/nfsd/nfs4state.c grace_disallows_io(struct net *net, struct inode *inode) · line 3431
Quoted 6 lines
fs/nfsd/nfs4state.c +1
-1@@ -3427,7 +3427,7 @@ grace_disallows_io(struct net *net, struct inode *inode)/* Returns true iff a is later than b: */ static bool stateid_generation_after (stateid_t *a,stateid_t *b) { -return (s32 )a->si_generation - (s32 )b->si_generation >0 ; +return (s32)(a->si_generation - b->si_generation) >0 ; }
Hmm, what is actually wrong with plain-old
---- snip ----
static bool stateid_generation_after(stateid_t *a, stateid_t *b)
{
return a->si_generation > b->si_generation;
}---- snip ----
?
Kind regards,
Bernd
Signature · 2 lines
Bernd Petrovitsch Email : [email protected] LUGA : http://www.luga.at
Bernd Petrovitsch wrote:
Hmm, what is actually wrong with plain-old
---- snip ----
static bool stateid_generation_after(stateid_t *a, stateid_t *b)
{
return a->si_generation > b->si_generation;
}
---- snip ----
?It doesn't account for wraparound. Try this:
unsigned int a = 0xffff0000, b = 0x7fff0000;
printf("%d %d\n", a > b, (int)(a - b) > 0);I just realized that I said this needed a comment, then didn't provide one.
Quoted 11 lines · Jim Rees
Bernd Petrovitsch wrote:
Hmm, what is actually wrong with plain-old ---- snip ---- static bool stateid_generation_after(stateid_t *a, stateid_t *b) { return a->si_generation > b->si_generation; } ---- snip ---- ?
Quoted 6 lines
It doesn't account for wraparound. Try this:
unsigned int a = 0xffff0000, b = 0x7fff0000; printf("%d %d\n", a > b, (int)(a - b) > 0);I just realized that I said this needed a comment, then didn't provide one.
Ooops, yes, thx.
Sry for the noise;-)
Bernd
Signature · 2 lines
Bernd Petrovitsch Email : [email protected] LUGA : http://www.luga.at