Re: [chrony-dev] [PATCH] Fix precision loss warnings

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]



Curiosity:  recvfrom and sendto returns ssize_t (which is 64 bits on some
platforms), so this would invite for code that could make mistakes, by cutting the values.

Should the return value not rather be put in a ssize_t or some other variable that is large enough? And if really casted down, first be checked that they fit.

Cheers,
Håkan


On Sun, 9 Aug 2015, Bryan Christianson wrote:

Suppress precision loss warnings that result from long being typecast to int. In all cases the typecast is OK so this patch is designed to suppress the compiler warnings, assuming int is 32 bit on all platforms and long may be either 64 or 32 bits depending on platform and compiler used.
---
client.c        | 20 +++++++++++++++-----
cmdmon.c        | 12 ++++++------
conf.c          |  5 +++--
keys.c          |  4 ++--
local.c         |  2 +-
logging.c       |  2 +-
main.c          |  2 +-
ntp_core.c      |  4 +++-
ntp_io.c        |  4 ++--
pktlength.c     |  4 ++--
refclock.c      |  4 ++--
refclock_sock.c |  6 +++---
sched.c         |  5 +++--
sources.c       |  4 ++--
sourcestats.c   |  2 +-
util.c          |  6 +++---
16 files changed, 50 insertions(+), 36 deletions(-)

diff --git a/client.c b/client.c
index afa9abb..16c5d34 100644
--- a/client.c
+++ b/client.c
@@ -69,7 +69,9 @@ static int on_terminal = 0;

static int no_dns = 0;

+#ifdef IP_RECVERR
static int recv_errqueue = 0;
+#endif

/* ================================================== */
/* Log a message. This is a minimalistic replacement of the logging.c
@@ -138,7 +140,9 @@ static void
open_io(const char *hostname, int port)
{
  IPAddr ip;
+#ifdef IP_RECVERR
  int on_off = 1;
+#endif

  /* Note, this call could block for a while */
  if (DNS_Name2IPAddress(hostname, &ip, 1) != DNS_Success) {
@@ -666,7 +670,9 @@ parse_allow_deny(CMD_Request *msg, char *line)
  int n, specified_subnet_bits;
  IPAddr ip;
  char *p;
-
+
+  a = b = c = d = 0;
+
  p = line;
  if (!*p) {
    /* blank line - applies to all addresses */
@@ -838,14 +844,14 @@ process_cmd_cmddenyall(CMD_Request *msg, char *line)
static int
accheck_getaddr(char *line, IPAddr *addr)
{
-  unsigned long a, b, c, d;
+  unsigned int a, b, c, d;
  IPAddr ip;
  char *p;
  p = line;
  if (!*p) {
    return 0;
  } else {
-    if (sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d) == 4) {
+    if (sscanf(p, "%u.%u.%u.%u", &a, &b, &c, &d) == 4) {
      addr->family = IPADDR_INET4;
      addr->addr.in4 = (a<<24) | (b<<16) | (c<<8) | d;
      return 1;
@@ -1105,7 +1111,7 @@ process_cmd_password(CMD_Request *msg, char *line)
  if (!*p)
    return 0;

-  len = strlen(p);
+  len = (int)strlen(p);
  password_length = UTI_DecodePasswordFromText(p);

  if (password_length > 0) {
@@ -1350,7 +1356,7 @@ submit_request(CMD_Request *request, CMD_Reply *reply, int *reply_auth_ok)
    } else {

      where_from_len = sizeof(where_from);
-      recvfrom_status = recvfrom(sock_fd, (void *) reply, sizeof(CMD_Reply), 0,
+      recvfrom_status = (int)recvfrom(sock_fd, (void *) reply, sizeof(CMD_Reply), 0,
                                 &where_from.u, &where_from_len);


@@ -2668,6 +2674,9 @@ authenticate_from_config(const char *filename)
  int key_id_valid = 1, ret;
  FILE *in;

+  password = NULL;
+  hashname = NULL;
+
  in = fopen(filename, "r");
  if (!in) {
    fprintf(stderr, "Could not open file %s : %s\n", filename, strerror(errno));
@@ -2734,6 +2743,7 @@ process_args(int argc, char **argv, int multi)
  int total_length, i, ret;
  char *line;

+  ret = 0;
  total_length = 0;
  for(i=0; i<argc; i++) {
    total_length += strlen(argv[i]) + 1;
diff --git a/cmdmon.c b/cmdmon.c
index 3af3d9c..1b199e8 100644
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -734,7 +734,7 @@ transmit_reply(CMD_Reply *msg, union sockaddr_all *where_to, int auth_len)
  }

  tx_message_length = PKL_ReplyLength(msg) + auth_len;
-  status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
+  status = (int)sendto(sock_fd, (void *) msg, tx_message_length, 0,
                  &where_to->sa, addrlen);

  if (status < 0) {
@@ -1402,7 +1402,7 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message)
  tx_message->reply = htons(RPY_CLIENT_ACCESSES_BY_INDEX);

  for (i = 0, j = 0; i < n_indices; i++) {
-    result = CLG_GetClientAccessReportByIndex(first_index + i, &report,
+    result = CLG_GetClientAccessReportByIndex((int)first_index + i, &report,
                                              now.tv_sec, &n_indices_in_table);
    tx_message->data.client_accesses_by_index.n_indices = htonl(n_indices_in_table);

@@ -1519,7 +1519,7 @@ read_from_cmd_socket(void *anything)
  int status;
  int read_length; /* Length of packet read */
  int expected_length; /* Expected length of packet without auth data */
-  unsigned long flags;
+  int flags;
  CMD_Request rx_message;
  CMD_Reply tx_message, *prev_tx_message;
  int rx_message_length, tx_message_length;
@@ -1548,8 +1548,8 @@ read_from_cmd_socket(void *anything)
  rx_message_length = sizeof(rx_message);
  from_length = sizeof(where_from);

-  sock_fd = (long)anything;
-  status = recvfrom(sock_fd, (char *)&rx_message, rx_message_length, flags,
+  sock_fd = (int)anything;
+  status = (int)recvfrom(sock_fd, (char *)&rx_message, rx_message_length, flags,
                    &where_from.sa, &from_length);

  if (status < 0) {
@@ -1748,7 +1748,7 @@ read_from_cmd_socket(void *anything)
    if (prev_tx_message) {
      /* Just send this message again */
      tx_message_length = PKL_ReplyLength(prev_tx_message);
-      status = sendto(sock_fd, (void *) prev_tx_message, tx_message_length, 0,
+      status = (int)sendto(sock_fd, (void *) prev_tx_message, tx_message_length, 0,
                      &where_from.sa, from_length);
      if (status < 0) {
        DEBUG_LOG(LOGF_CmdMon, "Could not send response to %s:%hu", UTI_IPToString(&remote_ip), remote_port);
diff --git a/conf.c b/conf.c
index 6d4ee67..0017f02 100644
--- a/conf.c
+++ b/conf.c
@@ -944,11 +944,12 @@ static void
parse_allow_deny(char *line, ARR_Instance restrictions, int allow)
{
  char *p;
-  unsigned long a, b, c, d, n;
+  unsigned int a, b, c, d, n;
  int all = 0;
  AllowDeny *new_node = NULL;
  IPAddr ip_addr;

+  a = b = c = d = 0;
  p = line;

  if (!strncmp(p, "all", 3)) {
@@ -971,7 +972,7 @@ parse_allow_deny(char *line, ARR_Instance restrictions, int allow)
    check_number_of_args(p, 1);
    n = 0;
    if (UTI_StringToIP(p, &ip_addr) ||
-        (n = sscanf(p, "%lu.%lu.%lu.%lu", &a, &b, &c, &d)) >= 1) {
+        (n = sscanf(p, "%u.%u.%u.%u", &a, &b, &c, &d)) >= 1) {
      new_node = (AllowDeny *)ARR_GetNewElement(restrictions);
      new_node->allow = allow;
      new_node->all = all;
diff --git a/keys.c b/keys.c
index fcd1974..fa40cb1 100644
--- a/keys.c
+++ b/keys.c
@@ -190,7 +190,7 @@ determine_hash_delay(uint32_t key_id)

  DEBUG_LOG(LOGF_Keys, "authentication delay for key %"PRIu32": %ld useconds", key_id, min_usecs);

-  return min_usecs;
+  return (int)min_usecs;
}

/* ================================================== */
@@ -304,7 +304,7 @@ lookup_key(uint32_t id)
  if (!where) {
    return -1;
  } else {
-    pos = where - keys_ptr;
+    pos = (int)(where - keys_ptr);
    return pos;
  }
}
diff --git a/local.c b/local.c
index 848cc3e..1076a74 100644
--- a/local.c
+++ b/local.c
@@ -111,7 +111,7 @@ static void
calculate_sys_precision(void)
{
  struct timeval tv, old_tv;
-  int dusec, best_dusec;
+  long dusec, best_dusec;
  int iters;

  gettimeofday(&old_tv, NULL);
diff --git a/logging.c b/logging.c
index 37fb572..238ea5c 100644
--- a/logging.c
+++ b/logging.c
@@ -278,7 +278,7 @@ LOG_FileWrite(LOG_FileID id, const char *format, ...)
    char bannerline[256];
    int i, bannerlen;

-    bannerlen = strlen(logfiles[id].banner);
+    bannerlen = (int)strlen(logfiles[id].banner);

    for (i = 0; i < bannerlen; i++)
      bannerline[i] = '=';
diff --git a/main.c b/main.c
index c3c5574..d02f807 100644
--- a/main.c
+++ b/main.c
@@ -299,7 +299,7 @@ go_daemon(void)
    int r;

    close(pipefd[1]);
-    r = read(pipefd[0], message, sizeof (message));
+    r = (int)read(pipefd[0], message, sizeof (message));
    if (r) {
      if (r > 0) {
        /* Print the error message from the child */
diff --git a/ntp_core.c b/ntp_core.c
index b477666..478aab8 100644
--- a/ntp_core.c
+++ b/ntp_core.c
@@ -1158,6 +1158,8 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins

  /* ==================== */

+  local_interval = 0.0;
+
  pkt_leap = NTP_LVM_TO_LEAP(message->lvm);
  pkt_refid = ntohl(message->reference_id);
  pkt_root_delay = UTI_Int32ToDouble(message->root_delay);
@@ -1980,7 +1982,7 @@ broadcast_timeout(void *arg)
  NTP_int64 orig_ts;
  struct timeval recv_ts;

-  destination = ARR_GetElement(broadcasts, (long)arg);
+  destination = ARR_GetElement(broadcasts, (unsigned int)arg);

  orig_ts.hi = 0;
  orig_ts.lo = 0;
diff --git a/ntp_io.c b/ntp_io.c
index 68d677d..35f068b 100644
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -511,8 +511,8 @@ read_from_socket(void *anything)
  msg.msg_controllen = sizeof(cmsgbuf);
  msg.msg_flags = 0;

-  sock_fd = (long)anything;
-  status = recvmsg(sock_fd, &msg, flags);
+  sock_fd = (int)anything;
+  status = (int)recvmsg(sock_fd, &msg, flags);

  /* Don't bother checking if read failed or why if it did.  More
     likely than not, it will be connection refused, resulting from a
diff --git a/pktlength.c b/pktlength.c
index be773c2..2c00558 100644
--- a/pktlength.c
+++ b/pktlength.c
@@ -348,7 +348,7 @@ PKL_ReplyLength(CMD_Reply *r)
            if (nc > MAX_CLIENT_ACCESSES)
              return 0;
            return (offsetof(CMD_Reply, data.client_accesses_by_index.clients) +
-                    nc * sizeof(RPY_ClientAccesses_Client));
+                    (int)(nc * sizeof(RPY_ClientAccesses_Client)));
          } else {
            return offsetof(CMD_Reply, data);
          }
@@ -359,7 +359,7 @@ PKL_ReplyLength(CMD_Reply *r)
          if (ns > MAX_MANUAL_LIST_SAMPLES)
            return 0;
          if (r->status == htons(STT_SUCCESS)) {
-            return (offsetof(CMD_Reply, data.manual_list.samples) +
+            return (int)(offsetof(CMD_Reply, data.manual_list.samples) +
                    ns * sizeof(RPY_ManualListSample));
          } else {
            return offsetof(CMD_Reply, data);
diff --git a/refclock.c b/refclock.c
index 26193f6..8a4788f 100644
--- a/refclock.c
+++ b/refclock.c
@@ -209,7 +209,7 @@ RCL_AddRefclock(RefclockParameters *params)
  if (inst->driver_parameter) {
    int i;

-    inst->driver_parameter_length = strlen(inst->driver_parameter);
+    inst->driver_parameter_length = (int)strlen(inst->driver_parameter);
    for (i = 0; i < inst->driver_parameter_length; i++)
      if (inst->driver_parameter[i] == ':')
        inst->driver_parameter[i] = '\0';
@@ -340,7 +340,7 @@ RCL_GetDriverOption(RCL_Instance instance, char *name)

  s = instance->driver_parameter;
  e = s + instance->driver_parameter_length;
-  n = strlen(name);
+  n = (int)strlen(name);

  while (1) {
    s += strlen(s) + 1;
diff --git a/refclock_sock.c b/refclock_sock.c
index 97f740b..e2bcdc3 100644
--- a/refclock_sock.c
+++ b/refclock_sock.c
@@ -52,9 +52,9 @@ static void read_sample(void *anything)
  int sockfd, s;

  instance = (RCL_Instance)anything;
-  sockfd = (long)RCL_GetDriverData(instance);
+  sockfd = (int)RCL_GetDriverData(instance);

-  s = recv(sockfd, &sample, sizeof (sample), 0);
+  s = (int)recv(sockfd, &sample, sizeof (sample), 0);

  if (s < 0) {
    LOG(LOGS_ERR, LOGF_Refclock, "Could not read SOCK sample : %s",
@@ -118,7 +118,7 @@ static void sock_finalise(RCL_Instance instance)
{
  int sockfd;

-  sockfd = (long)RCL_GetDriverData(instance);
+  sockfd = (int)RCL_GetDriverData(instance);
  SCH_RemoveInputFileHandler(sockfd);
  close(sockfd);
}
diff --git a/sched.c b/sched.c
index 1946745..5119d5d 100644
--- a/sched.c
+++ b/sched.c
@@ -150,7 +150,7 @@ SCH_Initialise(void)
  LCL_ReadRawTime(&last_select_ts_raw);
  last_select_ts = last_select_ts_raw;

-  srandom(last_select_ts.tv_sec << 16 ^ last_select_ts.tv_usec);
+  srandom((int)(last_select_ts.tv_sec << 16) ^ last_select_ts.tv_usec);

  initialised = 1;
}
@@ -452,7 +452,8 @@ dispatch_timeouts(struct timeval *now) {
  TimerQueueEntry *ptr;
  SCH_TimeoutHandler handler;
  SCH_ArbitraryArgument arg;
-  int n_done = 0, n_entries_on_start = n_timer_queue_entries;
+  int n_done = 0;
+  unsigned long n_entries_on_start = n_timer_queue_entries;

  while (1) {
    LCL_ReadRawTime(now);
diff --git a/sources.c b/sources.c
index 8ae1e6c..47dd9bc 100644
--- a/sources.c
+++ b/sources.c
@@ -1089,7 +1089,7 @@ SRC_DumpSources(void)
  char *direc;

  direc = CNF_GetDumpDir();
-  direc_len = strlen(direc);
+  direc_len = (int)strlen(direc);
  file_len = direc_len + 24;
  filename = MallocArray(char, file_len); /* a bit of slack */
  if (UTI_CreateDirAndParents(direc)) {
@@ -1133,7 +1133,7 @@ SRC_ReloadSources(void)
    d = ((sources[i]->ref_id)) & 0xff;

    dumpdir = CNF_GetDumpDir();
-    dumpdirlen = strlen(dumpdir);
+    dumpdirlen = (int)strlen(dumpdir);
    filelen = dumpdirlen + 24;
    filename = MallocArray(char, filelen);
    snprintf(filename, filelen-1, "%s/%d.%d.%d.%d.dat", dumpdir, a, b, c, d);
diff --git a/sourcestats.c b/sourcestats.c
index 52e5034..92d210e 100644
--- a/sourcestats.c
+++ b/sourcestats.c
@@ -806,7 +806,7 @@ SST_LoadFromFile(SST_Stats inst, FILE *in)

        /* This is the branch taken if the read is SUCCESSFUL */
        inst->sample_times[i].tv_sec = sec;
-        inst->sample_times[i].tv_usec = usec;
+        inst->sample_times[i].tv_usec = (int)usec;

        line_number++;
      }
diff --git a/util.c b/util.c
index 89379d9..a94b568 100644
--- a/util.c
+++ b/util.c
@@ -53,7 +53,7 @@ UTI_DoubleToTimeval(double a, struct timeval *b)
  frac_part = 1.0e6 * (a - (double)(int_part));
  frac_part = frac_part > 0 ? frac_part + 0.5 : frac_part - 0.5;
  b->tv_sec = int_part;
-  b->tv_usec = (long)frac_part;
+  b->tv_usec = (int)frac_part;
  UTI_NormaliseTimeval(b);
}

@@ -142,7 +142,7 @@ UTI_AddDoubleToTimeval(struct timeval *start,
  frac_part = (long) (increment > 0.0 ? increment + 0.5 : increment - 0.5);

  end->tv_sec  = int_part  + start->tv_sec;
-  end->tv_usec = frac_part + start->tv_usec;
+  end->tv_usec = (int)frac_part + start->tv_usec;

  UTI_NormaliseTimeval(end);
}
@@ -837,7 +837,7 @@ UTI_CheckNTPAuth(int hash_id, const unsigned char *key, int key_len,
int
UTI_DecodePasswordFromText(char *key)
{
-  int i, j, len = strlen(key);
+  int i, j, len = (int)strlen(key);
  char buf[3], *p;

  if (!strncmp(key, "ASCII:", 6)) {
--
2.3.2 (Apple Git-55)


--
To unsubscribe email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "unsubscribe" in the subject.
For help email chrony-dev-request@xxxxxxxxxxxxxxxxxxxx with "help" in the subject.
Trouble?  Email listmaster@xxxxxxxxxxxxxxxxxxxx.



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/