Skip to content

Commit 1ba3aab

Browse files
netoptimizerdavem330
authored andcommitted
net: codel: Avoid undefined behavior from signed overflow
As described in commit 5a581b3 (jiffies: Avoid undefined behavior from signed overflow), according to the C standard 3.4.3p3, overflow of a signed integer results in undefined behavior. To fix this, do as the above commit, and do an unsigned subtraction, and interpreting the result as a signed two's-complement number. This is based on the theory from RFC 1982 and is nicely described in wikipedia here: https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution A side-note, I have seen practical issues with the previous logic when dealing with 16-bit, on a 64-bit machine (gcc version 4.4.5). This were 32-bit, which I have not observed issues with. Cc: Paul E. McKenney <[email protected]> Signed-off-by: Jesper Dangaard Brouer <[email protected]> Acked-by: Paul E. McKenney <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 13521a5 commit 1ba3aab

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

include/net/codel.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,21 @@ static inline codel_time_t codel_get_time(void)
7272
return ns >> CODEL_SHIFT;
7373
}
7474

75-
#define codel_time_after(a, b) ((s32)(a) - (s32)(b) > 0)
76-
#define codel_time_after_eq(a, b) ((s32)(a) - (s32)(b) >= 0)
77-
#define codel_time_before(a, b) ((s32)(a) - (s32)(b) < 0)
78-
#define codel_time_before_eq(a, b) ((s32)(a) - (s32)(b) <= 0)
75+
/* Dealing with timer wrapping, according to RFC 1982, as desc in wikipedia:
76+
* https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
77+
* codel_time_after(a,b) returns true if the time a is after time b.
78+
*/
79+
#define codel_time_after(a, b) \
80+
(typecheck(codel_time_t, a) && \
81+
typecheck(codel_time_t, b) && \
82+
((s32)((a) - (b)) > 0))
83+
#define codel_time_before(a, b) codel_time_after(b, a)
84+
85+
#define codel_time_after_eq(a, b) \
86+
(typecheck(codel_time_t, a) && \
87+
typecheck(codel_time_t, b) && \
88+
((s32)((a) - (b)) >= 0))
89+
#define codel_time_before_eq(a, b) codel_time_after_eq(b, a)
7990

8091
/* Qdiscs using codel plugin must use codel_skb_cb in their own cb[] */
8192
struct codel_skb_cb {

0 commit comments

Comments
 (0)