From 1d0d1a54b1e2c6154b12f54f21152a596d7d75b6 Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Wed, 13 Oct 2021 14:07:27 +0200 Subject: [PATCH] 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. --- source/nl/netsoj/chris/blog/model/article.d | 4 ++-- source/nl/netsoj/chris/blog/model/page.d | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/nl/netsoj/chris/blog/model/article.d b/source/nl/netsoj/chris/blog/model/article.d index ef4b838..a2b10d4 100644 --- a/source/nl/netsoj/chris/blog/model/article.d +++ b/source/nl/netsoj/chris/blog/model/article.d @@ -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]; } diff --git a/source/nl/netsoj/chris/blog/model/page.d b/source/nl/netsoj/chris/blog/model/page.d index 833cf46..d80faf0 100644 --- a/source/nl/netsoj/chris/blog/model/page.d +++ b/source/nl/netsoj/chris/blog/model/page.d @@ -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];