Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

force key encoding to binary to avoid UTF-8 issues #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,14 @@ def rem_namespace(key)
key.each { |k| yielder.yield rem_namespace(k) }
end
else
key.to_s.sub(/\A#{@namespace}:/, '')
string_key = key.to_s.dup
if string_key.respond_to? :force_encoding
# force_encoding is not available in ruby 1.8.7
rem_key = string_key.force_encoding('BINARY')
else
rem_key = string_key
end
rem_key.sub(/\A#{@namespace}:/, '')
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@
@namespaced['foo'].should eq(nil)
end

it "should not throw exception on invalid UTF-8 sequences in keys" do
@namespaced.set("f\xFCo", 'bar')
@namespaced.set("foo", 'bar')
@namespaced.get("f\xFCo").should eq('bar')
keys = @namespaced.keys.sort
# force_encoding is not available in ruby 1.8.7
if "".respond_to? :force_encoding
keys.should eq ["f\xFCo".force_encoding('BINARY'), "foo"].sort
else
keys.should eq ["f\xFCo", "foo"].sort
end
keys.each do |k|
@namespaced.get(k).should eq('bar')
end
end

it "should respond to :namespace=" do
@namespaced.respond_to?(:namespace=).should eq(true)
end
Expand Down