Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't trust the encoding of ARGV. Fixes #307

  • Loading branch information...
commit 6bf71914de25776a6725eceae4046cd81c5ede45 1 parent be87fed
@evanphx evanphx authored
View
10 lib/rubygems/installer.rb
@@ -607,9 +607,13 @@ def app_script_text(bin_file_name)
version = "#{Gem::Requirement.default}"
-if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
- version = $1
- ARGV.shift
+if ARGV.first
+ str = ARGV.first
+ str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
+ if str =~ /\\A_(.*)_\\z/
+ version = $1
+ ARGV.shift
+ end
end
gem '#{spec.name}', version
View
4 lib/rubygems/installer_test_case.rb
@@ -162,7 +162,9 @@ def util_setup_gem(ui = @ui) # HACK fix use_ui to make this automatic
FileUtils.mkdir_p 'bin'
FileUtils.mkdir_p 'lib'
FileUtils.mkdir_p File.join('ext', 'a')
- File.open File.join('bin', 'executable'), 'w' do |f| f.puts '1' end
+ File.open File.join('bin', 'executable'), 'w' do |f|
+ f.puts "raise 'ran executable'"
+ end
File.open File.join('lib', 'code.rb'), 'w' do |f| f.puts '1' end
File.open File.join('ext', 'a', 'mkrf_conf.rb'), 'w' do |f|
f << <<-EOF
View
99 test/rubygems/test_gem_installer.rb
@@ -35,9 +35,13 @@ def test_app_script_text
version = \">= 0\"
-if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
- version = $1
- ARGV.shift
+if ARGV.first
+ str = ARGV.first
+ str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
+ if str =~ /\\A_(.*)_\\z/
+ version = $1
+ ARGV.shift
+ end
end
gem 'a', version
@@ -687,6 +691,95 @@ def test_install
assert_same @installer, @pre_install_hook_arg
end
+ def test_install_creates_working_binstub
+ Dir.mkdir util_inst_bindir
+ util_setup_gem
+ util_clear_gems
+
+ @installer.wrappers = true
+
+ gemdir = File.join @gemhome, 'gems', @spec.full_name
+
+ @newspec = nil
+ build_rake_in do
+ use_ui @ui do
+ @newspec = @installer.install
+ end
+ end
+
+ exe = File.join gemdir, 'bin', 'executable'
+
+ e = assert_raises RuntimeError do
+ instance_eval File.read(exe)
+ end
+
+ assert_match(/ran executable/, e.message)
+ end
+
+ def test_install_creates_binstub_that_understand_version
+ Dir.mkdir util_inst_bindir
+ util_setup_gem
+ util_clear_gems
+
+ @installer.wrappers = true
+
+ @newspec = nil
+ build_rake_in do
+ use_ui @ui do
+ @newspec = @installer.install
+ end
+ end
+
+ exe = File.join @gemhome, 'bin', 'executable'
+
+ ARGV.unshift "_3.0_"
+
+ begin
+ Gem::Specification.reset
+
+ e = assert_raises Gem::LoadError do
+ instance_eval File.read(exe)
+ end
+ ensure
+ ARGV.shift if ARGV.first == "_3.0_"
+ end
+
+ assert_match(/\(= 3\.0\)/, e.message)
+ end
+
+ def test_install_creates_binstub_that_dont_trust_encoding
+ skip unless "".respond_to?(:force_encoding)
+
+ Dir.mkdir util_inst_bindir
+ util_setup_gem
+ util_clear_gems
+
+ @installer.wrappers = true
+
+ @newspec = nil
+ build_rake_in do
+ use_ui @ui do
+ @newspec = @installer.install
+ end
+ end
+
+ exe = File.join @gemhome, 'bin', 'executable'
+
+ ARGV.unshift "\xE4pfel".force_encoding("UTF-8")
+
+ begin
+ Gem::Specification.reset
+
+ e = assert_raises RuntimeError do
+ instance_eval File.read(exe)
+ end
+ ensure
+ ARGV.shift if ARGV.first == "\xE4pfel"
+ end
+
+ assert_match(/ran executable/, e.message)
+ end
+
def test_install_with_no_prior_files
Dir.mkdir util_inst_bindir
util_clear_gems
Please sign in to comment.
Something went wrong with that request. Please try again.