Avoid OOM diring HTML parsing by reducing max block size and freeing unneeded buffers

This commit is contained in:
spfenwick
2026-05-02 16:07:27 +12:00
parent 913b7e5f8b
commit 5928f32364
2 changed files with 14 additions and 6 deletions
@@ -1207,11 +1207,12 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
self->partWordBuffer[self->partWordBufferIndex++] = s[i];
}
// If we have > 750 words buffered up, perform the layout and consume out all but the last line
// There should be enough here to build out 1-2 full pages and doing this will free up a lot of
// memory.
// Spotted when reading Intermezzo, there are some really long text blocks in there.
if (self->currentTextBlock->size() > 750) {
// Flush when words approach the doubling boundary that would require a
// large contiguous realloc. 96 fires before capacity reaches 128
// (the next doubling after 64), keeping the realloc below 1.5KB and
// releasing excess capacity via shrink_to_fit in layoutAndExtractLines.
// The original 750-word threshold was too late for low-heap devices.
if (self->currentTextBlock->size() > 96) {
LOG_DBG("EHP", "Text block too long, splitting into multiple pages");
const int horizontalInset = self->currentTextBlock->getBlockStyle().totalHorizontalInset();
const uint16_t effectiveWidth = (horizontalInset < self->viewportWidth)