Browse code

Fix format spec errors in Windows builds

- "%ll" is not supported by Windows run time, so use PRIi64
and cast the variable to (int64_t) in output statements
(as in commit 9ba36639abcac4367c8227d2dd87b18fb56267c4)

- Fix an instance of wchar_t * printed using %s -- should be %ls.

- Cast variables to int or unsigned int to match the output
format spec when necessary.

- In route.c correct format of adapter_index (should be %lu) in a few
places and remove some unnecessary casts to (unsigned int). Not
all such instances are changed, only those related to adapter_index
(for consistency) or close-by contexts are edited.

Most of these errors are seen in current Windows cross-compile,
but a few are triggered only if some DEBUG options are enabled.
Some are not in Windows specific paths. But for consistency, all uses
of %llu/%lld are removed. As these only affect log output, there are
no potential side effects.

Replacing long long by int64_t also has the advantage of avoiding
size ambiguity as long long is not guaranteed to be 64 bytes.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
Acked-by: Steffan Karger <steffan.karger@fox-it.com>
Message-Id: <1519275295-29121-1-git-send-email-selva.nair@gmail.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16522.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Selva Nair authored on 2018/02/22 13:54:55
Showing 14 changed files
... ...
@@ -342,8 +342,8 @@ x_msg_va(const unsigned int flags, const char *format, va_list arglist)
342 342
                 struct timeval tv;
343 343
                 gettimeofday(&tv, NULL);
344 344
 
345
-                fprintf(fp, "%lld.%06ld %x %s%s%s%s",
346
-                        (long long)tv.tv_sec,
345
+                fprintf(fp, "%"PRIi64".%06ld %x %s%s%s%s",
346
+                        (int64_t)tv.tv_sec,
347 347
                         (long)tv.tv_usec,
348 348
                         flags,
349 349
                         prefix,
... ...
@@ -1041,9 +1041,9 @@ se_wait_fast(struct event_set *es, const struct timeval *tv, struct event_set_re
1041 1041
     struct timeval tv_tmp = *tv;
1042 1042
     int stat;
1043 1043
 
1044
-    dmsg(D_EVENT_WAIT, "SE_WAIT_FAST maxfd=%d tv=%lld/%ld",
1044
+    dmsg(D_EVENT_WAIT, "SE_WAIT_FAST maxfd=%d tv=%"PRIi64"/%ld",
1045 1045
          ses->maxfd,
1046
-         (long long)tv_tmp.tv_sec,
1046
+         (int64_t)tv_tmp.tv_sec,
1047 1047
          (long)tv_tmp.tv_usec);
1048 1048
 
1049 1049
     stat = select(ses->maxfd + 1, &ses->readfds, &ses->writefds, NULL, &tv_tmp);
... ...
@@ -1065,8 +1065,8 @@ se_wait_scalable(struct event_set *es, const struct timeval *tv, struct event_se
1065 1065
     fd_set write = ses->writefds;
1066 1066
     int stat;
1067 1067
 
1068
-    dmsg(D_EVENT_WAIT, "SE_WAIT_SCALEABLE maxfd=%d tv=%lld/%ld",
1069
-         ses->maxfd, (long long)tv_tmp.tv_sec, (long)tv_tmp.tv_usec);
1068
+    dmsg(D_EVENT_WAIT, "SE_WAIT_SCALEABLE maxfd=%d tv=%"PRIi64"/%ld",
1069
+         ses->maxfd, (int64_t)tv_tmp.tv_sec, (long)tv_tmp.tv_usec);
1070 1070
 
1071 1071
     stat = select(ses->maxfd + 1, &read, &write, NULL, &tv_tmp);
1072 1072
 
... ...
@@ -610,7 +610,7 @@ check_coarse_timers_dowork(struct context *c)
610 610
     process_coarse_timers(c);
611 611
     c->c2.coarse_timer_wakeup = now + c->c2.timeval.tv_sec;
612 612
 
613
-    dmsg(D_INTERVAL, "TIMER: coarse timer wakeup %lld seconds", (long long)c->c2.timeval.tv_sec);
613
+    dmsg(D_INTERVAL, "TIMER: coarse timer wakeup %"PRIi64" seconds", (int64_t)c->c2.timeval.tv_sec);
614 614
 
615 615
     /* Is the coarse timeout NOT the earliest one? */
616 616
     if (c->c2.timeval.tv_sec > save.tv_sec)
... ...
@@ -556,7 +556,7 @@ void
556 556
 setenv_long_long(struct env_set *es, const char *name, long long value)
557 557
 {
558 558
     char buf[64];
559
-    openvpn_snprintf(buf, sizeof(buf), "%lld", value);
559
+    openvpn_snprintf(buf, sizeof(buf), "%"PRIi64, (int64_t)value);
560 560
     setenv_str(es, name, buf);
561 561
 }
562 562
 
... ...
@@ -2383,13 +2383,13 @@ multi_process_post(struct multi_context *m, struct multi_instance *mi, const uns
2383 2383
         multi_set_pending(m, ANY_OUT(&mi->context) ? mi : NULL);
2384 2384
 
2385 2385
 #ifdef MULTI_DEBUG_EVENT_LOOP
2386
-        printf("POST %s[%d] to=%d lo=%d/%d w=%lld/%ld\n",
2386
+        printf("POST %s[%d] to=%d lo=%d/%d w=%"PRIi64"/%ld\n",
2387 2387
                id(mi),
2388 2388
                (int) (mi == m->pending),
2389 2389
                mi ? mi->context.c2.to_tun.len : -1,
2390 2390
                mi ? mi->context.c2.to_link.len : -1,
2391 2391
                (mi && mi->context.c2.fragment) ? mi->context.c2.fragment->outgoing.len : -1,
2392
-               (long long)mi->context.c2.timeval.tv_sec,
2392
+               (int64_t)mi->context.c2.timeval.tv_sec,
2393 2393
                (long)mi->context.c2.timeval.tv_usec);
2394 2394
 #endif
2395 2395
     }
... ...
@@ -88,8 +88,8 @@ const char *
88 88
 tv_string(const struct timeval *tv, struct gc_arena *gc)
89 89
 {
90 90
     struct buffer out = alloc_buf_gc(64, gc);
91
-    buf_printf(&out, "[%lld/%ld]",
92
-               (long long)tv->tv_sec,
91
+    buf_printf(&out, "[%"PRIi64"/%ld]",
92
+               (int64_t)tv->tv_sec,
93 93
                (long)tv->tv_usec);
94 94
     return BSTR(&out);
95 95
 }
... ...
@@ -198,9 +198,9 @@ time_test(void)
198 198
         t = time(NULL);
199 199
         gettimeofday(&tv, NULL);
200 200
 #if 1
201
-        msg(M_INFO, "t=%lld s=%lld us=%ld",
202
-            (long long)t,
203
-            (long long)tv.tv_sec,
201
+        msg(M_INFO, "t=%"PRIi64" s=%"PRIi64" us=%ld",
202
+            (int64_t)t,
203
+            (int64_t)tv.tv_sec,
204 204
             (long)tv.tv_usec);
205 205
 #endif
206 206
     }
... ...
@@ -606,14 +606,14 @@ packet_id_debug_print(int msglevel,
606 606
         }
607 607
         buf_printf(&out, "%c", c);
608 608
     }
609
-    buf_printf(&out, "] %lld:" packet_id_format, (long long)p->time, (packet_id_print_type)p->id);
609
+    buf_printf(&out, "] %"PRIi64":" packet_id_format, (int64_t)p->time, (packet_id_print_type)p->id);
610 610
     if (pin)
611 611
     {
612
-        buf_printf(&out, " %lld:" packet_id_format, (long long)pin->time, (packet_id_print_type)pin->id);
612
+        buf_printf(&out, " %"PRIi64":" packet_id_format, (int64_t)pin->time, (packet_id_print_type)pin->id);
613 613
     }
614 614
 
615
-    buf_printf(&out, " t=%lld[%d]",
616
-               (long long)prev_now,
615
+    buf_printf(&out, " t=%"PRIi64"[%d]",
616
+               (int64_t)prev_now,
617 617
                (int)(prev_now - tv.tv_sec));
618 618
 
619 619
     buf_printf(&out, " r=[%d,%d,%d,%d,%d]",
... ...
@@ -666,8 +666,8 @@ packet_id_interactive_test(void)
666 666
         {
667 667
             packet_id_reap_test(&pid.rec);
668 668
             test = packet_id_test(&pid.rec, &pin);
669
-            printf("packet_id_test (%lld, " packet_id_format ") returned %d\n",
670
-                   (long long)pin.time,
669
+            printf("packet_id_test (%"PRIi64", " packet_id_format ") returned %d\n",
670
+                   (int64_t)pin.time,
671 671
                    (packet_id_print_type)pin.id,
672 672
                    test);
673 673
             if (test)
... ...
@@ -679,8 +679,8 @@ packet_id_interactive_test(void)
679 679
         {
680 680
             long_form = (count < 20);
681 681
             packet_id_alloc_outgoing(&pid.send, &pin, long_form);
682
-            printf("(%lld(" packet_id_format "), %d)\n",
683
-                   (long long)pin.time,
682
+            printf("(%"PRIi64"(" packet_id_format "), %d)\n",
683
+                   (int64_t)pin.time,
684 684
                    (packet_id_print_type)pin.id,
685 685
                    long_form);
686 686
             if (pid.send.id == 10)
... ...
@@ -762,14 +762,14 @@ reliable_debug_print(const struct reliable *rel, char *desc)
762 762
     printf("********* struct reliable %s\n", desc);
763 763
     printf("  initial_timeout=%d\n", (int)rel->initial_timeout);
764 764
     printf("  packet_id=" packet_id_format "\n", rel->packet_id);
765
-    printf("  now=%lld\n", (long long)now);
765
+    printf("  now=%"PRIi64"\n", (int64_t)now);
766 766
     for (i = 0; i < rel->size; ++i)
767 767
     {
768 768
         const struct reliable_entry *e = &rel->array[i];
769 769
         if (e->active)
770 770
         {
771 771
             printf("  %d: packet_id=" packet_id_format " len=%d", i, e->packet_id, e->buf.len);
772
-            printf(" next_try=%lld", (long long)e->next_try);
772
+            printf(" next_try=%"PRIi64, (int64_t)e->next_try);
773 773
             printf("\n");
774 774
         }
775 775
     }
... ...
@@ -1322,7 +1322,7 @@ print_default_gateway(const int msglevel,
1322 1322
 #ifdef _WIN32
1323 1323
         if (rgi->flags & RGI_IFACE_DEFINED)
1324 1324
         {
1325
-            buf_printf(&out, " I=%u", (unsigned int)rgi->adapter_index);
1325
+            buf_printf(&out, " I=%lu", rgi->adapter_index);
1326 1326
         }
1327 1327
 #else
1328 1328
         if (rgi->flags & RGI_IFACE_DEFINED)
... ...
@@ -1353,7 +1353,7 @@ print_default_gateway(const int msglevel,
1353 1353
 #ifdef _WIN32
1354 1354
         if (rgi6->flags & RGI_IFACE_DEFINED)
1355 1355
         {
1356
-            buf_printf(&out, " I=%u", (unsigned int)rgi6->adapter_index);
1356
+            buf_printf(&out, " I=%lu", rgi6->adapter_index);
1357 1357
         }
1358 1358
 #else
1359 1359
         if (rgi6->flags & RGI_IFACE_DEFINED)
... ...
@@ -1627,7 +1627,7 @@ add_route(struct route_ipv4 *r,
1627 1627
         if (is_on_link(is_local_route, flags, rgi))
1628 1628
         {
1629 1629
             ai = rgi->adapter_index;
1630
-            argv_printf_cat(&argv, "IF %u", (unsigned int)ai);
1630
+            argv_printf_cat(&argv, "IF %lu", ai);
1631 1631
         }
1632 1632
 
1633 1633
         argv_msg(D_ROUTE, &argv);
... ...
@@ -1969,12 +1969,12 @@ add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flag
1969 1969
         struct buffer out = alloc_buf_gc(64, &gc);
1970 1970
         if (r6->adapter_index)          /* vpn server special route */
1971 1971
         {
1972
-            buf_printf(&out, "interface=%d", r6->adapter_index );
1972
+            buf_printf(&out, "interface=%lu", r6->adapter_index );
1973 1973
             gateway_needed = true;
1974 1974
         }
1975 1975
         else
1976 1976
         {
1977
-            buf_printf(&out, "interface=%d", tt->adapter_index );
1977
+            buf_printf(&out, "interface=%lu", tt->adapter_index );
1978 1978
         }
1979 1979
         device = buf_bptr(&out);
1980 1980
 
... ...
@@ -2416,12 +2416,12 @@ delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt, unsigned
2416 2416
         struct buffer out = alloc_buf_gc(64, &gc);
2417 2417
         if (r6->adapter_index)          /* vpn server special route */
2418 2418
         {
2419
-            buf_printf(&out, "interface=%d", r6->adapter_index );
2419
+            buf_printf(&out, "interface=%lu", r6->adapter_index );
2420 2420
             gateway_needed = true;
2421 2421
         }
2422 2422
         else
2423 2423
         {
2424
-            buf_printf(&out, "interface=%d", tt->adapter_index );
2424
+            buf_printf(&out, "interface=%lu", tt->adapter_index );
2425 2425
         }
2426 2426
         device = buf_bptr(&out);
2427 2427
 
... ...
@@ -2841,7 +2841,7 @@ get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6,
2841 2841
         goto done;
2842 2842
     }
2843 2843
 
2844
-    msg( D_ROUTE, "GDG6: II=%d DP=%s/%d NH=%s",
2844
+    msg( D_ROUTE, "GDG6: II=%lu DP=%s/%d NH=%s",
2845 2845
          BestRoute.InterfaceIndex,
2846 2846
          print_in6_addr( BestRoute.DestinationPrefix.Prefix.Ipv6.sin6_addr, 0, &gc),
2847 2847
          BestRoute.DestinationPrefix.PrefixLength,
... ...
@@ -3002,7 +3002,7 @@ do_route_service(const bool add, const route_message_t *rt, const size_t size, H
3002 3002
 
3003 3003
     if (ack.error_number != NO_ERROR)
3004 3004
     {
3005
-        msg(M_WARN, "ROUTE: route %s failed using service: %s [status=%u if_index=%lu]",
3005
+        msg(M_WARN, "ROUTE: route %s failed using service: %s [status=%u if_index=%d]",
3006 3006
             (add ? "addition" : "deletion"), strerror_win32(ack.error_number, &gc),
3007 3007
             ack.error_number, rt->iface.index);
3008 3008
         goto out;
... ...
@@ -76,8 +76,8 @@ shaper_soonest_event(struct timeval *tv, int delay)
76 76
         }
77 77
     }
78 78
 #ifdef SHAPER_DEBUG
79
-    dmsg(D_SHAPER_DEBUG, "SHAPER shaper_soonest_event sec=%lld usec=%ld ret=%d",
80
-         (long long)tv->tv_sec, (long)tv->tv_usec, (int)ret);
79
+    dmsg(D_SHAPER_DEBUG, "SHAPER shaper_soonest_event sec=%"PRIi64" usec=%ld ret=%d",
80
+         (int64_t)tv->tv_sec, (long)tv->tv_usec, (int)ret);
81 81
 #endif
82 82
     return ret;
83 83
 }
... ...
@@ -147,10 +147,10 @@ shaper_wrote_bytes(struct shaper *s, int nbytes)
147 147
         tv_add(&s->wakeup, &tv);
148 148
 
149 149
 #ifdef SHAPER_DEBUG
150
-        dmsg(D_SHAPER_DEBUG, "SHAPER shaper_wrote_bytes bytes=%d delay=%ld sec=%lld usec=%ld",
150
+        dmsg(D_SHAPER_DEBUG, "SHAPER shaper_wrote_bytes bytes=%d delay=%ld sec=%"PRIi64" usec=%ld",
151 151
              nbytes,
152 152
              (long)tv.tv_usec,
153
-             (long long)s->wakeup.tv_sec,
153
+             (int64_t)s->wakeup.tv_sec,
154 154
              (long)s->wakeup.tv_usec);
155 155
 #endif
156 156
     }
... ...
@@ -1122,7 +1122,7 @@ socket_do_accept(socket_descriptor_t sd,
1122 1122
 
1123 1123
     if (!socket_defined(new_sd))
1124 1124
     {
1125
-        msg(D_LINK_ERRORS | M_ERRNO, "TCP: accept(%d) failed", sd);
1125
+        msg(D_LINK_ERRORS | M_ERRNO, "TCP: accept(%d) failed", (int)sd);
1126 1126
     }
1127 1127
     /* only valid if we have remote_len_af!=0 */
1128 1128
     else if (remote_len_af && remote_len != remote_len_af)
... ...
@@ -1875,12 +1875,12 @@ phase2_inetd(struct link_socket *sock, const struct frame *frame,
1875 1875
                 sock->info.lsa->actual.dest.addr.sa.sa_family = local_addr.addr.sa.sa_family;
1876 1876
                 dmsg(D_SOCKET_DEBUG, "inetd(%s): using sa_family=%d from getsockname(%d)",
1877 1877
                      proto2ascii(sock->info.proto, sock->info.af, false),
1878
-                     local_addr.addr.sa.sa_family, sock->sd);
1878
+                     local_addr.addr.sa.sa_family, (int)sock->sd);
1879 1879
             }
1880 1880
             else
1881 1881
             {
1882 1882
                 msg(M_WARN, "inetd(%s): getsockname(%d) failed, using AF_INET",
1883
-                    proto2ascii(sock->info.proto, sock->info.af, false), sock->sd);
1883
+                    proto2ascii(sock->info.proto, sock->info.af, false), (int)sock->sd);
1884 1884
             }
1885 1885
         }
1886 1886
 #else  /* ifdef HAVE_GETSOCKNAME */
... ...
@@ -1580,8 +1580,8 @@ bio_debug_data(const char *mode, BIO *bio, const uint8_t *buf, int len, const ch
1580 1580
     if (len > 0)
1581 1581
     {
1582 1582
         open_biofp();
1583
-        fprintf(biofp, "BIO_%s %s time=%lld bio=" ptr_format " len=%d data=%s\n",
1584
-                mode, desc, (long long)time(NULL), (ptr_type)bio, len, format_hex(buf, len, 0, &gc));
1583
+        fprintf(biofp, "BIO_%s %s time=%"PRIi64" bio=" ptr_format " len=%d data=%s\n",
1584
+                mode, desc, (int64_t)time(NULL), (ptr_type)bio, len, format_hex(buf, len, 0, &gc));
1585 1585
         fflush(biofp);
1586 1586
     }
1587 1587
     gc_free(&gc);
... ...
@@ -1591,8 +1591,8 @@ static void
1591 1591
 bio_debug_oc(const char *mode, BIO *bio)
1592 1592
 {
1593 1593
     open_biofp();
1594
-    fprintf(biofp, "BIO %s time=%lld bio=" ptr_format "\n",
1595
-            mode, (long long)time(NULL), (ptr_type)bio);
1594
+    fprintf(biofp, "BIO %s time=%"PRIi64" bio=" ptr_format "\n",
1595
+            mode, (int64_t)time(NULL), (ptr_type)bio);
1596 1596
     fflush(biofp);
1597 1597
 }
1598 1598
 
... ...
@@ -125,7 +125,7 @@ do_address_service(const bool add, const short family, const struct tuntap *tt)
125 125
 
126 126
     if (ack.error_number != NO_ERROR)
127 127
     {
128
-        msg(M_WARN, "TUN: %s address failed using service: %s [status=%u if_index=%lu]",
128
+        msg(M_WARN, "TUN: %s address failed using service: %s [status=%u if_index=%d]",
129 129
             (add ? "adding" : "deleting"), strerror_win32(ack.error_number, &gc),
130 130
             ack.error_number, addr.iface.index);
131 131
         goto out;
... ...
@@ -3791,7 +3791,7 @@ get_panel_reg(struct gc_arena *gc)
3791 3791
 
3792 3792
             if (status != ERROR_SUCCESS || name_type != REG_SZ)
3793 3793
             {
3794
-                dmsg(D_REGISTRY, "Error opening registry key: %s\\%s\\%s",
3794
+                dmsg(D_REGISTRY, "Error opening registry key: %s\\%s\\%ls",
3795 3795
                      NETWORK_CONNECTIONS_KEY, connection_string, name_string);
3796 3796
             }
3797 3797
             else
... ...
@@ -5560,7 +5560,7 @@ fork_dhcp_action(struct tuntap *tt)
5560 5560
         {
5561 5561
             buf_printf(&cmd, " --dhcp-renew");
5562 5562
         }
5563
-        buf_printf(&cmd, " --dhcp-internal %u", (unsigned int)tt->adapter_index);
5563
+        buf_printf(&cmd, " --dhcp-internal %lu", tt->adapter_index);
5564 5564
 
5565 5565
         fork_to_self(BSTR(&cmd));
5566 5566
         gc_free(&gc);
... ...
@@ -6043,16 +6043,16 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
6043 6043
 
6044 6044
             if (status == NO_ERROR)
6045 6045
             {
6046
-                msg(M_INFO, "Successful ARP Flush on interface [%u] %s",
6047
-                    (unsigned int)index,
6046
+                msg(M_INFO, "Successful ARP Flush on interface [%lu] %s",
6047
+                    index,
6048 6048
                     device_guid);
6049 6049
             }
6050 6050
             else if (status != -1)
6051 6051
             {
6052
-                msg(D_TUNTAP_INFO, "NOTE: FlushIpNetTable failed on interface [%u] %s (status=%u) : %s",
6053
-                    (unsigned int)index,
6052
+                msg(D_TUNTAP_INFO, "NOTE: FlushIpNetTable failed on interface [%lu] %s (status=%lu) : %s",
6053
+                    index,
6054 6054
                     device_guid,
6055
-                    (unsigned int)status,
6055
+                    status,
6056 6056
                     strerror_win32(status, &gc));
6057 6057
             }
6058 6058
         }
... ...
@@ -6123,12 +6123,12 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
6123 6123
             }
6124 6124
             else
6125 6125
             {
6126
-                msg(M_FATAL, "ERROR: AddIPAddress %s/%s failed on interface %s, index=%d, status=%u (windows error: '%s') -- %s",
6126
+                msg(M_FATAL, "ERROR: AddIPAddress %s/%s failed on interface %s, index=%lu, status=%lu (windows error: '%s') -- %s",
6127 6127
                     print_in_addr_t(tt->local, 0, &gc),
6128 6128
                     print_in_addr_t(tt->adapter_netmask, 0, &gc),
6129 6129
                     device_guid,
6130
-                    (int)index,
6131
-                    (unsigned int)status,
6130
+                    index,
6131
+                    status,
6132 6132
                     strerror_win32(status, &gc),
6133 6133
                     error_suffix);
6134 6134
             }