test-Xnlen-nonarray.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Test non-array inputs to string length functions.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This skeleton file is included from string/test-strnlen-nonarray.c
  16. and wcsmbs/test-wcsnlen-nonarray.c to test that reading of the array
  17. stops at the first null character.
  18. TEST_IDENTIFIER must be the test function identifier. TEST_NAME is
  19. the same as a string.
  20. CHAR must be defined as the character type. */
  21. #include <array_length.h>
  22. #include <string.h>
  23. #include <support/check.h>
  24. #include <support/next_to_fault.h>
  25. #include <support/test-driver.h>
  26. #include <sys/param.h>
  27. #include <unistd.h>
  28. typedef __typeof (TEST_IDENTIFIER) *proto_t;
  29. #define TEST_MAIN
  30. #include "test-string.h"
  31. IMPL (TEST_IDENTIFIER, 1)
  32. static int
  33. test_main (void)
  34. {
  35. enum { buffer_length = 256 };
  36. TEST_VERIFY_EXIT (sysconf (_SC_PAGESIZE) >= buffer_length);
  37. test_init ();
  38. /* Buffer layout: There are a_count 'A' character followed by
  39. zero_count null character, for a total of buffer_length
  40. character:
  41. AAAAA...AAAAA 00000 ... 00000 (unmapped page follows)
  42. \ / \ /
  43. (a_count) (zero_count)
  44. \___ (buffer_length) ___/
  45. ^
  46. |
  47. start_offset
  48. The buffer length does not change, but a_count (and thus _zero)
  49. and start_offset vary.
  50. If start_offset == buffer_length, only 0 is a valid length
  51. argument. The result is 0.
  52. Otherwwise, if zero_count > 0 (if there a null characters in the
  53. buffer), then any length argument is valid. If start_offset <
  54. a_count (i.e., there is a non-null character at start_offset), the
  55. result is the minimum of a_count - start_offset and the length
  56. argument. Otherwise the result is 0.
  57. Otherwise, there are no null characters before the unmapped page.
  58. The length argument must not be greater than buffer_length -
  59. start_offset, and the result is the length argument. */
  60. struct support_next_to_fault ntf
  61. = support_next_to_fault_allocate (buffer_length * sizeof (CHAR));
  62. CHAR *buffer = (CHAR *) ntf.buffer;
  63. FOR_EACH_IMPL (impl, 0)
  64. {
  65. printf ("info: testing %s\n", impl->name);
  66. for (size_t i = 0; i < buffer_length; ++i)
  67. buffer[i] = 'A';
  68. for (int zero_count = 0; zero_count <= buffer_length; ++zero_count)
  69. {
  70. if (zero_count > 0)
  71. buffer[buffer_length - zero_count] = 0;
  72. int a_count = buffer_length - zero_count;
  73. for (int start_offset = 0; start_offset <= buffer_length;
  74. ++start_offset)
  75. {
  76. CHAR *start_pointer = buffer + start_offset;
  77. if (start_offset == buffer_length)
  78. TEST_COMPARE (CALL (impl, buffer + start_offset, 0), 0);
  79. else if (zero_count > 0)
  80. for (int length_argument = 0;
  81. length_argument <= 2 * buffer_length;
  82. ++length_argument)
  83. {
  84. if (test_verbose)
  85. printf ("zero_count=%d a_count=%d start_offset=%d"
  86. " length_argument=%d\n",
  87. zero_count, a_count, start_offset,
  88. length_argument);
  89. if (start_offset < a_count)
  90. TEST_COMPARE (CALL (impl, start_pointer, length_argument),
  91. MIN (a_count - start_offset,
  92. length_argument));
  93. else
  94. TEST_COMPARE (CALL (impl, start_pointer, length_argument),
  95. 0);
  96. }
  97. else
  98. for (int length_argument = 0;
  99. length_argument <= buffer_length - start_offset;
  100. ++length_argument)
  101. TEST_COMPARE (CALL (impl, start_pointer, length_argument),
  102. length_argument);
  103. }
  104. }
  105. }
  106. support_next_to_fault_free (&ntf);
  107. return 0;
  108. }
  109. #include <support/test-driver.c>