tst-iconv-sticky-input-error.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Test __GCONV_ENCOUNTERED_ILLEGAL_INPUT, as used by iconv -c (bug 32046).
  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. #include <array_length.h>
  16. #include <errno.h>
  17. #include <gconv_int.h>
  18. #include <iconv.h>
  19. #include <stdbool.h>
  20. #include <support/check.h>
  21. #include <support/support.h>
  22. #include <support/test-driver.h>
  23. #include <stdio.h>
  24. #include <libc-diag.h>
  25. /* FROM is the input character set, TO the output character set. If
  26. IGNORE is true, the iconv descriptor is set up in the same way as
  27. iconv -c would. INPUT is the input string, EXPECTED_OUTPUT the
  28. output. OUTPUT_LIMIT is a byte count, specifying how many input
  29. bytes are passed to the iconv function on each invocation. */
  30. static void
  31. one_direction (const char *from, const char *to, bool ignore,
  32. const char *input, const char *expected_output,
  33. size_t output_limit)
  34. {
  35. if (test_verbose)
  36. {
  37. char *quoted_input = support_quote_string (input);
  38. char *quoted_output = support_quote_string (expected_output);
  39. printf ("info: testing from=\"%s\" to=\"%s\" ignore=%d input=\"%s\""
  40. " expected_output=\"%s\" output_limit=%zu\n",
  41. from, to, (int) ignore, quoted_input,
  42. quoted_output, output_limit);
  43. free (quoted_output);
  44. free (quoted_input);
  45. }
  46. __gconv_t cd;
  47. if (ignore)
  48. {
  49. struct gconv_spec conv_spec;
  50. TEST_VERIFY_EXIT (__gconv_create_spec (&conv_spec, from, to)
  51. == &conv_spec);
  52. conv_spec.ignore = true;
  53. cd = (iconv_t) -1;
  54. TEST_COMPARE (__gconv_open (&conv_spec, &cd, 0), __GCONV_OK);
  55. __gconv_destroy_spec (&conv_spec);
  56. }
  57. else
  58. cd = iconv_open (to, from);
  59. TEST_VERIFY_EXIT (cd != (iconv_t) -1);
  60. char *input_ptr = (char *) input;
  61. size_t input_len = strlen (input);
  62. char output_buf[20];
  63. char *output_ptr = output_buf;
  64. size_t output_len;
  65. do
  66. {
  67. output_len = array_end (output_buf) - output_ptr;
  68. if (output_len > output_limit)
  69. /* Limit the buffer size as requested by the caller. */
  70. output_len = output_limit;
  71. TEST_VERIFY_EXIT (output_len > 0);
  72. if (input_len == 0)
  73. /* Trigger final flush. */
  74. input_ptr = NULL;
  75. char *old_input_ptr = input_ptr;
  76. size_t ret = iconv (cd, &input_ptr, &input_len,
  77. &output_ptr, &output_len);
  78. if (ret == (size_t) -1)
  79. {
  80. if (errno != EILSEQ)
  81. TEST_COMPARE (errno, E2BIG);
  82. }
  83. if (input_ptr == old_input_ptr)
  84. /* Avoid endless loop if stuck on an invalid input character. */
  85. break;
  86. }
  87. while (input_ptr != NULL);
  88. /* Test the sticky illegal input bit. */
  89. TEST_VERIFY (__gconv_has_illegal_input (cd));
  90. TEST_COMPARE_BLOB (expected_output, strlen (expected_output),
  91. output_buf, output_ptr - output_buf);
  92. TEST_COMPARE (iconv_close (cd), 0);
  93. }
  94. static int
  95. do_test (void)
  96. {
  97. static const char charsets[][14] =
  98. {
  99. "ASCII",
  100. "ASCII//IGNORE",
  101. "UTF-8",
  102. "UTF-8//IGNORE",
  103. };
  104. for (size_t from_idx = 0; from_idx < array_length (charsets); ++from_idx)
  105. for (size_t to_idx = 0; to_idx < array_length (charsets); ++to_idx)
  106. for (int do_ignore = 0; do_ignore < 2; ++do_ignore)
  107. for (int limit = 1; limit < 5; ++limit)
  108. for (int skip = 0; skip < 3; ++skip)
  109. {
  110. const char *expected_output;
  111. DIAG_PUSH_NEEDS_COMMENT_CLANG;
  112. DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
  113. if (do_ignore || strstr (charsets[to_idx], "//IGNORE") != NULL)
  114. expected_output = "ABXY" + skip;
  115. else
  116. expected_output = "AB" + skip;
  117. one_direction (charsets[from_idx], charsets[to_idx], do_ignore,
  118. "AB\xffXY" + skip, expected_output, limit);
  119. DIAG_POP_NEEDS_COMMENT_CLANG;
  120. }
  121. return 0;
  122. }
  123. #include <support/test-driver.c>