From da2c57d3d33a0f643305f78ecc04729a475a2202 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 24 Jul 2017 17:04:25 -0700 Subject: [PATCH] Make best effort to encode to UTF-8. On failure log error and retain original. --- lib/vagrant/util/safe_exec.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/util/safe_exec.rb b/lib/vagrant/util/safe_exec.rb index 53f0b2597..3eacedb25 100644 --- a/lib/vagrant/util/safe_exec.rb +++ b/lib/vagrant/util/safe_exec.rb @@ -44,8 +44,19 @@ module Vagrant # Re-generate strings to ensure common encoding @@logger.debug("Converting command and arguments to common UTF-8 encoding for exec.") @@logger.debug("Command: `#{command.inspect}` Args: `#{args.inspect}`") - command = "#{command}".force_encoding("UTF-8") - args = args.map{|arg| "#{arg}".force_encoding("UTF-8") } + begin + command = "#{command}".encode("UTF-8") + rescue Encoding::UndefinedConversionError => e + @@logger.warn("Failed to convert command - #{e.class}: #{e} (`#{command}`)") + end + args = args.map do |arg| + begin + "#{arg}".encode("UTF-8") + rescue Encoding::UndefinedConversionError => e + @@logger.warn("Failed to convert command argument - #{e.class}: #{e} (`#{arg}`)") + arg + end + end @@logger.debug("Converted - Command: `#{command.inspect}` Args: `#{args.inspect}`") end Kernel.exec(command, *args)