]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
fix printf()/scanf() format signedness
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 20 Jun 2018 14:28:10 +0000 (16:28 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 20 Jun 2018 14:57:04 +0000 (16:57 +0200)
Fix mismatches between the format and sign of variables passed to
printf() or scanf(), which were found in a Frama-C analysis and gcc
using the -Wformat-signedness option.

client.c
keys.c
ntp_core.c
ntp_io.c
ntp_io_linux.c
refclock_sock.c
sources.c
test/unit/clientlog.c
test/unit/sources.c
util.c

index 1745db67172744e421eee867804a102ff8fa71ba..dab69ff8b78ae743f01f6e8ef77896b7f5eca6a9 100644 (file)
--- a/client.c
+++ b/client.c
@@ -1638,17 +1638,17 @@ print_seconds(unsigned long s)
   if (s == (uint32_t)-1) {
     printf("   -");
   } else if (s < 1200) {
-    printf("%4ld", s);
+    printf("%4lu", s);
   } else if (s < 36000) {
-    printf("%3ldm", s / 60);
+    printf("%3lum", s / 60);
   } else if (s < 345600) {
-    printf("%3ldh", s / 3600);
+    printf("%3luh", s / 3600);
   } else {
     d = s / 86400;
     if (d > 999) {
-      printf("%3ldy", d / 365);
+      printf("%3luy", d / 365);
     } else {
-      printf("%3ldd", d);
+      printf("%3lud", d);
     }
   }
 }
@@ -2890,7 +2890,7 @@ process_cmd_keygen(char *line)
   snprintf(hash_name, sizeof (hash_name), "MD5");
 #endif
 
-  if (sscanf(line, "%u %16s %d", &id, hash_name, &bits))
+  if (sscanf(line, "%u %16s %u", &id, hash_name, &bits))
     ;
 
   length = CLAMP(10, (bits + 7) / 8, sizeof (key));
diff --git a/keys.c b/keys.c
index 61ffb91c64d4c9c88b29017e98c483f885f8ce9d..7eb5cb37b8dc5da57f9444d57b04197db5673edb 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -214,7 +214,7 @@ KEY_Reload(void)
       continue;
 
     if (!CPS_ParseKey(line, &key_id, &hashname, &keyval)) {
-      LOG(LOGS_WARN, "Could not parse key at line %d in file %s", line_number, key_file);
+      LOG(LOGS_WARN, "Could not parse key at line %u in file %s", line_number, key_file);
       continue;
     }
 
index 991e3d9be80f714c653658d9450ac71aed995e8a..a4dd926b93f8a3d189106aa549f617a81dfd4996 100644 (file)
@@ -2043,7 +2043,7 @@ NCR_ProcessRxKnown(NCR_Instance inst, NTP_Local_Address *local_addr,
     /* It's not a reply to our request, don't return success */
     return 0;
   } else {
-    DEBUG_LOG("NTP packet discarded pkt_mode=%d our_mode=%d", pkt_mode, inst->mode);
+    DEBUG_LOG("NTP packet discarded pkt_mode=%d our_mode=%u", pkt_mode, inst->mode);
     return 0;
   }
 }
@@ -2102,7 +2102,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
       /* Fall through */
     default:
       /* Discard */
-      DEBUG_LOG("NTP packet discarded pkt_mode=%d", pkt_mode);
+      DEBUG_LOG("NTP packet discarded pkt_mode=%u", pkt_mode);
       return;
   }
 
@@ -2128,7 +2128,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
         break;
       default:
         /* Discard packets in other modes */
-        DEBUG_LOG("NTP packet discarded auth_mode=%d", auth_mode);
+        DEBUG_LOG("NTP packet discarded auth_mode=%u", auth_mode);
         return;
     }
   }
index 8df093ed458d2a3372ee2cfa70b13606dad35899..e92d27ebbfa81326e91696893aab1687141c188a 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -684,7 +684,7 @@ process_message(struct msghdr *hdr, int length, int sock_fd)
     return;
 #endif
 
-  DEBUG_LOG("Received %d bytes from %s:%d to %s fd=%d if=%d tss=%d delay=%.9f",
+  DEBUG_LOG("Received %d bytes from %s:%d to %s fd=%d if=%d tss=%u delay=%.9f",
             length, UTI_IPToString(&remote_addr.ip_addr), remote_addr.port,
             UTI_IPToString(&local_addr.ip_addr), local_addr.sock_fd, local_addr.if_index,
             local_ts.source, UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts));
index da39c950c0b2096dc05fa51d90323874d7adfd3e..a1f2fb7af6727a1f1ff4000de0f99d1681428307 100644 (file)
@@ -321,7 +321,7 @@ check_timestamping_option(int option)
     return 0;
 
   if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &option, sizeof (option)) < 0) {
-    DEBUG_LOG("Could not enable timestamping option %x", option);
+    DEBUG_LOG("Could not enable timestamping option %x", (unsigned int)option);
     close(sock_fd);
     return 0;
   }
@@ -790,7 +790,7 @@ NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *loc
   l2_length = length;
   length = extract_udp_data(hdr->msg_iov[0].iov_base, remote_addr, length);
 
-  DEBUG_LOG("Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%d",
+  DEBUG_LOG("Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%u",
             l2_length, length, UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
             local_addr->sock_fd, local_addr->if_index, local_ts->source);
 
index eb961474663da55ce6aac0cf8bc5b8411ad06aea..51c49976d0b61e7742e2720f730a6afe80e43490 100644 (file)
@@ -82,7 +82,7 @@ static void read_sample(int sockfd, int event, void *anything)
 
   if (sample.magic != SOCK_MAGIC) {
     LOG(LOGS_WARN, "Unexpected magic number in SOCK sample : %x != %x",
-        sample.magic, SOCK_MAGIC);
+        (unsigned int)sample.magic, (unsigned int)SOCK_MAGIC);
     return;
   }
 
index 8aaa4a6fefc00498d6a6dbe5219683d8b100684a..73b8ad01131d55b26daa45c6f6a0e24d9182bcc5 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -778,8 +778,8 @@ SRC_SelectSource(SRC_Instance updated_inst)
   }
 
   DEBUG_LOG("badstat=%d sel=%d badstat_reach=%x sel_reach=%x max_reach_ago=%f",
-            n_badstats_sources, n_sel_sources, max_badstat_reach,
-            max_sel_reach, max_reach_sample_ago);
+            n_badstats_sources, n_sel_sources, (unsigned int)max_badstat_reach,
+            (unsigned int)max_sel_reach, max_reach_sample_ago);
 
   /* Wait for the next call if we have no source selected and there is
      a source with bad stats (has less than 3 samples) with reachability
@@ -1015,7 +1015,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
       sources[i]->sel_score = 1.0 / distance;
     }
 
-    DEBUG_LOG("select score=%f refid=%"PRIx32" match_refid=%"PRIx32" status=%d dist=%f",
+    DEBUG_LOG("select score=%f refid=%"PRIx32" match_refid=%"PRIx32" status=%u dist=%f",
               sources[i]->sel_score, sources[i]->ref_id,
               updated_inst ? updated_inst->ref_id : 0,
               sources[i]->status, distance);
index 515ad1ad9f657d63b8d1252e14561ca3cc737442..a412b697e88a3e478757001062498a8556f1b1b4 100644 (file)
@@ -65,7 +65,7 @@ test_unit(void)
     }
   }
 
-  DEBUG_LOG("records %d", ARR_GetSize(records));
+  DEBUG_LOG("records %u", ARR_GetSize(records));
   TEST_CHECK(ARR_GetSize(records) == 64);
 
   for (i = j = 0; i < 10000; i++) {
@@ -76,7 +76,7 @@ test_unit(void)
       j++;
   }
 
-  DEBUG_LOG("requests %u responses %u", i, j);
+  DEBUG_LOG("requests %d responses %d", i, j);
   TEST_CHECK(j * 4 < i && j * 6 > i);
 
   CLG_Finalise();
index 341e22e1bcbf2fe58dd7ea2153cd2d1666c81790..735ed0d31f01334252b3fb010021687ba533df40 100644 (file)
@@ -82,7 +82,7 @@ test_unit(void)
         double passed_lo = DBL_MAX, passed_hi = DBL_MIN;
 
         SRC_SelectSource(srcs[k]);
-        DEBUG_LOG("source %d status %d", k, sources[k]->status);
+        DEBUG_LOG("source %d status %u", k, sources[k]->status);
 
         for (l = 0; l <= j; l++) {
           TEST_CHECK(sources[l]->status > SRC_OK && sources[l]->status <= SRC_SELECTED);
diff --git a/util.c b/util.c
index 76417d50444e1a02b571f9641e4082279ede03fa..72362026f4b86e43d9ed378537422cbf8a95e7e8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -298,16 +298,17 @@ UTI_IPToString(IPAddr *addr)
       b = (ip>>16) & 0xff;
       c = (ip>> 8) & 0xff;
       d = (ip>> 0) & 0xff;
-      snprintf(result, BUFFER_LENGTH, "%ld.%ld.%ld.%ld", a, b, c, d);
+      snprintf(result, BUFFER_LENGTH, "%lu.%lu.%lu.%lu", a, b, c, d);
       break;
     case IPADDR_INET6:
       ip6 = addr->addr.in6;
 #ifdef FEAT_IPV6
       inet_ntop(AF_INET6, ip6, result, BUFFER_LENGTH);
 #else
-      snprintf(result, BUFFER_LENGTH, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
-               ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7],
-               ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15]);
+      assert(BUFFER_LENGTH >= 40);
+      for (a = 0; a < 8; a++)
+        snprintf(result + a * 5, 40 - a * 5, "%04x:",
+                 (unsigned int)(ip6[2 * a] << 8 | ip6[2 * a + 1]));
 #endif
       break;
     default:
@@ -1160,12 +1161,12 @@ UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid)
   }
 
   if (buf.st_uid != uid) {
-    LOG(LOGS_ERR, "Wrong owner of %s (%s != %d)", path, "UID", uid);
+    LOG(LOGS_ERR, "Wrong owner of %s (%s != %u)", path, "UID", uid);
     return 0;
   }
 
   if (buf.st_gid != gid) {
-    LOG(LOGS_ERR, "Wrong owner of %s (%s != %d)", path, "GID", gid);
+    LOG(LOGS_ERR, "Wrong owner of %s (%s != %u)", path, "GID", gid);
     return 0;
   }
 
@@ -1183,13 +1184,13 @@ UTI_DropRoot(uid_t uid, gid_t gid)
 
   /* Set effective, saved and real group ID */
   if (setgid(gid))
-    LOG_FATAL("setgid(%d) failed : %s", gid, strerror(errno));
+    LOG_FATAL("setgid(%u) failed : %s", gid, strerror(errno));
 
   /* Set effective, saved and real user ID */
   if (setuid(uid))
-    LOG_FATAL("setuid(%d) failed : %s", uid, strerror(errno));
+    LOG_FATAL("setuid(%u) failed : %s", uid, strerror(errno));
 
-  DEBUG_LOG("Dropped root privileges: UID %d GID %d", uid, gid);
+  DEBUG_LOG("Dropped root privileges: UID %u GID %u", uid, gid);
 }
 
 /* ================================================== */