From 96b8b15ae5c3715520fa9ad80ca03e0e30803e50 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 4 Jan 2025 15:19:46 +0100 Subject: [PATCH] mbs_endswith: Fix abort in the case of incomplete characters. Reported by Paul Eggert. * lib/mbs_endswith.c: Don't include . (mbs_endswith): Instead of aborting, return false. * tests/test-mbs_endswith2.c (main): Test invalid and incomplete characters. --- ChangeLog | 9 +++++++++ lib/mbs_endswith.c | 10 +++++----- tests/test-mbs_endswith2.c | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index fe14499cbb..06819500ab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2025-01-04 Bruno Haible + + mbs_endswith: Fix abort in the case of incomplete characters. + Reported by Paul Eggert. + * lib/mbs_endswith.c: Don't include . + (mbs_endswith): Instead of aborting, return false. + * tests/test-mbs_endswith2.c (main): Test invalid and incomplete + characters. + 2025-01-04 Bruno Haible doc: Deal with tables that are overloaded in info mode. diff --git a/lib/mbs_endswith.c b/lib/mbs_endswith.c index 8f163e1aa5..00d5128d43 100644 --- a/lib/mbs_endswith.c +++ b/lib/mbs_endswith.c @@ -23,8 +23,6 @@ #include "mbiter.h" -#include - bool mbs_endswith (const char *string, const char *suffix) { @@ -62,13 +60,15 @@ 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; } diff --git a/tests/test-mbs_endswith2.c b/tests/test-mbs_endswith2.c index f76f9b04e4..35c3b8652d 100644 --- a/tests/test-mbs_endswith2.c +++ b/tests/test-mbs_endswith2.c @@ -62,5 +62,29 @@ main () 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; } -- 2.39.5