Skip to content

Commit

Permalink
Fix pathological case commonmark/cmark#178.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Aug 30, 2019
1 parent 903f35e commit 34b8f65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions lib/inlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,15 @@ var processEmphasis = function(stack_bottom) {
var use_delims;
var tmp, next;
var opener_found;
var openers_bottom = [];
var openers_bottom = [[],[],[]];
var odd_match = false;

openers_bottom[C_UNDERSCORE] = stack_bottom;
openers_bottom[C_ASTERISK] = stack_bottom;
openers_bottom[C_SINGLEQUOTE] = stack_bottom;
openers_bottom[C_DOUBLEQUOTE] = stack_bottom;

for (var i=0; i < 3; i++) {
openers_bottom[i][C_UNDERSCORE] = stack_bottom;
openers_bottom[i][C_ASTERISK] = stack_bottom;
openers_bottom[i][C_SINGLEQUOTE] = stack_bottom;
openers_bottom[i][C_DOUBLEQUOTE] = stack_bottom;
}
// find first closer above stack_bottom:
closer = this.delimiters;
while (closer !== null && closer.previous !== stack_bottom) {
Expand All @@ -369,7 +370,7 @@ var processEmphasis = function(stack_bottom) {
opener = closer.previous;
opener_found = false;
while (opener !== null && opener !== stack_bottom &&
opener !== openers_bottom[closercc]) {
opener !== openers_bottom[closer.origdelims % 3][closercc]) {
odd_match = (closer.can_open || opener.can_close) &&
closer.origdelims % 3 !== 0 &&
(opener.origdelims + closer.origdelims) % 3 === 0;
Expand Down Expand Up @@ -448,13 +449,13 @@ var processEmphasis = function(stack_bottom) {
closer = closer.next;

}
if (!opener_found && !odd_match) {
if (!opener_found) {
// Set lower bound for future searches for openers:
// We don't do this with odd_match because a **
// that doesn't match an earlier * might turn into
// an opener, and the * might be matched by something
// else.
openers_bottom[closercc] = old_closer.previous;
openers_bottom[old_closer.origdelims % 3][closercc] = old_closer.previous;
if (!old_closer.can_open) {
// We can remove a closer that can't be an opener,
// once we've seen there's no matching opener:
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ for (x = 1000; x <= 10000; x *= 10) {
for (x=1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' openers and closers multiple of 3',
input: "a**b" + repeat("c* ", 50000),
expected: '<p>a**b' + repeat('c* ', x) + '<p>' });
input: "a**b" + repeat("c* ", x),
expected: '<p>a**b' + repeat('c* ', x - 1) + 'c*</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
Expand Down

0 comments on commit 34b8f65

Please sign in to comment.