Crystal: Invalid memory access with libxml2 2.9.9

Created on 22 Feb 2019  路  3Comments  路  Source: crystal-lang/crystal

The stdlib XML specs fail with the following error when linked against libxml2 2.9.9:

bin/crystal run --debug spec/std/xml/reader_spec.cr -- -e "#read_outer_xml"
Invalid memory access (signal 11) at address 0x40
[0xaab2f9c880e0] *CallStack::print_backtrace:Int32 +100
[0xaab2f969fb74] __crystal_sigfault_handler +228
[0xfff613bbe964] __setjmp +56
[0xfff613a33170] xmlTextReaderReadOuterXml +20
[0xaab2facf9dbc] *XML::Reader#read_outer_xml:String +24
[0xaab2f9c43b90] ~procProc(Nil) +44
[0xaab2f96c241c] *it<String, String, Int32, Int32, &Proc(Nil)>:(Array(Spec::Result) | Nil) +580
[0xaab2f9c43b54] ~procProc(Nil) +72
[0xaab2f9e1a3a0] *Spec::RootContext::describe<String, String, Int32, &Proc(Nil)>:Spec::Context+ +316
[0xaab2f96c7834] *describe<String, String, Int32, &Proc(Nil)>:Spec::Context+ +8
[0xaab2f9c3d420] ~procProc(Nil) +1024
[0xaab2f9e1a3a0] *Spec::RootContext::describe<String, String, Int32, &Proc(Nil)>:Spec::Context+ +316
[0xaab2f9c44644] *describe<XML::Reader.class, String, Int32, &Proc(Nil)>:Spec::Context+ +48
[0xaab2f968623c] __crystal_main +37812
[0xaab2fa6f3984] *Crystal::main_user_code<Int32, Pointer(Pointer(UInt8))>:Nil +8
[0xaab2fa6f38dc] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +40
[0xaab2f968d624] main +8
[0xfff613b93734] ???
make: *** [Makefile:86: spec] Error 11
>>> ERROR: crystal: check failed

Tested on Alpine edge with Crystal 0.27.2

With 2.9.8. everything seems to work fine. LibXML2 changelog: https://gitlab.gnome.org/GNOME/libxml2/blob/a7fe7ee45938c53a8dd028dd40baa461191a2fd2/NEWS

This issue was first reported by mps on Gitter and I've been able to reproduce it.

Most helpful comment

I filed an issue with libxml2: https://gitlab.gnome.org/GNOME/libxml2/issues/43

All 3 comments

The error also reproduces in a plain C implementation of the spec:

#include <stdio.h>
#include <libxml/xmlreader.h>

int main(int argc, char **argv) {
  xmlTextReaderPtr reader;

  reader = xmlReaderForMemory("<root>\n<child/>\n</root>\n", 23, NULL, NULL, 0);
  printf("%s", xmlTextReaderReadOuterXml(reader));
  xmlTextReaderRead(reader);
  printf("%s", xmlTextReaderReadOuterXml(reader));
}

So either we're using the API wrong or this is a libxml2 issue.

The only change to xmlTextReaderReadOuterXML between 2.9.8 and 2.9.9 was this one: https://gitlab.gnome.org/GNOME/libxml2/commit/39fbfb4fd08eae88d4b0c15f3a8ac33babc740e6

I filed an issue with libxml2: https://gitlab.gnome.org/GNOME/libxml2/issues/43

It looks like a bug on their side.

However, on our side we can probably check the node type before invoking that C function. I think the outer XML is only available on some node types. I might give it a shot.

Was this page helpful?
0 / 5 - 0 ratings