malloc: Fix a realloc crash with heap tagging [BZ 27468]
[glibc.git] / support / support_test_compare_string.c
blobcbeaf7b1eeea8ca8a742a7b807ba868d1cd5d767
1 /* Check two strings for equality.
2 Copyright (C) 2018-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <support/check.h>
23 #include <support/support.h>
24 #include <support/xmemstream.h>
26 static void
27 report_length (const char *what, const char *str, size_t length)
29 if (str == NULL)
30 printf (" %s string: NULL\n", what);
31 else
32 printf (" %s string: %zu bytes\n", what, length);
35 static void
36 report_string (const char *what, const unsigned char *blob,
37 size_t length, const char *expr)
39 if (length > 0)
41 printf (" %s (evaluated from %s):\n", what, expr);
42 char *quoted = support_quote_blob (blob, length);
43 printf (" \"%s\"\n", quoted);
44 free (quoted);
46 fputs (" ", stdout);
47 for (size_t i = 0; i < length; ++i)
48 printf (" %02X", blob[i]);
49 putc ('\n', stdout);
53 static size_t
54 string_length_or_zero (const char *str)
56 if (str == NULL)
57 return 0;
58 else
59 return strlen (str);
62 void
63 support_test_compare_string (const char *left, const char *right,
64 const char *file, int line,
65 const char *left_expr, const char *right_expr)
67 /* Two null pointers are accepted. */
68 if (left == NULL && right == NULL)
69 return;
71 size_t left_length = string_length_or_zero (left);
72 size_t right_length = string_length_or_zero (right);
74 if (left_length != right_length || left == NULL || right == NULL
75 || memcmp (left, right, left_length) != 0)
77 support_record_failure ();
78 printf ("%s:%d: error: string comparison failed\n", file, line);
79 if (left_length == right_length && right != NULL && left != NULL)
80 printf (" string length: %zu bytes\n", left_length);
81 else
83 report_length ("left", left, left_length);
84 report_length ("right", right, right_length);
86 report_string ("left", (const unsigned char *) left,
87 left_length, left_expr);
88 report_string ("right", (const unsigned char *) right,
89 right_length, right_expr);