Fix wrap-aware isAfter function in Congestion.cxx
Result of overflow on signed integer arithmetic is undefined in C/C++ standard.
So in previous version clang was compiling the statement as (int)a > (int)b (i.e. assuming no overflow), which leads to incorrect result.
Correct deterministic behavior means doing overflow arithmetic as unsigned, i.e.
a != b && a - b <= UINT_MAX / 2
diff --git a/common/rfb/Congestion.cxx b/common/rfb/Congestion.cxx
index 4a78452..f3f9cee 100644
--- a/common/rfb/Congestion.cxx
+++ b/common/rfb/Congestion.cxx
@@ -70,7 +70,7 @@
// Compare position even when wrapped around
static inline bool isAfter(unsigned a, unsigned b) {
- return (int)a - (int)b > 0;
+ return a != b && a - b <= UINT_MAX / 2;
}
static LogWriter vlog("Congestion");