Skip to content

Commit 3b962a1

Browse files
committed
Always assume UTF-8 encoding instead of relying on Encoding.default_external
1 parent bf00251 commit 3b962a1

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Always assume UTF-8 encoding instead of relying on `Encoding.default_external`.
4+
35
# 0.21.1
46

57
- Handle unresolved Sentinel master/replica error when displaying server URL in exceptions. See #182.

hiredis-client/ext/redis_client/hiredis/hiredis_connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static void *reply_create_string(const redisReadTask *task, char *cstr, size_t l
178178
len -= 4;
179179
}
180180

181-
VALUE string = rb_external_str_new(cstr, len);
181+
VALUE string = rb_utf8_str_new(cstr, len);
182182
if (rb_enc_str_coderange(string) == ENC_CODERANGE_BROKEN) {
183183
rb_enc_associate(string, rb_ascii8bit_encoding());
184184
}

lib/redis_client/ruby_connection/buffered_io.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ class BufferedIO
1010

1111
attr_accessor :read_timeout, :write_timeout
1212

13-
def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096, encoding: Encoding.default_external)
13+
def initialize(io, read_timeout:, write_timeout:, chunk_size: 4096)
1414
@io = io
15-
@encoding = encoding
16-
@buffer = "".dup.force_encoding(@encoding)
15+
@buffer = +""
1716
@offset = 0
1817
@chunk_size = chunk_size
1918
@read_timeout = read_timeout
@@ -175,9 +174,9 @@ def fill_buffer(strict, size = @chunk_size)
175174
if empty_buffer
176175
@offset = start
177176
empty_buffer = false
178-
@buffer.force_encoding(@encoding) if RESET_BUFFER_ENCODING
177+
@buffer.force_encoding(Encoding::UTF_8) if RESET_BUFFER_ENCODING
179178
else
180-
@buffer << bytes.force_encoding(@encoding)
179+
@buffer << bytes.force_encoding(Encoding::UTF_8)
181180
end
182181
remaining -= bytes.bytesize
183182
return if !strict || remaining <= 0

test/redis_client_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_encoding
2828
@redis.call("SET", "str", "fée")
2929
str = @redis.call("GET", "str")
3030

31-
assert_equal Encoding.default_external, str.encoding
31+
assert_equal Encoding::UTF_8, str.encoding
3232
assert_predicate str, :valid_encoding?
3333

3434
bytes = "\xFF\00"

0 commit comments

Comments
 (0)