Than Ngo f938c2
commit 28b51a7f199d003b309e9dab52457759d5fd7691
Than Ngo f938c2
Author: Jakub Klinkovský <1289205+lahwaacz@users.noreply.github.com>
Than Ngo f938c2
Date:   Thu May 23 21:05:56 2024 +0200
Than Ngo f938c2
Than Ngo f938c2
    Fix buffer overflow in Markdown parser
Than Ngo f938c2
    
Than Ngo f938c2
    This fixes a buffer overflow that happened when parsing a bad Markdown
Than Ngo f938c2
    file with an unclosed emphasis nested in other elements, such as
Than Ngo f938c2
    
Than Ngo f938c2
    ```markdown
Than Ngo f938c2
    > __af_err af_flip(af_array *out, const af_array in, const unsigned dim)__
Than Ngo f938c2
    ```
Than Ngo f938c2
    
Than Ngo f938c2
    This snippet comes from the ArrayFire repository [1]. The problem was
Than Ngo f938c2
    found after the refactoring [2] that introduced std::string_view in the
Than Ngo f938c2
    code. The `std::string_view::operator[]` has bounds checking enabled
Than Ngo f938c2
    when the macro `_GLIBCXX_ASSERTIONS` is defined, which is the case of
Than Ngo f938c2
    Arch Linux build system.
Than Ngo f938c2
    
Than Ngo f938c2
    [1] https://github.com/arrayfire/arrayfire/blob/0a25d36238aa1eee3b775d3584937ca65b0a1807/docs/pages/matrix_manipulation.md
Than Ngo f938c2
    [2] https://github.com/doxygen/doxygen/commit/f4e37514325abe4aa6aeecbc96e9e3e027885aef
Than Ngo f938c2
Than Ngo f938c2
diff --git a/src/markdown.cpp b/src/markdown.cpp
Than Ngo f938c2
index 10429edd5..df00900b0 100644
Than Ngo f938c2
--- a/src/markdown.cpp
Than Ngo f938c2
+++ b/src/markdown.cpp
Than Ngo f938c2
@@ -661,6 +661,11 @@ size_t Markdown::Private::findEmphasisChar(std::string_view data, char c, size_t
Than Ngo f938c2
                      data[i]!='\\' && data[i]!='@' &&
Than Ngo f938c2
                      !(data[i]=='/' && data[i-1]=='<') && // html end tag also ends emphasis
Than Ngo f938c2
                      data[i]!='\n') i++;
Than Ngo f938c2
+    // avoid overflow (unclosed emph token)
Than Ngo f938c2
+    if (i==size)
Than Ngo f938c2
+    {
Than Ngo f938c2
+      return 0;
Than Ngo f938c2
+    }
Than Ngo f938c2
     //printf("findEmphasisChar: data=[%s] i=%d c=%c\n",data,i,data[i]);
Than Ngo f938c2
 
Than Ngo f938c2
     // not counting escaped chars or characters that are unlikely