From 8ae989f6b6e92ac68257fa2d078a38c5561b0ee2 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 15 Sep 2014 01:29:21 +0000 Subject: pathname.rb: fix a Pathname#relative_path_from crash on * ext/pathname/lib/pathname.rb (SAME_PATHS): Pathname#relative_path_from uses String#casecmp to compare strings on case-insensitive filesystem platforms (e.g., Windows). This can return nil for strings with different encodings, and the code previously assumed that it always returned a Fixnum. [Fix GH-713] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47591 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ ext/pathname/lib/pathname.rb | 3 ++- test/pathname/test_pathname.rb | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 599a55f88b..0959d20a2c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Sep 15 10:29:25 2014 Natalie Weizenbaum + + * ext/pathname/lib/pathname.rb (SAME_PATHS): + Pathname#relative_path_from uses String#casecmp to compare strings + on case-insensitive filesystem platforms (e.g., Windows). This can + return nil for strings with different encodings, and the code + previously assumed that it always returned a Fixnum. [Fix GH-713] + Mon Sep 15 09:43:18 2014 Sho Hashimoto * ext/fiddle/lib/fiddle/import.rb (Fiddle::Importer#sizeof): fix typo, diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index e61aa2c081..82541e9b15 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -22,7 +22,8 @@ class Pathname end SAME_PATHS = if File::FNM_SYSCASE.nonzero? - proc {|a, b| a.casecmp(b).zero?} + # Avoid #zero? here because #casecmp can return nil. + proc {|a, b| a.casecmp(b) == 0} else proc {|a, b| a == b} end diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 75040431d1..f77552cc9a 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1374,4 +1374,17 @@ class TestPathname < Test::Unit::TestCase assert_equal("foo/bar", File.join(Pathname.new("foo"), Pathname.new("bar").taint)) }.call end + + def test_relative_path_from_casefold + assert_separately([], <<-'end;') # do + module File::Constants + remove_const :FNM_SYSCASE + FNM_SYSCASE = FNM_CASEFOLD + end + require 'pathname' + foo = Pathname.new("fo\u{f6}") + bar = Pathname.new("b\u{e4}r".encode("ISO-8859-1")) + assert_instance_of(Pathname, foo.relative_path_from(bar)) + end; + end end -- cgit v1.2.3