Commit 2da83fd8 for tesseract

commit 2da83fd880b508c9f7e667b43f4aa230effd045b
Author: Stefan Weil <sw@weilnetz.de>
Date:   Sun Jul 12 21:42:42 2026 +0200

    Fix memory leak and partial mutation in SquishedDawg::read_squished_dawg

    Free existing edges_ before re-allocating to prevent leak when Load()
    is called more than once on the same instance. On all failure paths
    after allocation, delete edges_, reset it to nullptr and num_edges_ to
    0 so the object is not left partially mutated.

    Assisted-by: OpenCode / big-pickle (opencode)
    Reported-by: GitHub Copilot
    Signed-off-by: Stefan Weil <stweil@tessus.org>

diff --git a/src/dict/dawg.cpp b/src/dict/dawg.cpp
index 93a18f6e..060b565d 100644
--- a/src/dict/dawg.cpp
+++ b/src/dict/dawg.cpp
@@ -381,8 +381,12 @@ bool SquishedDawg::read_squished_dawg(TFile *file) {
   }
   Dawg::init(unicharset_size);

+  delete[] edges_;
   edges_ = new EDGE_RECORD[num_edges_];
   if (!file->DeSerialize(&edges_[0], num_edges_)) {
+    delete[] edges_;
+    edges_ = nullptr;
+    num_edges_ = 0;
     return false;
   }
   // Validate the loaded edge structure: check that next_node values are in
@@ -399,6 +403,9 @@ bool SquishedDawg::read_squished_dawg(TFile *file) {
         NODE_REF nj = next_node_from_edge_rec(edges_[j]);
         if (nj != 0 && static_cast<uint32_t>(nj) >= num_edges_) {
           tprintf("Dawg edge %u has out-of-bounds next_node\n", j);
+          delete[] edges_;
+          edges_ = nullptr;
+          num_edges_ = 0;
           return false;
         }
         if (last_edge(j)) {
@@ -410,12 +417,18 @@ bool SquishedDawg::read_squished_dawg(TFile *file) {
       } while (j < num_edges_);
       if (!terminated) {
         tprintf("Dawg forward edge run starting at %u is not terminated\n", i);
+        delete[] edges_;
+        edges_ = nullptr;
+        num_edges_ = 0;
         return false;
       }
     } else {
       NODE_REF next = next_node_from_edge_rec(edges_[i]);
       if (next != 0 && static_cast<uint32_t>(next) >= num_edges_) {
         tprintf("Dawg edge %u has out-of-bounds next_node\n", i);
+        delete[] edges_;
+        edges_ = nullptr;
+        num_edges_ = 0;
         return false;
       }
     }