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 <noreply@anthropic.com>
This commit is contained in:
shivanayak
2026-03-15 15:25:02 +05:30
committed by GitHub
parent 92c2e1e79e
commit 8d4d35c604

View File

@@ -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;