jindw / xmldom

A PURE JS W3C Standard based(XML DOM Level2 CORE) DOMParser and XMLSerializer.
Other
819 stars 265 forks source link

fix endless loop when 'unexpected end of input' #172

Closed talrasha007 closed 7 years ago

talrasha007 commented 8 years ago
var DOMParser = require('xmldom').DOMParser;

var str = '<s';
new DOMParser().parseFromString(str);

This test will run into an endless loop @

       // line 72 of sax.js
    function position(p,m){
        while(p>=lineEnd && (m = linePattern.exec(source))){
            lineStart = m.index;
            lineEnd = lineStart + m[0].length;
            // m.index === lineStart && m[0].length === 0
            // Nothing changes, so this loop will never end.
            locator.lineNumber++;
            //console.log('line++:',locator,startPos,endPos)
        }
        locator.columnNumber = p-lineStart+1;
    }

This pull request will fix this issue.

bhishp commented 7 years ago

This fix works for us. Without it our app stalls and becomes unresponsive, would be good to get included in the package.

Not looked at the code yet but it seems that errors or fatalErrors don't throw? We want to wrap parser.parseFromString(xmlCode, 'application/xml'); in a try/catch and handle errors..?

bhishp commented 7 years ago

Oh, just cloned latest GitHub version (0.1.24) and this is handling it correctly anyway, but seems that npmjs version is outdated, v0.1.22

jindw commented 7 years ago

thanks fixed in new version