From 71948018f48ed428f63b34b29c909dea9d744d10 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 24 May 2018 17:51:40 +0200 Subject: [PATCH] Add a sanity check for AdjustPagebreak() implementation Verify that this function never adjusts page break so far back that it comes before the previous one (or even at the same position). This avoids infinite loops in CountPages() even if a custom cell class implements its overridden AdjustPagebreak() incorrectly. --- src/html/htmprint.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/html/htmprint.cpp b/src/html/htmprint.cpp index 5a1e4d0c87..f42b008fff 100644 --- a/src/html/htmprint.cpp +++ b/src/html/htmprint.cpp @@ -151,9 +151,16 @@ int wxHtmlDCRenderer::FindNextPageBreak(const wxArrayInt& known_pagebreaks, if ( pos != 0 && pos >= GetTotalHeight() ) return wxNOT_FOUND; - pos += m_Height; - m_Cells->AdjustPagebreak(&pos, known_pagebreaks, m_Height); - return pos; + int posNext = pos + m_Height; + if ( m_Cells->AdjustPagebreak(&posNext, known_pagebreaks, m_Height) ) + { + // Check that AdjustPagebreak() returns the page break at a strictly + // greater position than that of the previous page, otherwise + // CountPages() would enter into an infinite loop. + wxCHECK_MSG( posNext > pos, wxNOT_FOUND, "Bug in AdjustPagebreak()" ); + } + + return posNext; } void wxHtmlDCRenderer::Render(int x, int y, int from, int to)