Skip to content

Commit

Permalink
tools/ocaml: Fix stubs build when OCaml has been compiled with -safe-…
Browse files Browse the repository at this point in the history
…string

The OCaml code has been fixed to handle properly -safe-string in Xen
4.11, however the stubs part were missed.

On OCaml newer than 4.06.1, String_Val() will return a const char *
when using -safe-string leading to build failure when this is used
in place where char * is expected.

The main use in Xen code base is when a new string is allocated. The
suggested approach by the OCaml community [1] is to use the helper
caml_alloc_initialized_string() but it was introduced by OCaml 4.06.1.

The next best approach is to cast String_val() to (char *) as the helper
would have done. So use it when we need to update the new string using
memcpy().

Take the opportunity to remove the unnecessary cast of the source as
mempcy() is expecting a void *.

[1] ocaml/ocaml#1274

Reported-by: Dario Faggioli <[email protected]>
Signed-off-by: Julien Grall <[email protected]>
Acked-by: Christian Lindig <[email protected]>
  • Loading branch information
Julien Grall committed Apr 20, 2020
1 parent 7868643 commit 59b087e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tools/ocaml/libs/xb/xenbus_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CAMLprim value stub_string_of_header(value tid, value rid, value ty, value len)
};

ret = caml_alloc_string(sizeof(struct xsd_sockmsg));
memcpy(String_val(ret), &xsd, sizeof(struct xsd_sockmsg));
memcpy((char *) String_val(ret), &xsd, sizeof(struct xsd_sockmsg));

CAMLreturn(ret);
}
4 changes: 2 additions & 2 deletions tools/ocaml/libs/xc/xenctrl_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ CAMLprim value stub_xc_vcpu_context_get(value xch, value domid,
failwith_xc(_H(xch));

context = caml_alloc_string(sizeof(ctxt));
memcpy(String_val(context), (char *) &ctxt.c, sizeof(ctxt.c));
memcpy((char *) String_val(context), &ctxt.c, sizeof(ctxt.c));

CAMLreturn(context);
}
Expand Down Expand Up @@ -680,7 +680,7 @@ CAMLprim value stub_xc_readconsolering(value xch)
conring_size = size;

ring = caml_alloc_string(count);
memcpy(String_val(ring), str, count);
memcpy((char *) String_val(ring), str, count);
free(str);

CAMLreturn(ring);
Expand Down

0 comments on commit 59b087e

Please sign in to comment.