From 2aeff37840096f7bc086b8822c5539efbbf5b800 Mon Sep 17 00:00:00 2001 From: Harrison Kaiser Date: Sun, 15 Dec 2024 20:06:02 -0500 Subject: [PATCH] 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. --- compiler/rustc_errors/src/emitter.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index ac2f91cdeb3fe..075c0cccdf80a 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -140,7 +140,14 @@ impl Margin { if self.computed_right - self.computed_left > self.column_width { // Trimming only whitespace isn't enough, let's get craftier. - if self.label_right - self.whitespace_left <= self.column_width { + + // Note: self.label_right - self.whitespace_left will overflow in the case that the label + // is addressing whitespace. This happens when a spurious no-breaking-space (\u{a0}) + // is present in the whitespace. + if let Some(non_whitespace_label_width) = + self.label_right.checked_sub(self.whitespace_left) + && non_whitespace_label_width <= self.column_width + { // Attempt to fit the code window only trimming whitespace. self.computed_left = self.whitespace_left; self.computed_right = self.computed_left + self.column_width;