Commit b3739a42ad8ccb8af83c3a409b872b54be25e8fa

Martin Mitas 2019-12-28T20:21:00

md_is_html_block_start_condition: Fix the MD4C_USE_UTF16 mode. The function was calling md_ascii_case_eq() incorrectly with a number of bytes instead of characters. This could lead to invalid memory accesses and/or mis-detection of raw HTML blocks in the special MD4C_USE_UTF16 builds on Windows.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6a7e917..daed7a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,12 @@
 
 ## Next Version (Work in Progress)
 
+Fixes:
+
+ * Fixed some string length handling in the build mode with `MD4C_USE_UTF16`
+   enabled (Windows-only feature, in order to use `WCHAR` instead of ordinary
+   `char` types).
+
 Changes:
 
  * When building MD4C by hand instead of using its CMake-based build, the UTF-8
@@ -23,6 +29,7 @@ Changes:
 ## Version 0.4.2
 
 Fixes:
+
  * [#98](https://github.com/mity/md4c/issues/98):
    Fix mis-detection of asterisk-encoded emphasis in some corner cases when
    length of the opener and closer differs, as in `**a *b c** d*`.
diff --git a/md4c/md4c.c b/md4c/md4c.c
index d02ae6d..7ba153d 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -5245,7 +5245,7 @@ md_is_html_block_start_condition(MD_CTX* ctx, OFF beg)
 #ifdef X
     #undef X
 #endif
-#define X(name)     { _T(name), sizeof(name)-1 }
+#define X(name)     { _T(name), (sizeof(name)-1) / sizeof(CHAR) }
 #define Xend        { NULL, 0 }
     static const TAG t1[] = { X("script"), X("pre"), X("style"), Xend };
 
@@ -5301,7 +5301,7 @@ md_is_html_block_start_condition(MD_CTX* ctx, OFF beg)
 
         /* Check for type 5: <![CDATA[ */
         if(off + 8 < ctx->size) {
-            if(md_ascii_eq(STR(off), _T("![CDATA["), 8 * sizeof(CHAR)))
+            if(md_ascii_eq(STR(off), _T("![CDATA["), 8))
                 return 5;
         }
     }