+2025-01-04 Bruno Haible <bruno@clisp.org>
+
+ mbs_endswith: Fix abort in the case of incomplete characters.
+ Reported by Paul Eggert.
+ * lib/mbs_endswith.c: Don't include <stdlib.h>.
+ (mbs_endswith): Instead of aborting, return false.
+ * tests/test-mbs_endswith2.c (main): Test invalid and incomplete
+ characters.
+
2025-01-04 Bruno Haible <bruno@clisp.org>
doc: Deal with tables that are overloaded in info mode.
#include "mbiter.h"
-#include <stdlib.h>
-
bool
mbs_endswith (const char *string, const char *suffix)
{
for (; len > n; len--)
{
if (!mbi_avail (iter))
- abort ();
+ /* We can get here due to incomplete multibyte characters. */
+ return false;
mbi_advance (iter);
}
if (!mbi_avail (iter))
- abort ();
+ /* We can get here due to incomplete multibyte characters. */
+ return false;
return strcmp (mbi_cur_ptr (iter), suffix) == 0;
}
}
- return 0;
+ return false;
}
ASSERT (mbs_endswith ("\341\272\213\303\277\341\272\221", "\341\272\213\303\277\341\272\221")); /* "ẋÿẑ" "ẋÿẑ" */
ASSERT (mbs_endswith ("\303\277\341\272\213\341\272\213\303\277\341\272\221", "\341\272\213\303\277\341\272\221")); /* "ÿẋẋÿẑ" "ẋÿẑ" */
+ /* Test cases with invalid or incomplete characters. */
+
+ /* A valid character should not match an invalid character. */
+ ASSERT (!mbs_endswith ("\303\247", "\301\247"));
+ ASSERT (!mbs_endswith ("\301\247", "\303\247"));
+
+ /* A valid character should not match an incomplete character. */
+ ASSERT (!mbs_endswith ("\303\247", "\343\247"));
+ ASSERT (!mbs_endswith ("\343\247", "\303\247"));
+
+ /* An invalid character should not match an incomplete character. */
+ ASSERT (!mbs_endswith ("\301\247", "\343\247"));
+ ASSERT (!mbs_endswith ("\343\247", "\301\247"));
+
+ /* Two invalid characters should match only if they are identical. */
+ ASSERT (!mbs_endswith ("\301\246", "\301\247"));
+ ASSERT (!mbs_endswith ("\301\247", "\301\246"));
+ ASSERT (mbs_endswith ("\301\247", "\301\247"));
+
+ /* Two incomplete characters should match only if they are identical. */
+ ASSERT (!mbs_endswith ("\343\246", "\343\247"));
+ ASSERT (!mbs_endswith ("\343\247", "\343\246"));
+ ASSERT (mbs_endswith ("\343\247", "\343\247"));
+
return test_exit_status;
}