From 8ac0fedbbb742851b13c3ccabc9a946321532183 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 23 Feb 2018 07:53:25 -0800 Subject: [PATCH] Suppress errors from invalid path encoding and carry on with best effort Fixes #9299 --- lib/vagrant.rb | 1 + lib/vagrant/shared_helpers.rb | 19 +++++++++++++++++++ lib/vagrant/util/platform.rb | 10 +++++++++- test/unit/vagrant/shared_helpers_test.rb | 14 ++++++++++++++ test/unit/vagrant/util/platform_test.rb | 9 +++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index af527fa95..985ec1926 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -68,6 +68,7 @@ require 'openssl' # Always make the version available require 'vagrant/version' global_logger = Log4r::Logger.new("vagrant::global") +Vagrant.global_logger = global_logger global_logger.info("Vagrant version: #{Vagrant::VERSION}") global_logger.info("Ruby version: #{RUBY_VERSION}") global_logger.info("RubyGems version: #{Gem::VERSION}") diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index ae9cd15df..0bdcd7fa5 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -147,4 +147,23 @@ module Vagrant end end end + + # Set the global logger + # + # @param log Logger + # @return [Logger] + def self.global_logger=(log) + @_global_logger = log + end + + # Get the global logger instance + # + # @return [Logger] + def self.global_logger + if @_global_logger.nil? + require "log4r" + @_global_logger = Log4r::Logger.new("vagrant::global") + end + @_global_logger + end end diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index 7f0d6f039..bcf66c751 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -223,7 +223,15 @@ module Vagrant # Traverse each part and join it into the resulting path original.each do |single| Dir.entries(path).each do |entry| - if entry.downcase == single.encode('filesystem').downcase + begin + single = single.encode("filesystem").to_s + rescue ArgumentError => err + Vagrant.global_logger.warn("path encoding failed - part=#{single} err=#{err.class} msg=#{err}") + # NOTE: Depending on the Windows environment the above + # encode will generate an "input string invalid" when + # attempting to encode. If that happens, continue on + end + if entry.downcase == single.downcase path = path.join(entry) end end diff --git a/test/unit/vagrant/shared_helpers_test.rb b/test/unit/vagrant/shared_helpers_test.rb index 64adf7bc6..7ff41e6a6 100644 --- a/test/unit/vagrant/shared_helpers_test.rb +++ b/test/unit/vagrant/shared_helpers_test.rb @@ -162,4 +162,18 @@ describe Vagrant do end end end + + describe "#global_logger" do + after{ subject.global_logger = nil } + + it "should return a logger when none have been provided" do + expect(subject.global_logger).not_to be_nil + end + + it "should return previously set logger" do + logger = double("logger") + expect(subject.global_logger = logger).to eq(logger) + expect(subject.global_logger).to eq(logger) + end + end end diff --git a/test/unit/vagrant/util/platform_test.rb b/test/unit/vagrant/util/platform_test.rb index 8b4046188..80100e349 100644 --- a/test/unit/vagrant/util/platform_test.rb +++ b/test/unit/vagrant/util/platform_test.rb @@ -133,6 +133,15 @@ describe Vagrant::Util::Platform do it "fixes drive letters on Windows", :windows do expect(described_class.fs_real_path("c:/foo").to_s).to eql("C:/foo") end + + it "gracefully handles invalid input string errors" do + bad_string = double("bad_string") + allow(bad_string).to receive(:to_s).and_raise(ArgumentError) + allow_any_instance_of(String).to receive(:encode).with("filesystem").and_return(bad_string) + allow(subject).to receive(:fs_case_sensitive?).and_return(false) + + expect(described_class.fs_real_path("/dev/null").to_s).to eql("/dev/null") + end end describe "#windows_unc_path" do