[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Fixed format specifier width mismatch


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 | 15 ++++++++++++++-
 src/cmocka.c     | 20 ++++++++++++++------
 2 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/include/cmocka.h b/include/cmocka.h
index 6242ff2..0a255e9 100644
--- a/include/cmocka.h
+++ b/include/cmocka.h
@@ -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))
diff --git a/src/cmocka.c b/src/cmocka.c
index 310073d..b88d5e5 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -1049,10 +1049,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");
     }
@@ -1071,7 +1072,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;
 }
@@ -1088,7 +1092,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;
 }
@@ -1566,7 +1573,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);
-- 
2.6.4



Follow-Ups:
Re: [PATCH] Fixed format specifier width mismatchLukas Slebodnik <lslebodn@xxxxxxxxxx>