From dc62959553f59a0a88dcc803eeac6d0eb6a2b77b Mon Sep 17 00:00:00 2001 From: Richard Cohen Date: Mon, 15 May 2023 18:58:10 +0100 Subject: [PATCH] Fix crash in test-engine on Arch Thanks, Valgrind: ==515314== Invalid read of size 8 ==515314== at 0x4ED46F3: gncInvoiceRemoveEntries (gncInvoice.c:767) ==515314== by 0x142B35: teardown_with_invoice (utest-Invoice.c:274) ... ==515314== Address 0x8557b98 is 8 bytes inside a block of size 24 free'd ==515314== at 0x484620F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==515314== by 0x51B565D: g_list_remove (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.7600.1) ==515314== by 0x4ED42EF: gncInvoiceRemoveEntry (gncInvoice.c:688) ==515314== by 0x4ED46A2: gncInvoiceRemoveEntries (gncInvoice.c:781) ==515314== by 0x142B35: teardown_with_invoice (utest-Invoice.c:274) ... ==515314== Block was alloc'd at ==515314== at 0x4843828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so) ==515314== by 0x51BD948: g_malloc (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.7600.1) ==515314== by 0x51B1CB9: ??? (in /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0.7600.1) ==515314== by 0x4ED4271: gncInvoiceAddEntry (gncInvoice.c:676) ==515314== by 0x142401: setup_with_invoice (utest-Invoice.c:142) ... ok 57 /engine/gncInvoice/post trans - vendor bill --- libgnucash/engine/gncInvoice.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libgnucash/engine/gncInvoice.c b/libgnucash/engine/gncInvoice.c index f4ad3cb66b..fc5220795d 100644 --- a/libgnucash/engine/gncInvoice.c +++ b/libgnucash/engine/gncInvoice.c @@ -760,12 +760,13 @@ void gncInvoiceSortEntries (GncInvoice *invoice) void gncInvoiceRemoveEntries (GncInvoice *invoice) { - GList *node; - if (!invoice) return; - for (node = invoice->entries; node; node = node->next) + // gnc{Bill,Invoice}RemoveEntry free the "entry" node. + // Make sure to save "next" first. + for (GList *next, *node = invoice->entries; node; node = next) { + next = node->next; GncEntry *entry = node->data; switch (gncInvoiceGetOwnerType (invoice))