Re: [xml] Change from 1.8.2 to 1.8.7 causes major failure [long]

Date view Thread view Subject view Author view

From: Byron Ellacott (bje@apnic.net)
Date: Wed Apr 05 2000 - 20:09:17 EDT


On Wed, 5 Apr 2000, Daniel Veillard wrote:

Thanks for the quick response.

> Ok I think I know the problem:
[null terminate string]

> So basically the answer is:
> add an instruction buffer[size] = 0 before calling xmlParseMemory()

I had read that in the list archives or the bug report page, I forget
which, and was using the following code:

    stat(filename, &statbuf);
    fsize = statbuf.st_size - ftell(f);

    if ((buffer = malloc(fsize+1)) == NULL) {
        ...
    }

    if (fread(buffer, fsize, 1, f) != 1) {
        ...
    }

    fclose(f);

    buffer[fsize] = '\0';
    doc = xmlParseMemory(buffer, fsize+1);

    free(buffer);

    if (!doc) return 0;

Close inspection of parser.c reveals xmlCreateMemoryParserCtxt() is doing
the following:

    if (buffer[size] != '\0')

This strikes me as confusing, since it means the size argument I pass to
xmlParseMemory is the size of the buffer, *minus the null character*.

For example, if I was parsing 3 characters, I'd have a memory array:

char buffer[] = {'H', 'i', '!'}

where sizeof(buffer) == 3. If I add a null:

char buffer[] = {'H', 'i', '!', '\0'}

I get buffer[sizeof(buffer)] being undetermined, since it's off the end of
the buffer, and so I cannot call xmlParseMemory(buffer, sizeof(buffer)) as
would seem intuitive, but rather, I must call
xmlParseMemory(buffer, sizeof(buffer) - 1).

The simple fix for me was to pass fsize instead of fsize+1 to
xmlParseMemory, but this means I'm no longer passing the actual size of
the buffer, but rather the size of the buffer minus one.

This also explains why the parser would have been succeeding first time,
but not subsequent times, since memory would have been uninitialised in
the first pass, and the character one past the null character I'm adding
would have been null too.

Is this a logic error, or deliberate?

> > [0] Why isn't there an xmlParseStream(FILE *input) method? I'm sure I'm
> This could be implemented in less than 10 lines of code using
> existing interface, there is actually such code in parser.c / xmllint.c
> in parseAndPrintFile(char *filename)

It's effectively what I've implemented, too, but it would be nice not to
require everyone who wishes to do this to implement the same code. Are
there any plans to add such a function to the API?

--
bje

---- Message from the list xml@xmlsoft.org Archived at : http://xmlsoft.org/messages/ to unsubscribe: echo "unsubscribe xml" | mail majordomo@xmlsoft.org


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Aug 02 2000 - 12:30:10 EDT