From 53714abfa39921eb0eeb35c67e624fa369e4b0a2 Mon Sep 17 00:00:00 2001 From: Sam Phippen Date: Sat, 12 Sep 2015 14:32:05 +0100 Subject: [PATCH] Handle encoding errors in shell provisioner newline normalization. The problem demonstrated in #6065 is that a string has incorrectly been encoded with US-ASCII even though it contains invalid US-ASCII byte sequences (any byte with the most significant bit on is invalid in the US-ASCII encoding). The thing about doing newline normalization is that it is not actually sensitive to the presence of US-ASCII byte sequenzes. Additionally, it is very unlikely that a user will ever be using an encoding where \r\n is not encoded the same as it would be in ASCII. This patch first tries the existing method of normalizing the newlines in the provided script file, if that fails for any reason it force encodes the string to ASCII-8BIT (which allows the most significant bit to be on in any individual byte) and then performs the substitution in that byte space. --- plugins/provisioners/shell/provisioner.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/provisioners/shell/provisioner.rb b/plugins/provisioners/shell/provisioner.rb index 4ee302f5d..1b89d1f29 100644 --- a/plugins/provisioners/shell/provisioner.rb +++ b/plugins/provisioners/shell/provisioner.rb @@ -182,7 +182,11 @@ module VagrantPlugins # Replace Windows line endings with Unix ones unless binary file # or we're running on Windows. if !config.binary && @machine.config.vm.communicator != :winrm - script.gsub!(/\r\n?$/, "\n") + begin + script.gsub!(/\r\n?$/, "\n") + rescue ArgumentError + script = script.force_encoding("ASCII-8BIT").gsub(/\r\n?$/, "\n") + end end # Otherwise we have an inline script, we need to Tempfile it,