mbs_endswith: Fix abort in the case of incomplete characters.
authorBruno Haible <bruno@clisp.org>
Sat, 4 Jan 2025 14:19:46 +0000 (15:19 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 4 Jan 2025 17:20:51 +0000 (18:20 +0100)
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.

ChangeLog
lib/mbs_endswith.c
tests/test-mbs_endswith2.c

index fe14499cbb761dc1b906d4c0852ed5fdb6a92ddf..06819500abc6e6fd544f76cc125f45c153f3e890 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+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.
index 8f163e1aa5ad78379000c5976e15a61871c076cc..00d5128d4384ab4cd1b241b5698d5b9d2ea9391a 100644 (file)
@@ -23,8 +23,6 @@
 
 #include "mbiter.h"
 
-#include <stdlib.h>
-
 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;
 }
index f76f9b04e436d72a7730868477b489bddf8f4719..35c3b8652d47308f4d28e4c4f2f79f5514ccd122 100644 (file)
@@ -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;
 }