about summary refs log tree commit diff
path: root/support/check.h
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2018-01-08 13:01:36 +0100
committerFlorian Weimer <fweimer@redhat.com>2018-01-08 20:07:24 +0100
commit2b3aa44656dd873e2753c98fdcb95be6a9d147a6 (patch)
tree7c6df54631968d08cc407487b20b93c5123baac6 /support/check.h
parent630f4cc3aa019ede55976ea561f1a7af2f068639 (diff)
downloadglibc-2b3aa44656dd873e2753c98fdcb95be6a9d147a6.tar.gz
glibc-2b3aa44656dd873e2753c98fdcb95be6a9d147a6.tar.xz
glibc-2b3aa44656dd873e2753c98fdcb95be6a9d147a6.zip
support: Increase usability of TEST_COMPARE
The previous implementation of the TEST_COMPARE macro would fail
to compile code like this:

  int ret = res_send (query, sizeof (query), buf, sizeof (buf));
  TEST_COMPARE (ret,
                sizeof (query)
                + 2             /* Compression reference.  */
                + 2 + 2 + 4 + 2 /* Type, class, TTL, RDATA length.  */
                + 1             /* Pascal-style string length.  */
                + strlen (expected_name));

This resulted in a failed static assertion, "integer conversions
may alter sign of operands".  A user of the TEST_COMPARE would have
to add a cast to fix this.

This patch reverts to the original proposed solution of a run-time
check, making TEST_COMPARE usable for comparisons of numbers with
types with different signedness in more contexts.
Diffstat (limited to 'support/check.h')
-rw-r--r--support/check.h35
1 files changed, 11 insertions, 24 deletions
diff --git a/support/check.h b/support/check.h
index a28e650aa3..2192f38941 100644
--- a/support/check.h
+++ b/support/check.h
@@ -102,7 +102,9 @@ void support_record_failure (void);
     typedef __typeof__ (+ (right)) __right_type;                        \
     __left_type __left_value = (left);                                  \
     __right_type __right_value = (right);                               \
-    /* Prevent use with floating-point and boolean types.  */           \
+    int __left_is_positive = __left_value > 0;                          \
+    int __right_is_positive = __right_value > 0;                        \
+    /* Prevent use with floating-point types.  */                       \
     support_static_assert ((__left_type) 1.0 == (__left_type) 1.5,      \
                            "left value has floating-point type");       \
     support_static_assert ((__right_type) 1.0 == (__right_type) 1.5,    \
@@ -112,33 +114,18 @@ void support_record_failure (void);
                            "left value fits into long long");           \
     support_static_assert (sizeof (__right_value) <= sizeof (long long), \
                     "right value fits into long long");                 \
-    /* Make sure that integer conversions does not alter the sign.   */ \
-    enum                                                                \
-    {                                                                   \
-      __left_is_unsigned = (__left_type) -1 > 0,                        \
-      __right_is_unsigned = (__right_type) -1 > 0,                      \
-      __unsigned_left_converts_to_wider = (__left_is_unsigned           \
-                                           && (sizeof (__left_value)    \
-                                               < sizeof (__right_value))), \
-      __unsigned_right_converts_to_wider = (__right_is_unsigned         \
-                                            && (sizeof (__right_value)  \
-                                                < sizeof (__left_value))) \
-    };                                                                  \
-    support_static_assert (__left_is_unsigned == __right_is_unsigned    \
-                           || __unsigned_left_converts_to_wider         \
-                    || __unsigned_right_converts_to_wider,              \
-                    "integer conversions may alter sign of operands");  \
     /* Compare the value.  */                                           \
-    if (__left_value != __right_value)                                  \
+    if (__left_value != __right_value                                   \
+        || __left_is_positive != __right_is_positive)                   \
       /* Pass the sign for printing the correct value.  */              \
       support_test_compare_failure                                      \
         (__FILE__, __LINE__,                                            \
-         #left, __left_value, __left_value < 0, sizeof (__left_type),   \
-         #right, __right_value, __right_value < 0, sizeof (__right_type)); \
+         #left, __left_value, __left_is_positive, sizeof (__left_type), \
+         #right, __right_value, __right_is_positive, sizeof (__right_type)); \
   })
 
-/* Internal implementation of TEST_COMPARE.  LEFT_NEGATIVE and
-   RIGHT_NEGATIVE are used to store the sign separately, so that both
+/* Internal implementation of TEST_COMPARE.  LEFT_POSITIVE and
+   RIGHT_POSITIVE are used to store the sign separately, so that both
    unsigned long long and long long arguments fit into LEFT_VALUE and
    RIGHT_VALUE, and the function can still print the original value.
    LEFT_SIZE and RIGHT_SIZE specify the size of the argument in bytes,
@@ -146,11 +133,11 @@ void support_record_failure (void);
 void support_test_compare_failure (const char *file, int line,
                                    const char *left_expr,
                                    long long left_value,
-                                   int left_negative,
+                                   int left_positive,
                                    int left_size,
                                    const char *right_expr,
                                    long long right_value,
-                                   int right_negative,
+                                   int right_positive,
                                    int right_size);