diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-23 14:25:43 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-23 14:25:43 +0100 |
commit | 3e09dbba94de103d4d7a211ec578b049d0adc3c7 (patch) | |
tree | d52a0a75e2ffcc7d71481f2d452969522c76d91e /crates/ra_ide/src/syntax_highlighting/injection.rs | |
parent | 45f3a5f9c151e7728cda47ea20fa72b18927ca2b (diff) | |
parent | 0b971625c389c1638957b010a35c7bb1a6bd69b9 (diff) |
Merge #5002
5002: Fix underflow panic when doctests are at top of file r=Nashenas88 a=Nashenas88
While debugging a comment at the top of a test string, I discovered that the offset calculations could underflow and panic. This only seemed to occur in tests, I assume because it's running a debug mode. The wrapping is quickly fixed later on in release mode, which is why this seems to have gone unnoticed. The new checks ensure the value is always positive or zero.
Co-authored-by: Paul Daniel Faria <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting/injection.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/injection.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 415f24a6d..9d82b4009 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs | |||
@@ -155,17 +155,21 @@ pub(super) fn highlight_doc_comment( | |||
155 | let mut start_offset = None; | 155 | let mut start_offset = None; |
156 | let mut end_offset = None; | 156 | let mut end_offset = None; |
157 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { | 157 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { |
158 | // It's possible for orig_line_start - line_start to be negative. Add h.range.start() | ||
159 | // here and remove it from the end range after the loop below so that the values are | ||
160 | // always non-negative. | ||
161 | let offset = h.range.start() + orig_line_start - line_start; | ||
158 | if line_start <= &h.range.start() { | 162 | if line_start <= &h.range.start() { |
159 | start_offset.get_or_insert(orig_line_start - line_start); | 163 | start_offset.get_or_insert(offset); |
160 | break; | 164 | break; |
161 | } else { | 165 | } else { |
162 | end_offset.get_or_insert(orig_line_start - line_start); | 166 | end_offset.get_or_insert(offset); |
163 | } | 167 | } |
164 | } | 168 | } |
165 | if let Some(start_offset) = start_offset { | 169 | if let Some(start_offset) = start_offset { |
166 | h.range = TextRange::new( | 170 | h.range = TextRange::new( |
167 | h.range.start() + start_offset, | 171 | start_offset, |
168 | h.range.end() + end_offset.unwrap_or(start_offset), | 172 | h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), |
169 | ); | 173 | ); |
170 | 174 | ||
171 | stack.add(h); | 175 | stack.add(h); |