From 8d4d35c604f0141064d34d008f9fc233077d767a Mon Sep 17 00:00:00 2001 From: shivanayak Date: Sun, 15 Mar 2026 15:25:02 +0530 Subject: [PATCH] ocaml: fix memory leak in guestfs_int_ocaml_strings_val on strdup failure (#324) When strdup() fails partway through the loop, caml_raise_out_of_memory() longjmps without freeing the previously allocated strings or the array. Free all prior allocations before raising the exception. Co-authored-by: Claude --- ocaml/guestfs-c.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ocaml/guestfs-c.c b/ocaml/guestfs-c.c index 7189aebbe..87c276d91 100644 --- a/ocaml/guestfs-c.c +++ b/ocaml/guestfs-c.c @@ -193,7 +193,13 @@ guestfs_int_ocaml_strings_val (guestfs_h *g, value sv) if (r == NULL) caml_raise_out_of_memory (); for (i = 0; i < Wosize_val (sv); ++i) { r[i] = strdup (String_val (Field (sv, i))); - if (r[i] == NULL) caml_raise_out_of_memory (); + if (r[i] == NULL) { + size_t j; + for (j = 0; j < i; ++j) + free (r[j]); + free (r); + caml_raise_out_of_memory (); + } } r[i] = NULL;