Skip to content

Commit 0bd9667

Browse files
committed
Fixed infinite loop issues and added some improved logging.
1 parent 0c467d5 commit 0bd9667

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/libsyntax/codemap.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -638,25 +638,33 @@ impl CodeMap {
638638
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
639639
// Disregard malformed spans and assume a one-byte wide character.
640640
if sp.lo() >= sp.hi() {
641+
debug!("find_width_of_character_at_span: early return malformed span");
641642
return 1;
642643
}
643644

644645
let local_begin = self.lookup_byte_offset(sp.lo());
645646
let local_end = self.lookup_byte_offset(sp.hi());
647+
debug!("find_width_of_character_at_span: local_begin=`{:?}`, local_end=`{:?}`",
648+
local_begin, local_end);
646649

647650
let start_index = local_begin.pos.to_usize();
648651
let end_index = local_end.pos.to_usize();
652+
debug!("find_width_of_character_at_span: start_index=`{:?}`, end_index=`{:?}`",
653+
start_index, end_index);
649654

650655
// Disregard indexes that are at the start or end of their spans, they can't fit bigger
651656
// characters.
652657
if (!forwards && end_index == usize::min_value()) ||
653658
(forwards && start_index == usize::max_value()) {
659+
debug!("find_width_of_character_at_span: start or end of span, cannot be multibyte");
654660
return 1;
655661
}
656662

657663
let source_len = (local_begin.fm.end_pos - local_begin.fm.start_pos).to_usize();
664+
debug!("find_width_of_character_at_span: source_len=`{:?}`", source_len);
658665
// Ensure indexes are also not malformed.
659666
if start_index > end_index || end_index > source_len {
667+
debug!("find_width_of_character_at_span: source indexes are malformed");
660668
return 1;
661669
}
662670

@@ -671,16 +679,22 @@ impl CodeMap {
671679
} else {
672680
return 1;
673681
};
674-
debug!("DTW start {:?} end {:?}", start_index, end_index);
675-
debug!("DTW snippet {:?}", snippet);
682+
debug!("find_width_of_character_at_span: snippet=`{:?}`", snippet);
683+
684+
let file_start_pos = local_begin.fm.start_pos.to_usize();
685+
let file_end_pos = local_begin.fm.end_pos.to_usize();
686+
debug!("find_width_of_character_at_span: file_start_pos=`{:?}` file_end_pos=`{:?}`",
687+
file_start_pos, file_end_pos);
676688

677689
let mut target = if forwards { end_index + 1 } else { end_index - 1 };
678-
debug!("DTW initial target {:?}", target);
679-
while !snippet.is_char_boundary(target - start_index) {
690+
debug!("find_width_of_character_at_span: initial target=`{:?}`", target);
691+
692+
while !snippet.is_char_boundary(target - start_index)
693+
&& target >= file_start_pos && target <= file_end_pos {
680694
target = if forwards { target + 1 } else { target - 1 };
681-
debug!("DTW update target {:?}", target);
695+
debug!("find_width_of_character_at_span: target=`{:?}`", target);
682696
}
683-
debug!("DTW final target {:?}", target);
697+
debug!("find_width_of_character_at_span: final target=`{:?}`", target);
684698

685699
if forwards {
686700
(target - end_index) as u32

0 commit comments

Comments
 (0)