diff mbox series

support: Report errno constants in TEST_COMPARE failures

Message ID 875xrsr6fa.fsf@oldenburg.str.redhat.com
State New
Headers show
Series support: Report errno constants in TEST_COMPARE failures | expand

Commit Message

Florian Weimer Aug. 22, 2024, 2:14 p.m. UTC
If the expression is errno, decode it as an errno constant
using strerrorname_np.

---
 support/support_test_compare_failure.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Comments

Arjun Shankar Aug. 26, 2024, 2:37 p.m. UTC | #1
Hi Florian,

> If the expression is errno, decode it as an errno constant
> using strerrorname_np.

This looks good to me.
Reviewed-by: Arjun Shankar <arjun@redhat.com>

I also tested it by forcing an LHS and an RHS errno expression
comparison to fail:

tst-nss-test_errno.c:48: numeric comparison failure
   left: 1 (0x1, EPERM); from: errno
  right: 0 (0x0); from: 0

tst-fgetsgent_r.c:149: numeric comparison failure
   left: 35 (0x23); from: ret
  right: 34 (0x22, ERANGE); from: errno

>
> ---
>  support/support_test_compare_failure.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/support/support_test_compare_failure.c b/support/support_test_compare_failure.c
> index ae73d200cd..dba79e413f 100644
> --- a/support/support_test_compare_failure.c
> +++ b/support/support_test_compare_failure.c
> @@ -17,7 +17,9 @@
>     <https://www.gnu.org/licenses/>.  */
>
>  #include <errno.h>
> +#include <limits.h>

OK. Needed for INT_MAX.

>  #include <stdio.h>
> +#include <string.h>

OK. Needed for strerrorname_np.

>  #include <support/check.h>
>
>  static void
> @@ -31,7 +33,14 @@ report (const char *which, const char *expr, long long value, int positive,
>      printf ("%lld", value);
>    unsigned long long mask
>      = (~0ULL) >> (8 * (sizeof (unsigned long long) - size));
> -  printf (" (0x%llx); from: %s\n", (unsigned long long) value & mask, expr);

OK. We used to print the hexadecimal representation of the expression
as a ULL, followed by the expression itself (as a string).

> +  const char *errno_constant = NULL;
> +  if (strcmp (expr, "errno") == 0
> +      && positive && (unsigned long long int) value <= INT_MAX)
> +    errno_constant = strerrorname_np (value);

OK. If the expression is "errno" and a not-too-big positive number
(all valid errors are), then we try to get the error code's name. If
it's too not a valid errno value, strerrorname_np returns NULL which
is fine for our needs.

> +  printf (" (0x%llx", (unsigned long long) value & mask);

OK. First we print the value.

> +  if (errno_constant != NULL)
> +    printf (", %s", errno_constant);

OK. Then if it's the errno expression, the error code name (assuming
strerrorname_np succeeded). This is the new bit.

> +  printf ("); from: %s\n", expr);

OK. Then we print the expression string.

>  }
>
>  void
>
diff mbox series

Patch

diff --git a/support/support_test_compare_failure.c b/support/support_test_compare_failure.c
index ae73d200cd..dba79e413f 100644
--- a/support/support_test_compare_failure.c
+++ b/support/support_test_compare_failure.c
@@ -17,7 +17,9 @@ 
    <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
+#include <string.h>
 #include <support/check.h>
 
 static void
@@ -31,7 +33,14 @@  report (const char *which, const char *expr, long long value, int positive,
     printf ("%lld", value);
   unsigned long long mask
     = (~0ULL) >> (8 * (sizeof (unsigned long long) - size));
-  printf (" (0x%llx); from: %s\n", (unsigned long long) value & mask, expr);
+  const char *errno_constant = NULL;
+  if (strcmp (expr, "errno") == 0
+      && positive && (unsigned long long int) value <= INT_MAX)
+    errno_constant = strerrorname_np (value);
+  printf (" (0x%llx", (unsigned long long) value & mask);
+  if (errno_constant != NULL)
+    printf (", %s", errno_constant);
+  printf ("); from: %s\n", expr);
 }
 
 void