Fix crash caused by lastIndexOf returning negative numbers

The ArticleParser would crash when a negative number was returned by
lastIndexOf, since it was casted to an unsigned integer. This unsigned
integer then was used to allocate memory, which would allocate way to
much memory, causing a MemoryException and crashing the process.
This commit is contained in:
Chris Josten 2021-10-13 14:07:27 +02:00
parent 48b95c4a13
commit 1d0d1a54b1
2 changed files with 3 additions and 3 deletions

View file

@ -34,9 +34,9 @@ class Article : Page {
// Find the first header and mark everything up to that as
if (m_excerpt is null) {
// an excerpt, used in search results.
const uint seperatorIndex = cast(uint) indexOf(m_contentSource, "---\n");
const long seperatorIndex = cast(long) lastIndexOf(m_contentSource, "---\n");
this.m_excerpt = this.m_contentSource[seperatorIndex + 4..$];
const uint firstHeaderIndex = cast(uint) indexOf(this.m_excerpt, '#');
const long firstHeaderIndex = indexOf(this.m_excerpt, '#');
if (firstHeaderIndex >= 0) {
this.m_excerpt = this.m_excerpt[0..firstHeaderIndex];
}

View file

@ -55,7 +55,7 @@ class Page {
this.m_name = file;
this.m_contentSource = readText(file);
// Find the seperator and split the string in two
const uint seperatorIndex = cast(uint) lastIndexOf(m_contentSource, "\n---\n");
const long seperatorIndex = lastIndexOf(m_contentSource, "\n---\n");
enforce!ArticleParseException(seperatorIndex >= 0);
string header = m_contentSource[0..seperatorIndex];