@@ -751,11 +751,11 @@ static int link_label(subject *subj, cmark_chunk *raw_label) {
751751
752752// Return a link, an image, or a literal close bracket.
753753static cmark_node * handle_close_bracket (subject * subj ) {
754- bufsize_t initial_pos ;
754+ bufsize_t initial_pos , save_pos ;
755755 bufsize_t starturl , endurl , starttitle , endtitle , endall ;
756756 bufsize_t n ;
757757 bufsize_t sps ;
758- cmark_reference * ref ;
758+ cmark_reference * ref = NULL ;
759759 bool is_image = false;
760760 cmark_chunk url_chunk , title_chunk ;
761761 cmark_chunk url , title ;
@@ -830,24 +830,29 @@ static cmark_node *handle_close_bracket(subject *subj) {
830830 // skip spaces
831831 raw_label = cmark_chunk_literal ("" );
832832 found_label = link_label (subj , & raw_label );
833- if (!found_label || raw_label .len == 0 ) {
834- cmark_chunk_free (subj -> mem , & raw_label );
835- raw_label = cmark_chunk_dup (& subj -> input , opener -> position ,
836- initial_pos - opener -> position - 1 );
837- }
838833
839834 if (!found_label ) {
840835 // If we have a shortcut reference link, back up
841836 // to before the spacse we skipped.
842837 subj -> pos = initial_pos ;
843838 }
844839
845- ref = cmark_reference_lookup (subj -> refmap , & raw_label );
846- cmark_chunk_free (subj -> mem , & raw_label );
840+ if (!found_label || raw_label .len == 0 ) {
841+ save_pos = subj -> pos ;
842+ subj -> pos = opener -> position - 1 ;
843+ cmark_chunk_free (subj -> mem , & raw_label );
844+ found_label = link_label (subj , & raw_label );
845+ subj -> pos = save_pos ;
846+ }
847+
848+ if (found_label ) {
849+ ref = cmark_reference_lookup (subj -> refmap , & raw_label );
850+ }
847851
848- if (ref != NULL ) { // found
852+ if (ref ) {
849853 url = chunk_clone (subj -> mem , & ref -> url );
850854 title = chunk_clone (subj -> mem , & ref -> title );
855+ cmark_chunk_free (subj -> mem , & raw_label );
851856 goto match ;
852857 } else {
853858 goto noMatch ;
0 commit comments