]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
fix undefined shift operations on signed integers
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 2 Dec 2015 11:06:01 +0000 (12:06 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 2 Dec 2015 11:06:01 +0000 (12:06 +0100)
sources.c
util.c

index 017f3bc9b1af8ad66a4477a77a79908927f1aa58..b0742c8e5112088d4a923d5a81fdec6ac894f1ee 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -414,7 +414,7 @@ SRC_UpdateReachability(SRC_Instance inst, int reachable)
 {
   inst->reachability <<= 1;
   inst->reachability |= !!reachable;
-  inst->reachability &= ~(-1 << SOURCE_REACH_BITS);
+  inst->reachability %= 1U << SOURCE_REACH_BITS;
 
   if (inst->reachability_size < SOURCE_REACH_BITS)
       inst->reachability_size++;
diff --git a/util.c b/util.c
index da70f0bdf3154c34792012084959fab508dd2967..b01b93625e6369c25f77008e405555e4b06c17b5 100644 (file)
--- a/util.c
+++ b/util.c
@@ -352,7 +352,7 @@ UTI_IPToRefid(IPAddr *ip)
         assert(0);
         return 0;
       };
-      return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
+      return (uint32_t)buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
   }
   return 0;
 }
@@ -796,11 +796,19 @@ UTI_TimevalHostToNetwork(struct timeval *src, Timeval *dest)
 double
 UTI_FloatNetworkToHost(Float f)
 {
-  int32_t exp, coef, x;
+  int32_t exp, coef;
+  uint32_t x;
 
   x = ntohl(f.f);
+
   exp = (x >> FLOAT_COEF_BITS) - FLOAT_COEF_BITS;
-  coef = x << FLOAT_EXP_BITS >> FLOAT_EXP_BITS;
+  if (exp >= 1 << (FLOAT_EXP_BITS - 1))
+      exp -= 1 << FLOAT_EXP_BITS;
+
+  coef = x % (1U << FLOAT_COEF_BITS);
+  if (coef >= 1 << (FLOAT_COEF_BITS - 1))
+      coef -= 1 << FLOAT_COEF_BITS;
+
   return coef * pow(2.0, exp);
 }
 
@@ -857,7 +865,7 @@ UTI_FloatHostToNetwork(double x)
   if (neg)
     coef = (uint32_t)-coef << FLOAT_EXP_BITS >> FLOAT_EXP_BITS;
 
-  f.f = htonl(exp << FLOAT_COEF_BITS | coef);
+  f.f = htonl((uint32_t)exp << FLOAT_COEF_BITS | coef);
   return f;
 }