From 79034bd291c13b92d68561ba957dc898c6ad3ae7 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Wed, 12 Mar 2025 20:58:56 -0400 Subject: [PATCH] fix(linker): prevent overflow when estimating CLI arg list length This also updates the estimate on Windows of the length argument list to `saturating_add` to avoid overflow. --- compiler/rustc_codegen_ssa/src/back/command.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/command.rs b/compiler/rustc_codegen_ssa/src/back/command.rs index 383d0579e52..84252386041 100644 --- a/compiler/rustc_codegen_ssa/src/back/command.rs +++ b/compiler/rustc_codegen_ssa/src/back/command.rs @@ -166,7 +166,8 @@ impl Command { // [1]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa // [2]: https://devblogs.microsoft.com/oldnewthing/?p=41553 - let estimated_command_line_len = self.args.iter().map(|a| a.len()).sum::(); + let estimated_command_line_len = + self.args.iter().fold(0usize, |acc, a| acc.saturating_add(a.as_encoded_bytes().len())); estimated_command_line_len > 1024 * 6 } }