Commit 8fa86060 for tesseract
commit 8fa860605c0e7a12775917b4d85b2125b2d0d37a
Author: Stefan Weil <sw@weilnetz.de>
Date: Thu Nov 26 14:39:20 2026 +0100
Fix memory leak in MakeRowFromSubBlobs
make_single_row is used by the page segmentation modes without line
finding (PSM_SINGLE_LINE, PSM_SINGLE_WORD, PSM_CIRCLE_WORD,
PSM_SINGLE_CHAR). If the image has a single blob, MakeRowFromSubBlobs
makes a new C_BLOB from a copy of each child outline of that blob and
puts it into a BLOBNBOX.
A BLOBNBOX only deletes its C_BLOB if it was told that it owns it
(commits f77b1c68 and 6d0595b4), and that was not done here. A copy was
therefore lost when its BLOBNBOX was deleted before the copy had been
moved to a word. In all inputs which showed the leak, that was the
"delete u_line" in restore_underlined_blobs for a copy which
separate_underlines had moved to the list of underlines.
Mark these BLOBNBOXes as owners, like the ones which
assign_blobs_to_blocks2 creates. The code which moves a C_BLOB into a
word already clears the ownership.
The leak was found with LeakSanitizer by a new fuzzer which also uses
other page segmentation modes than PSM_SINGLE_BLOCK. It needed less
than a minute to find it with PSM_SINGLE_LINE.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/textord/makerow.cpp b/src/textord/makerow.cpp
index 414d884b..36ec0a1f 100644
--- a/src/textord/makerow.cpp
+++ b/src/textord/makerow.cpp
@@ -169,6 +169,9 @@ static float MakeRowFromSubBlobs(TO_BLOCK *block, C_BLOB *blob, TO_ROW_IT *row_i
// Correct direction as needed.
blob->CheckInverseFlagAndDirection();
auto *bbox = new BLOBNBOX(blob);
+ // The copy belongs to nobody else, so the BLOBNBOX has to delete it
+ // unless it is moved to a word later.
+ bbox->set_owns_cblob(true);
bb_it.add_after_then_move(bbox);
}
// Now we can make a row from the blobs.