Fixed format specifier width mismatch
authorJoseph Ates <joseph.ates@msasafety.com>
Sun, 20 Dec 2015 21:29:09 +0000 (16:29 -0500)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 21 Sep 2016 09:25:46 +0000 (11:25 +0200)
On some platforms PRIu64 is not necessarily the width of the
LargestIntegralType. A new decimal format specifier for
LargestIntegralType was added and replaces the invocations of PRIu64.

include/cmocka.h
src/cmocka.c

index 282963d..4be40ea 100644 (file)
@@ -79,7 +79,7 @@ typedef uintmax_t LargestIntegralType;
 #endif /* LargestIntegralType */
 #endif /* DOXYGEN */
 
-/* Printf format used to display LargestIntegralType. */
+/* Printf format used to display LargestIntegralType as a hexidecimal. */
 #ifndef LargestIntegralTypePrintfFormat
 # ifdef _WIN32
 #  define LargestIntegralTypePrintfFormat "0x%I64x"
@@ -92,6 +92,19 @@ typedef uintmax_t LargestIntegralType;
 # endif /* _WIN32 */
 #endif /* LargestIntegralTypePrintfFormat */
 
+/* Printf format used to display LargestIntegralType as a decimal. */
+#ifndef LargestIntegralTypePrintfFormatDecimal
+# ifdef _WIN32
+#  define LargestIntegralTypePrintfFormatDecimal "%I64u"
+# else
+#  if __WORDSIZE == 64
+#   define LargestIntegralTypePrintfFormatDecimal "%lu"
+#  else
+#   define LargestIntegralTypePrintfFormatDecimal "%llu"
+#  endif
+# endif /* _WIN32 */
+#endif /* LargestIntegralTypePrintfFormat */
+
 /* Perform an unsigned cast to LargestIntegralType. */
 #define cast_to_largest_integral_type(value) \
     ((LargestIntegralType)(value))
index 829dd7a..abe1863 100644 (file)
@@ -1055,10 +1055,11 @@ static int value_in_set_display_error(
         if (succeeded) {
             return 1;
         }
-        cm_print_error("%" PRIu64 " is %sin the set (", value,
-                       invert ? "" : "not ");
+        cm_print_error(LargestIntegralTypePrintfFormatDecimal
+                       " is %sin the set (",
+                       value, invert ? "" : "not ");
         for (i = 0; i < size_of_set; i++) {
-            cm_print_error("%" PRIu64 ", ", set[i]);
+            cm_print_error(LargestIntegralTypePrintfFormat ", ", set[i]);
         }
         cm_print_error(")\n");
     }
@@ -1077,7 +1078,10 @@ static int integer_in_range_display_error(
     if (value >= range_min && value <= range_max) {
         return 1;
     }
-    cm_print_error("%" PRIu64 " is not within the range %" PRIu64 "-%" PRIu64 "\n",
+    cm_print_error(LargestIntegralTypePrintfFormatDecimal
+                   " is not within the range "
+                   LargestIntegralTypePrintfFormatDecimal "-"
+                   LargestIntegralTypePrintfFormatDecimal "\n",
                    value, range_min, range_max);
     return 0;
 }
@@ -1094,7 +1098,10 @@ static int integer_not_in_range_display_error(
     if (value < range_min || value > range_max) {
         return 1;
     }
-    cm_print_error("%" PRIu64 " is within the range %" PRIu64 "-%" PRIu64 "\n",
+    cm_print_error(LargestIntegralTypePrintfFormatDecimal
+                   " is within the range "
+                   LargestIntegralTypePrintfFormatDecimal "-"
+                   LargestIntegralTypePrintfFormatDecimal "\n",
                    value, range_min, range_max);
     return 0;
 }
@@ -1572,7 +1579,8 @@ void _assert_return_code(const LargestIntegralType result,
 
     if (result > valmax - 1) {
         if (error > 0) {
-            cm_print_error("%s < 0, errno(%" PRIu64 "): %s\n",
+            cm_print_error("%s < 0, errno("
+                           LargestIntegralTypePrintfFormatDecimal "): %s\n",
                            expression, error, strerror((int)error));
         } else {
             cm_print_error("%s < 0\n", expression);