Project

Profile

Help

parseJsonFromFile gives segmentation fault

Added by Martin Honnen 4 months ago

I am still trying to get a handle on using the various parseJson functions that are supposed to be in SaxonC 12.4.1 in the C++/Python API.

As I was not able to make any progress on the Python API, I have tried the C++ API by now, with sample code like the following, using parseJsonFromFile:

// Test case on the evaluateSinge method in XPathProcessor,
// with a JSON file parsed into an XdmMap as the input
void testXPathSingleJSONFile(SaxonProcessor* processor, XPathProcessor* xpath,
    sResultCount* sresult) {

    cout << endl << "Test testXPathSingleJSONFile:" << endl;
    try {
        XdmValue* input = processor->parseJsonFromFile("file:/home/mh/libsaxon-HEC-linux-amd64-v12.4.1/samples/data/sample1.json");

        cout << endl <<  "parsed JSON XdmValue" << input->toString();

        xpath->setContextItem((XdmItem*)input);

        XdmItem* result = xpath->evaluateSingle("array { ?data?* => sort() }");
        const char* messagei = result->toString();
        cout << "Number of items=" << result->size() << endl;
        cout << "String representation of result=" << messagei << endl;
        sresult->success++;
        operator delete ((char*)messagei);
        delete result;
        delete input;
    }
    catch (SaxonApiException& e) {
        cout << "Error Message = " << e.what() << endl;
        sresult->failure++;
        sresult->failureList.push_back("testXPathSingleJSONString");
    }

    xpath->clearParameters();
    xpath->clearProperties();
}

However, this goes down (testing under Ubuntu WSL Linux) with a segmentation fault in the line XdmValue* input = processor->parseJsonFromFile("file:/home/mh/libsaxon-HEC-linux-amd64-v12.4.1/samples/data/sample1.json"); which the debugger shows happens in XdmValue.cpp XdmValue::addXdmItem:

Thread 1 "testXPath" received signal SIGSEGV, Segmentation fault.
0x0000555555576279 in XdmValue::addXdmItem (this=0x5555555abf10, val=0x5555555ac1b0) at ../../Saxon.C.API/XdmValue.cpp:174
174         values[xdmSize] = val;
(gdb) f
#0  0x0000555555576279 in XdmValue::addXdmItem (this=0x5555555abf10, val=0x5555555ac1b0) at ../../Saxon.C.API/XdmValue.cpp:174
174         values[xdmSize] = val;
(gdb) where
#0  0x0000555555576279 in XdmValue::addXdmItem (this=0x5555555abf10, val=0x5555555ac1b0) at ../../Saxon.C.API/XdmValue.cpp:174
#1  0x00005555555759d3 in XdmValue::XdmValue (this=0x5555555abf10, valRef=6) at ../../Saxon.C.API/XdmValue.cpp:61
#2  0x000055555555ee86 in SaxonProcessor::parseJsonFromFile (this=0x5555555aaf40,
    source=0x555555587278 "file:/home/mh/libsaxon-HEC-linux-amd64-v12.4.1/samples/data/sample1.json") at ../../Saxon.C.API/SaxonProcessor.cpp:816
#3  0x000055555558068c in testXPathSingleJSONFile (processor=0x5555555aaf40, xpath=0x5555555abd10, sresult=0x5555555ab900) at testXPath.cpp:47
#4  0x0000555555583a21 in main () at testXPath.cpp:562

Am I doing anything wrong or is that code path in SaxonC not yet worked out?


Replies (2)

RE: parseJsonFromFile gives segmentation fault - Added by Martin Honnen 4 months ago

I have now also tried parseJsonFromString in e.g. code like

// Test case on the evaluateSinge method in XPathProcessor,
// with a JSON string parsed into an XdmMap as the input
void testXPathSingleJSONString(SaxonProcessor* processor, XPathProcessor* xpath,
    sResultCount* sresult) {

    cout << endl << "Test testXPathSingleJSONString:" << endl;
    try {
        XdmValue *input = processor->parseJsonFromString("{ \"test\" : [3, 2, 1] }");

        cout << endl <<  "parsed JSON XdmValue" << input->toString();

        xpath->setContextItem((XdmItem*)input);

        XdmItem* result = xpath->evaluateSingle("array { ?test?* => sort() }");
        const char* messagei = result->toString();
        cout << "Number of items=" << result->size() << endl;
        cout << "String representation of result=" << messagei << endl;
        sresult->success++;
        operator delete ((char*)messagei);
        delete result;
        delete input;
    }
    catch (SaxonApiException& e) {
        cout << "Error Message = " << e.what() << endl;
        sresult->failure++;
        sresult->failureList.push_back("testXPathSingleJSONString");
    }

    xpath->clearParameters();
    xpath->clearProperties();
}

but (still under Ubuntu 22 WSL) I also get a segmentation fail; in gdb it shows as e.g.

Test testXPathSingleJSONString:

Thread 1 "testXPath" received signal SIGSEGV, Segmentation fault.
0x0000555555576279 in XdmValue::addXdmItem (this=0x5555555abf10, val=0x5555555ac1b0) at ../../Saxon.C.API/XdmValue.cpp:174

with the full stack showing e.g.

#0  0x0000555555576279 in XdmValue::addXdmItem (this=0x5555555abf10, val=0x5555555ac1b0) at ../../Saxon.C.API/XdmValue.cpp:174
#1  0x00005555555759d3 in XdmValue::XdmValue (this=0x5555555abf10, valRef=6) at ../../Saxon.C.API/XdmValue.cpp:61
#2  0x000055555555ed0c in SaxonProcessor::parseJsonFromString (this=0x5555555aaf40, source=0x555555587358 "{ \"test\" : [3, 2, 1] }", encoding=0x0)
    at ../../Saxon.C.API/SaxonProcessor.cpp:796
#3  0x00005555555809fa in testXPathSingleJSONString (processor=0x5555555aaf40, xpath=0x5555555abd10, sresult=0x5555555ab900) at testXPath.cpp:79
#4  0x0000555555583a21 in main () at testXPath.cpp:567

RE: parseJsonFromFile gives segmentation fault - Added by O'Neil Delpratt 4 months ago

Thanks for reporting this problem. Bug issue created and here #6294. The fix has been applied. The XdmValue constructor (i.e. XdmValue(int64_t valRef)) is missing a call on the initialize().

    (1-2/2)

    Please register to reply