about summary refs log tree commit diff
path: root/support/tst-test_compare.c
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/tst-test_compare.c
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/tst-test_compare.c')
-rw-r--r--support/tst-test_compare.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/support/tst-test_compare.c b/support/tst-test_compare.c
index 340268fadf..123ba1bc3c 100644
--- a/support/tst-test_compare.c
+++ b/support/tst-test_compare.c
@@ -42,6 +42,22 @@ struct bitfield
   unsigned long long int u63 : 63;
 };
 
+/* Functions which return signed sizes are common, so test that these
+   results can readily checked using TEST_COMPARE.  */
+
+static int
+return_ssize_t (void)
+{
+  return 4;
+}
+
+static int
+return_int (void)
+{
+  return 4;
+}
+
+
 static int
 do_test (void)
 {
@@ -53,6 +69,8 @@ do_test (void)
     unsigned short u16 = 3;
     TEST_COMPARE (i8, u16);
   }
+  TEST_COMPARE (return_ssize_t (), sizeof (char[4]));
+  TEST_COMPARE (return_int (), sizeof (char[4]));
 
   struct bitfield bitfield = { 0 };
   TEST_COMPARE (bitfield.i2, bitfield.i3);