Re: [xml] [Bug 143739] - incorrect line numbers in well-formedness error message

R. Steven Rainwater said:
On Fri, 2004-07-02 at 04:01, Daniel Veillard wrote:
Now all error messages display the same, incorrect line number:

Well-formedness Error [line 65535]: Char 0xD96B out of allowed

 Do you use specific error reporting mechanism ?

I'm using a function called xmlTextReaderLocatorLineNumber(locator)
determine the line number.

Here's the entire libxml2-related code - just two small functions.
pretty minimal:

 * wfErrorFunc - used by isWellFormedXML to handle errors
void wfErrorFunc(void *arg, const char *msg, xmlParserSeverities
severity, xmlTextReaderLocatorPtr locator) {
  const char *err[5] = {
    "Validity Warning",
    "Validity Error",

  printf("Well-formedness %s [line %i]:

 * isWellFormedXML: Checks XML well-formedness acccording to the W3C
 * XML 1.0 definition.
 * Based on the streamFile function from xmllint.c, part of the
 * library by Daniel Veillard.
static int isWellFormedXML(char *filename) {
    xmlTextReaderPtr reader;
    int ret = 1;

    reader = xmlNewTextReaderFilename(filename);
    if (reader != NULL) {
        while (ret == 1) {
            ret = xmlTextReaderRead(reader);
    else {
        fprintf(stderr,"Error: unable to open [%s]\n",filename);
    if(ret != 0) return 0;
    return 1;

The entire source for the dumpcheck program is available here:


If you take a look at the source code for
xmlTextReaderLocatorLineNumber you can see where your trouble comes
from.  Right at the beginning it has

    if (ctx->node != NULL) {
        ret = xmlGetLineNo(ctx->node);
    else {

i.e., if the node info is available from the context, use that,
otherwise get the info from the file pointers.  There are good
reasons why the code does it that way (the routine is used for more
than error reporting).  Unfortunately, as already noted, the node
info has only 16-bits worth of line-number (and that gets set to
65535 once the number passes that magic point).

There are at least two solutions which I can see.  The first (and
cleanest) is to change your code to use the newer Structured Error
Handler.  The second, quicker (and a bit dirtier) is to code your
own Locator Line Number routine which always uses the file info,

int lineLoc(xmlTextReaderLocatorPtr locator) {
  xmlParserCtxtPtr ctx = (xmlParserCtxtPtr)locator;
  int ret;
  xmlParserInputPtr input;

  input = ctx->input;
  if ((input->filename == NULL) && (ctx->inputNr > 1))
      input = ctx->inputTab[ctx->inputNr - 2];
  if (input != NULL) {
      ret = input->line;
  } else
      ret = -1;
  return ret;

and call that instead of the system routine when you do the print.



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