@@ -1144,7 +1144,7 @@ static cmark_node *handle_close_bracket(cmark_parser *parser, subject *subj) {
1144
1144
// look back to the opening '[', and skip ahead to the next character
1145
1145
// if we're looking at a '[^' sequence, and there is other text or nodes
1146
1146
// 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 )) {
1148
1148
1149
1149
// Before we got this far, the `handle_close_bracket` function may have
1150
1150
// 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) {
1168
1168
//
1169
1169
// this copies the footnote reference string, even if between the
1170
1170
// `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
+ }
1172
1178
1173
1179
fnref -> start_line = fnref -> end_line = subj -> line ;
1174
1180
fnref -> start_column = fnref_start_column ;
0 commit comments