]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Fix memory leak in rspamd_shingles_from_html
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 5 Oct 2025 16:01:48 +0000 (17:01 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sun, 5 Oct 2025 16:01:48 +0000 (17:01 +0100)
The struct_sgl object from generate_shingles_from_string_tokens() was only
deleted when pool == nullptr, causing memory leaks when a memory pool was
active. Now struct_sgl is always deleted after copying to res, regardless
of pool allocation method.

src/libutil/shingles_html.cxx

index 8792f3ee8464fdc5cb506c55d38d69c3d4cf8a6c..6a6e6cd806d34a99ed39a1683467e07a2295c876 100644 (file)
@@ -427,7 +427,7 @@ rspamd_shingles_from_html(void *html_content,
                return nullptr;
        }
 
-       /* Allocate result */
+       /* Allocate result after all early exit checks to avoid leaks */
        struct rspamd_html_shingle *res;
        if (pool) {
                res = static_cast<rspamd_html_shingle *>(rspamd_mempool_alloc0(pool, sizeof(*res)));
@@ -451,9 +451,8 @@ rspamd_shingles_from_html(void *html_content,
 
        if (struct_sgl) {
                std::memcpy(&res->structure_shingles, struct_sgl, sizeof(struct rspamd_shingle));
-               if (pool == nullptr) {
-                       delete struct_sgl;
-               }
+               /* Always delete struct_sgl after copying, regardless of pool */
+               delete struct_sgl;
        }
        else {
                std::memset(&res->structure_shingles, 0, sizeof(struct rspamd_shingle));