[nanomsg] Warning fixes

  • From: Boszormenyi Zoltan <zboszor@xxxxx>
  • To: nanomsg@xxxxxxxxxxxxx, Martin Sustrik <sustrik@xxxxxxxxxx>
  • Date: Tue, 17 Dec 2013 12:47:00 +0100

Hi,

I get 3 warnings when compiling nanomsg (current GIT) with
GCC 4.8.2 for 64-bit under Fedora 20:

src/core/global.c: In function 'nn_global_submit_counter':
src/core/global.c:904:13: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 5 has type 'uint64_t' [-Wformat=]
             s->socket_name, name, value);
             ^
src/core/global.c:919:17: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 8 has type 'uint64_t' [-Wformat=]
                 timebuf, value);
                 ^
src/core/global.c:923:17: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 8 has type 'uint64_t' [-Wformat=]
                 timebuf, value);
                 ^

The first attached patch fixes it in a "naive" way.
The %llu format needs an (unsigned long long)value argument,
since uint64_t is only (unsigned long) under 64-bit Linux.
It should be portable but actually isn't.

The second, more portable patch uses macros (PRIu64)
from <inttypes.h> so the compiler doesn't make the value
wider than necessary.

This header doesn't exist under Visual Studio but you can
install it from http://code.google.com/p/msinttypes/

I had the same problem in another project regarding portability,
and I used MinGW32 at the time. As it turned out, "I64d", "I64u"
and others are the proper 64-bit format specifiers for Windows.
It seems to depend on msvcrt.dll, not the compiler version.

The patches touch exactly the places that commit 624c483 modified.

I am not familiar with CMakeLists.txt but added the check for the header 
according to
http://www.cmake.org/Wiki/CMake:How_To_Write_Platform_Checks
to CMakeLists.txt and also to configure.ac and src/core/global.c.
It compiled without warnings for me under GCC.

Best regards,
Zoltán Böszörményi

diff --git a/src/core/global.c b/src/core/global.c
index 6ac705b..a31f780 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -901,7 +901,7 @@ static void nn_global_submit_counter (int i, struct nn_sock 
*s,
 
     if(self.print_statistics) {
         fprintf(stderr, "nanomsg: socket.%s: %s: %llu\n",
-            s->socket_name, name, value);
+            s->socket_name, name, (unsigned long long)value);
     }
 
     if (self.statistics_socket >= 0) {
@@ -916,11 +916,11 @@ static void nn_global_submit_counter (int i, struct 
nn_sock *s,
         if(*s->socket_name) {
             len = sprintf (buf, "ESTP:%s:%s:socket.%s:%s: %sZ 10 %llu:c",
                 self.hostname, self.appname, s->socket_name, name,
-                timebuf, value);
+                timebuf, (unsigned long long)value);
         } else {
             len = sprintf (buf, "ESTP:%s:%s:socket.%d:%s: %sZ 10 %llu:c",
                 self.hostname, self.appname, i, name,
-                timebuf, value);
+                timebuf, (unsigned long long)value);
         }
         nn_assert (len < (int)sizeof(buf));
         (void) nn_send (self.statistics_socket, buf, len, NN_DONTWAIT);
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 13fabba..d9b073b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,6 +37,8 @@ else ()
     message (FATAL_ERROR "ERROR: CMake build system is intended only to 
generate MSVC solution files.\nUse autotools build system for any other 
purpose." )
 endif ()
 
+CHECK_INCLUDE_FILES (inttypes.h HAVE_INTTYPES_H)
+
 #  Build the library itself.
 
 add_subdirectory (src)
diff --git a/configure.ac b/configure.ac
index 94e5eb1..5e457cc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -240,6 +240,7 @@ AC_CHECK_HEADERS([arpa/inet.h])
 AC_CHECK_HEADERS([unistd.h])
 AC_CHECK_HEADERS([sys/socket.h])
 AC_CHECK_HEADERS([sys/ioctl.h])
+AC_CHECK_HEADERS([inttypes.h])
 
 AC_CHECK_FUNCS([eventfd], [AC_DEFINE([NN_HAVE_EVENTFD])])
 AC_CHECK_FUNCS([pipe], [AC_DEFINE([NN_HAVE_PIPE])])
diff --git a/src/core/global.c b/src/core/global.c
index 6ac705b..15194c7 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -71,6 +71,11 @@
 #include "../pubsub.h"
 #include "../pipeline.h"
 
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#else
+#error "inttypes.h missing! For Visual Studio, install the C99 header files 
from http://code.google.com/p/msinttypes/";
+#endif
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -900,7 +905,7 @@ static void nn_global_submit_counter (int i, struct nn_sock 
*s,
     int len;
 
     if(self.print_statistics) {
-        fprintf(stderr, "nanomsg: socket.%s: %s: %llu\n",
+        fprintf(stderr, "nanomsg: socket.%s: %s: %" PRIu64 "\n",
             s->socket_name, name, value);
     }
 
@@ -914,11 +919,11 @@ static void nn_global_submit_counter (int i, struct 
nn_sock *s,
 #endif
         strftime (timebuf, 20, "%Y-%m-%dT%H:%M:%S", &strtime);
         if(*s->socket_name) {
-            len = sprintf (buf, "ESTP:%s:%s:socket.%s:%s: %sZ 10 %llu:c",
+            len = sprintf (buf, "ESTP:%s:%s:socket.%s:%s: %sZ 10 %" PRIu64 
":c",
                 self.hostname, self.appname, s->socket_name, name,
                 timebuf, value);
         } else {
-            len = sprintf (buf, "ESTP:%s:%s:socket.%d:%s: %sZ 10 %llu:c",
+            len = sprintf (buf, "ESTP:%s:%s:socket.%d:%s: %sZ 10 %" PRIu64 
":c",
                 self.hostname, self.appname, i, name,
                 timebuf, value);
         }

Other related posts: