[xslt] xsltParseStylesheetDoc



Hi List

When I do the following, i am getting a segfault.....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/xmlIO.h>
#include <libxml/DOCBparser.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxml/parser.h>

#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

extern int xmlLoadExtDtdDefaultValue;

char *readToBuffer(char *filename) {
        long length;
        int count;
        char *buf;
        FILE *f = fopen(filename, "rt");
        if (!f) {
                fprintf(stderr, "Error: Cannot open file %s/n", filename);
                exit(EXIT_FAILURE);
        }
        length = 0xffff;
        buf = (char*) malloc(length+1);
        count = fread(buf, 1, length+1, f);
        fclose(f);
        if (count > length) {
                fprintf(stderr, "Error: file too large (max 64k)\n");
                exit(EXIT_FAILURE);
        }
        buf[count] = 0;
        return buf;
}

int main(int argc, char *argv[]) {
        char *sheetBuf, *inputBuf;

        xsltStylesheetPtr cur = NULL;
        xmlDocPtr sheet, input, result;
        if (argc != 3) {
                fprintf(stderr, "Error: %d arguments supplied\n", argc -
1);
                fprintf(stderr, "Usage: %s xslfile xmlfile\n", argv[0]);
                exit(EXIT_FAILURE);
        }
        sheetBuf = readToBuffer(argv[1]);
        inputBuf = readToBuffer(argv[2]);
        xmlSubstituteEntitiesDefault(1);
        xmlLoadExtDtdDefaultValue = 1;
        sheet = xmlParseMemory(sheetBuf, 0xffff);
        input = xmlParseMemory(inputBuf, 0xffff);
        cur = xsltParseStylesheetDoc(sheet);
        result = xsltApplyStylesheet(cur, input, NULL);
        xsltSaveResultToFile(stdout, result, cur);
        printf("\nFinished Processing....\n\n");
        xsltFreeStylesheet(cur);
        printf("\nFreed Stylesheet....\n\n");
        xmlFreeDoc(result);
        printf("\nFreed result....\n\n");
        xmlFreeDoc(sheet);                      /*  <------ segfault
happening here ------<    */
        printf("\nFreed sheet....\n\n");
        xmlFreeDoc(input);
        printf("\nFreed input....\n\n");
        free(sheetBuf);
        free(inputBuf);
        exit(EXIT_SUCCESS);
}

What am I doing wrong?

my guess is that xsltParseStylesheetDoc is setting cur to the same address
as sheet
and the call to xsltFreeStylesheet(cur) is freeing the memory
allocated by sheet = xmlParseMemory(sheetBuf, 0xffff);

anyway, it's not documented so I thaught I'd check......

Thanks,

Darrin Wortlehock.









[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]