Commit 5a44e327a02fdd3ef9bdbcd709eb864e5cde293d

Martin Mitas 2020-12-14T18:59:56

md_link_label_cmp: Fix the loop end condition. The old version likely could stop prematurely in a corner case when there was a Unicode character at the end of the either string, which maps into multiple fold info codepoints. Fixes #142.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index efe693f..256951d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,10 @@ Fixes:
  * [#139](https://github.com/mity/md4c/issues/139):
    Recognize a list item mark even when EOF follows it.
 
+ * [#142](https://github.com/mity/md4c/issues/142):
+   Fix reference link definition label matching in a case when the label ends
+   with a Unicode character with non-trivial case folding mapping.
+
 
 ## Version 0.4.6
 
diff --git a/src/md4c.c b/src/md4c.c
index 233b8ea..0883a9e 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -1589,8 +1589,6 @@ md_link_label_cmp(const CHAR* a_label, SZ a_size, const CHAR* b_label, SZ b_size
 {
     OFF a_off;
     OFF b_off;
-    int a_reached_end = FALSE;
-    int b_reached_end = FALSE;
     MD_UNICODE_FOLD_INFO a_fi = { { 0 }, 0 };
     MD_UNICODE_FOLD_INFO b_fi = { { 0 }, 0 };
     OFF a_fi_off = 0;
@@ -1599,17 +1597,17 @@ md_link_label_cmp(const CHAR* a_label, SZ a_size, const CHAR* b_label, SZ b_size
 
     a_off = md_skip_unicode_whitespace(a_label, 0, a_size);
     b_off = md_skip_unicode_whitespace(b_label, 0, b_size);
-    while(!a_reached_end  ||  !b_reached_end) {
+    while(a_off < a_size || a_fi_off < a_fi.n_codepoints ||
+          b_off < b_size || b_fi_off < b_fi.n_codepoints)
+    {
         /* If needed, load fold info for next char. */
         if(a_fi_off >= a_fi.n_codepoints) {
             a_fi_off = 0;
             a_off = md_link_label_cmp_load_fold_info(a_label, a_off, a_size, &a_fi);
-            a_reached_end = (a_off >= a_size);
         }
         if(b_fi_off >= b_fi.n_codepoints) {
             b_fi_off = 0;
             b_off = md_link_label_cmp_load_fold_info(b_label, b_off, b_size, &b_fi);
-            b_reached_end = (b_off >= b_size);
         }
 
         cmp = b_fi.codepoints[b_fi_off] - a_fi.codepoints[a_fi_off];
diff --git a/test/coverage.txt b/test/coverage.txt
index 4b504e1..d17fd94 100644
--- a/test/coverage.txt
+++ b/test/coverage.txt
@@ -296,6 +296,16 @@ foo
 ````````````````````````````````
 
 
+### [Issue 142](https://github.com/mity/md4c/issues/142)
+
+```````````````````````````````` example
+[fooﬗ]: /url
+[fooﬕ]
+.
+[fooﬕ]
+````````````````````````````````
+
+
 ## Code coverage
 
 ### `md_is_unicode_whitespace__()`