From 9735879318c2aeb46203e447f681b8670a71b0e5 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 26 Oct 2020 17:44:43 +0000 Subject: [PATCH] LibMarkdown: Don't parse lines starting with a space as heading This fixes a bug where lines starting with a space would get parsed as "level 0" headings - it would not find a "#" and therefore never increase the level counter (starting at zero), which then would cause the check for "space after #" pass (again, there is no "#"). Eventually we would get funny results like this: [n-1 spaces]oops! Also ASSERT(level > 0) in the Heading constructor. --- Libraries/LibMarkdown/Heading.cpp | 5 +++-- Libraries/LibMarkdown/Heading.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Libraries/LibMarkdown/Heading.cpp b/Libraries/LibMarkdown/Heading.cpp index e0374c97c9..8bee287bfe 100644 --- a/Libraries/LibMarkdown/Heading.cpp +++ b/Libraries/LibMarkdown/Heading.cpp @@ -67,11 +67,12 @@ OwnPtr Heading::parse(Vector::ConstIterator& lines) const StringView& line = *lines; size_t level; - for (level = 0; level < line.length(); level++) + for (level = 0; level < line.length(); level++) { if (line[level] != '#') break; + } - if (level >= line.length() || line[level] != ' ') + if (!level || level >= line.length() || line[level] != ' ') return nullptr; StringView title_view = line.substring_view(level + 1, line.length() - level - 1); diff --git a/Libraries/LibMarkdown/Heading.h b/Libraries/LibMarkdown/Heading.h index f68ebd2cd3..5628f68679 100644 --- a/Libraries/LibMarkdown/Heading.h +++ b/Libraries/LibMarkdown/Heading.h @@ -40,6 +40,7 @@ public: : m_text(move(text)) , m_level(level) { + ASSERT(m_level > 0); } virtual ~Heading() override { }