Skip to content

Commit

Permalink
Add support for NaN in RESP3 protocol
Browse files Browse the repository at this point in the history
Fix: #143

This was initially missing from the spec and added about a
year ago: redis/redis-specifications#10
  • Loading branch information
byroot committed Oct 22, 2023
1 parent 1ab081c commit 765c689
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

- Add support for `NaN` in RESP3 protocol doubles.
This was initially missing from the spec and added about a year ago.

# 0.17.0

- Adds `sentinel_username` and `sentinel_password` options for `RedisClient#sentinel`
Expand Down
3 changes: 3 additions & 0 deletions hiredis-client/ext/redis_client/hiredis/vendor/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ static int processLineItem(redisReader *r) {
d = INFINITY; /* Positive infinite. */
} else if (strcasecmp(buf,",-inf") == 0) {
d = -INFINITY; /* Negative infinite. */
} else if ((len == 3 && strcasecmp(buf,"nan") == 0) ||
(len == 4 && strcasecmp(buf, "-nan") == 0)) {
d = NAN; /* nan. */
} else {
d = strtod((char*)buf,&eptr);
if (buf[0] == '\0' || eptr[0] != '\0' || isnan(d)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/redis_client/ruby_connection/resp3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def parse_integer(io)

def parse_double(io)
case value = io.gets_chomp
when "nan"
Float::NAN
when "inf"
Float::INFINITY
when "-inf"
Expand Down
12 changes: 9 additions & 3 deletions test/redis_client/resp3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def test_load_double
assert_parses(-42.42, ",-42.42\r\n")
assert_parses Float::INFINITY, ",inf\r\n"
assert_parses(-Float::INFINITY, ",-inf\r\n")
assert_parses(nil, ",nan\r\n") do |actual|
assert_predicate actual, :nan?
end
end

def test_load_null
Expand Down Expand Up @@ -132,10 +135,13 @@ def test_load_verbatim_string
def assert_parses(expected, payload)
raw_io = StringIO.new(payload.b)
io = RedisClient::RubyConnection::BufferedIO.new(raw_io, read_timeout: 1, write_timeout: 1)
if expected.nil?
assert_nil RESP3.load(io)
actual = RESP3.load(io)
if block_given?
yield actual
elsif expected.nil?
assert_nil actual
else
assert_equal(expected, RESP3.load(io))
assert_equal(expected, actual)
end

assert io.eof?, "Expected IO to be fully consumed: #{raw_io.read.inspect}"
Expand Down

0 comments on commit 765c689

Please sign in to comment.