volgroup = (*env)->GetStringUTFChars (env, jvolgroup, NULL);
physvols_len = (*env)->GetArrayLength (env, jphysvols);
- physvols = malloc (sizeof (char *) * (physvols_len+1));
+ physvols = guestfs_safe_malloc (g, sizeof (char *) * (physvols_len+1));
for (i = 0; i < physvols_len; ++i) {
jobject o = (*env)->GetObjectArrayElement (env, jphysvols, i);
physvols[i] = (*env)->GetStringUTFChars (env, o, NULL);
heads = jheads;
sectors = jsectors;
lines_len = (*env)->GetArrayLength (env, jlines);
- lines = malloc (sizeof (char *) * (lines_len+1));
+ lines = guestfs_safe_malloc (g, sizeof (char *) * (lines_len+1));
for (i = 0; i < lines_len; ++i) {
jobject o = (*env)->GetObjectArrayElement (env, jlines, i);
lines[i] = (*env)->GetStringUTFChars (env, o, NULL);
int i;
arguments_len = (*env)->GetArrayLength (env, jarguments);
- arguments = malloc (sizeof (char *) * (arguments_len+1));
+ arguments = guestfs_safe_malloc (g, sizeof (char *) * (arguments_len+1));
for (i = 0; i < arguments_len; ++i) {
jobject o = (*env)->GetObjectArrayElement (env, jarguments, i);
arguments[i] = (*env)->GetStringUTFChars (env, o, NULL);
int i;
arguments_len = (*env)->GetArrayLength (env, jarguments);
- arguments = malloc (sizeof (char *) * (arguments_len+1));
+ arguments = guestfs_safe_malloc (g, sizeof (char *) * (arguments_len+1));
for (i = 0; i < arguments_len; ++i) {
jobject o = (*env)->GetObjectArrayElement (env, jarguments, i);
arguments[i] = (*env)->GetStringUTFChars (env, o, NULL);
subcmd = (*env)->GetStringUTFChars (env, jsubcmd, NULL);
extraargs_len = (*env)->GetArrayLength (env, jextraargs);
- extraargs = malloc (sizeof (char *) * (extraargs_len+1));
+ extraargs = guestfs_safe_malloc (g, sizeof (char *) * (extraargs_len+1));
for (i = 0; i < extraargs_len; ++i) {
jobject o = (*env)->GetObjectArrayElement (env, jextraargs, i);
extraargs[i] = (*env)->GetStringUTFChars (env, o, NULL);
CAMLreturn (Val_unit);
}
-/* Copy string array value. */
+/* Copy string array value.
+ * The return value is only 'safe' provided we don't allocate anything
+ * further on the OCaml heap (ie. cannot trigger the OCaml GC) because
+ * that could move the strings around.
+ */
char **
-ocaml_guestfs_strings_val (value sv)
+ocaml_guestfs_strings_val (guestfs_h *g, value sv)
{
CAMLparam1 (sv);
char **r;
int i;
- r = malloc (sizeof (char *) * (Wosize_val (sv) + 1));
+ r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
for (i = 0; i < Wosize_val (sv); ++i)
r[i] = String_val (Field (sv, i));
r[i] = NULL;
#define Guestfs_val(v) (*((guestfs_h **)Data_custom_val(v)))
extern void ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
Noreturn;
-extern char **ocaml_guestfs_strings_val (value sv);
+extern char **ocaml_guestfs_strings_val (guestfs_h *g, value sv);
extern void ocaml_guestfs_free_strings (char **r);
#endif /* GUESTFS_OCAML_C_H */
caml_failwith ("vgcreate: used handle after closing it");
const char *volgroup = String_val (volgroupv);
- char **physvols = ocaml_guestfs_strings_val (physvolsv);
+ char **physvols = ocaml_guestfs_strings_val (g, physvolsv);
int r;
caml_enter_blocking_section ();
int cyls = Int_val (cylsv);
int heads = Int_val (headsv);
int sectors = Int_val (sectorsv);
- char **lines = ocaml_guestfs_strings_val (linesv);
+ char **lines = ocaml_guestfs_strings_val (g, linesv);
int r;
caml_enter_blocking_section ();
if (g == NULL)
caml_failwith ("command: used handle after closing it");
- char **arguments = ocaml_guestfs_strings_val (argumentsv);
+ char **arguments = ocaml_guestfs_strings_val (g, argumentsv);
char *r;
caml_enter_blocking_section ();
if (g == NULL)
caml_failwith ("command_lines: used handle after closing it");
- char **arguments = ocaml_guestfs_strings_val (argumentsv);
+ char **arguments = ocaml_guestfs_strings_val (g, argumentsv);
int i;
char **r;
caml_failwith ("debug: used handle after closing it");
const char *subcmd = String_val (subcmdv);
- char **extraargs = ocaml_guestfs_strings_val (extraargsv);
+ char **extraargs = ocaml_guestfs_strings_val (g, extraargsv);
char *r;
caml_enter_blocking_section ();
AV *av;
I32 i;
- if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) {
+ if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
croak ("array reference expected");
- }
av = (AV *)SvRV (arg);
- ret = (char **)malloc (av_len (av) + 1 + 1);
+ ret = malloc (av_len (av) + 1 + 1);
+ if (!ret)
+ croak ("malloc failed");
for (i = 0; i <= av_len (av); i++) {
SV **elem = av_fetch (av, i, 0);
char **physvols; {
int i, len;
len = RARRAY_LEN (physvolsv);
- physvols = malloc (sizeof (char *) * (len+1));
+ physvols = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
for (i = 0; i < len; ++i) {
VALUE v = rb_ary_entry (physvolsv, i);
physvols[i] = StringValueCStr (v);
char **lines; {
int i, len;
len = RARRAY_LEN (linesv);
- lines = malloc (sizeof (char *) * (len+1));
+ lines = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
for (i = 0; i < len; ++i) {
VALUE v = rb_ary_entry (linesv, i);
lines[i] = StringValueCStr (v);
char **arguments; {
int i, len;
len = RARRAY_LEN (argumentsv);
- arguments = malloc (sizeof (char *) * (len+1));
+ arguments = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
for (i = 0; i < len; ++i) {
VALUE v = rb_ary_entry (argumentsv, i);
arguments[i] = StringValueCStr (v);
char **arguments; {
int i, len;
len = RARRAY_LEN (argumentsv);
- arguments = malloc (sizeof (char *) * (len+1));
+ arguments = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
for (i = 0; i < len; ++i) {
VALUE v = rb_ary_entry (argumentsv, i);
arguments[i] = StringValueCStr (v);
char **extraargs; {
int i, len;
len = RARRAY_LEN (extraargsv);
- extraargs = malloc (sizeof (char *) * (len+1));
+ extraargs = guestfs_safe_malloc (g, sizeof (char *) * (len+1));
for (i = 0; i < len; ++i) {
VALUE v = rb_ary_entry (extraargsv, i);
extraargs[i] = StringValueCStr (v);
pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
n n
| StringList n ->
- pr " char **%s = ocaml_guestfs_strings_val (%sv);\n" n n
+ pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
| Bool n ->
pr " int %s = Bool_val (%sv);\n" n n
| Int n ->
AV *av;
I32 i;
- if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) {
+ if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
croak (\"array reference expected\");
- }
av = (AV *)SvRV (arg);
- ret = (char **)malloc (av_len (av) + 1 + 1);
+ ret = malloc (av_len (av) + 1 + 1);
+ if (!ret)
+ croak (\"malloc failed\");
for (i = 0; i <= av_len (av); i++) {
SV **elem = av_fetch (av, i, 0);
pr " {\n";
pr " int i, len;\n";
pr " len = RARRAY_LEN (%sv);\n" n;
- pr " %s = malloc (sizeof (char *) * (len+1));\n" n;
+ pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
+ n;
pr " for (i = 0; i < len; ++i) {\n";
pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
pr " %s[i] = StringValueCStr (v);\n" n;
pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
| StringList n ->
pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
- pr " %s = malloc (sizeof (char *) * (%s_len+1));\n" n n;
+ pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
pr " for (i = 0; i < %s_len; ++i) {\n" n;
pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
n;