Skip to content

Commit 2aeff37

Browse files
committed
Replace overflowing sub with checked_sub.
In the case when a no-break-space \u{a0} exists before any non white space characters an error is generated that points into the white space. When the terminal width is also too small a subtraction is triggered here that would overflow. That subtraction implicitly assumed that no errors occur in the white space. This commit replaces that subtraction with an explicitly checked one to avoid this overflow.
1 parent c26db43 commit 2aeff37

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ impl Margin {
140140

141141
if self.computed_right - self.computed_left > self.column_width {
142142
// Trimming only whitespace isn't enough, let's get craftier.
143-
if self.label_right - self.whitespace_left <= self.column_width {
143+
144+
// Note: self.label_right - self.whitespace_left will overflow in the case that the label
145+
// is addressing whitespace. This happens when a spurious no-breaking-space (\u{a0})
146+
// is present in the whitespace.
147+
if let Some(non_whitespace_label_width) =
148+
self.label_right.checked_sub(self.whitespace_left)
149+
&& non_whitespace_label_width <= self.column_width
150+
{
144151
// Attempt to fit the code window only trimming whitespace.
145152
self.computed_left = self.whitespace_left;
146153
self.computed_right = self.computed_left + self.column_width;

0 commit comments

Comments
 (0)