@@ -1144,7 +1144,7 @@ static cmark_node *handle_close_bracket(cmark_parser *parser, subject *subj) {
11441144 // look back to the opening '[', and skip ahead to the next character
11451145 // if we're looking at a '[^' sequence, and there is other text or nodes
11461146 // after the ^, let's call it a footnote reference.
1147- if (literal -> data [0 ] == '^' && (literal -> len > 1 || opener -> inl_text -> next -> next )) {
1147+ if (( literal -> len > 0 && literal -> data [0 ] == '^' ) && (literal -> len > 1 || opener -> inl_text -> next -> next )) {
11481148
11491149 // Before we got this far, the `handle_close_bracket` function may have
11501150 // advanced the current state beyond our footnote's actual closing
@@ -1168,7 +1168,13 @@ static cmark_node *handle_close_bracket(cmark_parser *parser, subject *subj) {
11681168 //
11691169 // this copies the footnote reference string, even if between the
11701170 // `opener` and the subject's current position there are other nodes
1171- fnref -> as .literal = cmark_chunk_dup (literal , 1 , (fnref_end_column - fnref_start_column ) - 2 );
1171+ //
1172+ // (first, check for underflows)
1173+ if ((fnref_start_column + 2 ) <= fnref_end_column ) {
1174+ fnref -> as .literal = cmark_chunk_dup (literal , 1 , (fnref_end_column - fnref_start_column ) - 2 );
1175+ } else {
1176+ fnref -> as .literal = cmark_chunk_dup (literal , 1 , 0 );
1177+ }
11721178
11731179 fnref -> start_line = fnref -> end_line = subj -> line ;
11741180 fnref -> start_column = fnref_start_column ;
0 commit comments