Browse Source

Merge pull request #2540 from mduan/handle-broken-hex-string

Handle broken hex string
Brendan Dahl 12 years ago
parent
commit
e978ea2617
  1. 42
      src/parser.js
  2. 11
      test/unit/parser_spec.js

42
src/parser.js

@ -460,28 +460,34 @@ var Lexer = (function LexerClosure() {
getHexString: function Lexer_getHexString(ch) { getHexString: function Lexer_getHexString(ch) {
var str = ''; var str = '';
var stream = this.stream; var stream = this.stream;
for (;;) { var isFirstHex = true;
var firstDigit;
var secondDigit;
while (true) {
ch = stream.getChar(); ch = stream.getChar();
if (ch == '>') {
break;
}
if (!ch) { if (!ch) {
warn('Unterminated hex string'); warn('Unterminated hex string');
break; break;
} } else if (ch === '>') {
if (specialChars[ch.charCodeAt(0)] != 1) { break;
var x, x2; } else if (specialChars[ch.charCodeAt(0)] === 1) {
if ((x = toHexDigit(ch)) == -1) continue;
error('Illegal character in hex string: ' + ch); } else {
if (isFirstHex) {
ch = stream.getChar(); firstDigit = toHexDigit(ch);
while (specialChars[ch.charCodeAt(0)] == 1) if (firstDigit === -1) {
ch = stream.getChar(); warn("Ignoring invalid character '" + ch + "' in hex string");
continue;
if ((x2 = toHexDigit(ch)) == -1) }
error('Illegal character in hex string: ' + ch); } else {
secondDigit = toHexDigit(ch);
str += String.fromCharCode((x << 4) | x2); if (secondDigit === -1) {
warn("Ignoring invalid character '" + ch + "' in hex string");
continue;
}
str += String.fromCharCode((firstDigit << 4) | secondDigit);
}
isFirstHex = !isFirstHex;
} }
} }
return str; return str;

11
test/unit/parser_spec.js

@ -12,6 +12,17 @@ describe('parser', function() {
expect(result).toEqual(11.234); expect(result).toEqual(11.234);
}); });
it('should not throw exception on bad input', function() {
// '8 0 2 15 5 2 2 2 4 3 2 4'
// should be parsed as
// '80 21 55 22 24 32'
var input = new StringStream('7 0 2 15 5 2 2 2 4 3 2 4>');
var lexer = new Lexer(input);
var result = lexer.getHexString('<');
expect(result).toEqual('p!U"$2');
});
}); });
}); });

Loading…
Cancel
Save