Skip to content

Commit c509312

Browse files
paulmckpopcornmix
authored andcommitted
jiffies: Avoid undefined behavior from signed overflow
commit 5a581b3 upstream. According to the C standard 3.4.3p3, overflow of a signed integer results in undefined behavior. This commit therefore changes the definitions of time_after(), time_after_eq(), time_after64(), and time_after_eq64() to avoid this undefined behavior. The trick is that the subtraction is done using unsigned arithmetic, which according to 6.2.5p9 cannot overflow because it is defined as modulo arithmetic. This has the added (though admittedly quite small) benefit of shortening four lines of code by four characters each. Note that the C standard considers the cast from unsigned to signed to be implementation-defined, see 6.3.1.3p3. However, on a two's-complement system, an implementation that defines anything other than a reinterpretation of the bits is free to come to me, and I will be happy to act as a witness for its being committed to an insane asylum. (Although I have nothing against saturating arithmetic or signals in some cases, these things really should not be the default when compiling an operating-system kernel.) Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: John Stultz <john.stultz@linaro.org> Cc: "David S. Miller" <davem@davemloft.net> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Kevin Easton <kevin@guarana.org> [ paulmck: Included time_after64() and time_after_eq64(), as suggested by Eric Dumazet, also fixed commit message.] Reviewed-by: Josh Triplett <josh@joshtriplett.org> Ruchi Kandoi <kandoiruchi@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a9467db commit c509312

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎include/linux/jiffies.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ static inline u64 get_jiffies_64(void)
101101
#define time_after(a,b) \
102102
(typecheck(unsigned long, a) && \
103103
typecheck(unsigned long, b) && \
104-
((long)(b) - (long)(a) < 0))
104+
((long)((b) - (a)) < 0))
105105
#define time_before(a,b) time_after(b,a)
106106

107107
#define time_after_eq(a,b) \
108108
(typecheck(unsigned long, a) && \
109109
typecheck(unsigned long, b) && \
110-
((long)(a) - (long)(b) >= 0))
110+
((long)((a) - (b)) >= 0))
111111
#define time_before_eq(a,b) time_after_eq(b,a)
112112

113113
/*
@@ -130,13 +130,13 @@ static inline u64 get_jiffies_64(void)
130130
#define time_after64(a,b) \
131131
(typecheck(__u64, a) && \
132132
typecheck(__u64, b) && \
133-
((__s64)(b) - (__s64)(a) < 0))
133+
((__s64)((b) - (a)) < 0))
134134
#define time_before64(a,b) time_after64(b,a)
135135

136136
#define time_after_eq64(a,b) \
137137
(typecheck(__u64, a) && \
138138
typecheck(__u64, b) && \
139-
((__s64)(a) - (__s64)(b) >= 0))
139+
((__s64)((a) - (b)) >= 0))
140140
#define time_before_eq64(a,b) time_after_eq64(b,a)
141141

142142
/*

0 commit comments

Comments
 (0)