mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
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:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user