[Libguestfs] Segmentation fault when trying to add binding

Richard W.M. Jones rjones at redhat.com
Tue Aug 26 07:33:05 UTC 2014


On Tue, Aug 26, 2014 at 09:34:42AM +0300, Shahar Havivi wrote:
> Hi,
> 
> I am trying to add xmlXPathRegisterNs() to v2v/xml-c.c but I get a seg fault,
> the signature should be:
> http://xmlsoft.org/html/libxml-xpathInternals.html#xmlXPathRegisterNs
> 
> I think I am wrong in CAMLparam and CAMLlocal...,
> Following is the patch:
> 
> ===================================================================================
> diff --git a/v2v/xml-c.c b/v2v/xml-c.c
> index 4c9bc77..a917c24 100644
> --- a/v2v/xml-c.c
> +++ b/v2v/xml-c.c
> @@ -141,6 +141,16 @@ v2v_xml_xpath_new_context (value docv)
>  }
>  
>  value
> +v2v_xml_xpath_register_ns (value prefix, value uri, value xpathctx)
> +{
> +  CAMLparam3 (prefix, uri, xpathctx);
> +  CAMLlocal1 (retval);
> +  retval = xmlXPathRegisterNs (BAD_CAST String_val (prefix), BAD_CAST String_val (uri), xpathctx);
> +
> +  CAMLreturn (retval);

An 'int' isn't a 'value', so this will not work and likely segfault.
For the representation of 'value' see:

  http://rwmj.wordpress.com/2009/08/04/ocaml-internals/#content
  (and follow up articles).

In any case, here you need to turn the return value into an exception,
ie something like this untested code:

  value
  v2v_xml_xpath_register_ns (value prefix, value uri, value xpathctx)
  {
    CAMLparam3 (prefix, uri, xpathctx);
    int r;

    r = xmlXPathRegisterNs (...);
    if (r == -1)
      caml_invalid_argument ("some suitable error message here ...");

    /* return unit */
    CAMLreturn (Val_unit);
  }

> +external xpath_register_ns : string -> string -> xpathctx -> int = "v2v_xml_xpath_register_ns"

Needs to return unit, not int.  If the libxml2 function returns -1
then this will throw an exception (Invalid_arg).

> +val xpath_register_ns : string -> string -> xpathctx -> int
> +(** xmlXPathRegisterNs *)

Same here.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/




More information about the Libguestfs mailing list