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

raise on invalid UInt8 and Int8 when encoding RowBinary #180

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- raise on invalid UInt8 and Int8 when encoding RowBinary https://github.com/plausible/ch/pull/180

## 0.2.6 (2024-05-30)

- fix query encoding for datetimes where the microseconds value starts with zeroes `~U[****-**-** **:**:**.0*****]` https://github.com/plausible/ch/pull/175
Expand Down
16 changes: 13 additions & 3 deletions lib/ch/row_binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,23 @@ defmodule Ch.RowBinary do

def encode({:fixed_string, size}, nil), do: <<0::size(size * 8)>>

def encode(:u8, u) when is_integer(u), do: u
# UInt8 — [0 : 255]
def encode(:u8, u) when is_integer(u) and u >= 0 and u <= 255, do: u
def encode(:u8, nil), do: 0

def encode(:i8, i) when is_integer(i) and i >= 0, do: i
def encode(:i8, i) when is_integer(i), do: <<i::signed>>
def encode(:u8, term) do
raise ArgumentError, "invalid UInt8: #{inspect(term)}"
end

# Int8 — [-128 : 127]
def encode(:i8, i) when is_integer(i) and i >= 0 and i <= 127, do: i
def encode(:i8, i) when is_integer(i) and i < 0 and i >= -128, do: <<i::signed>>
def encode(:i8, nil), do: 0

def encode(:i8, term) do
raise ArgumentError, "invalid Int8: #{inspect(term)}"
end

for size <- [16, 32, 64, 128, 256] do
def encode(unquote(:"u#{size}"), u) when is_integer(u) do
<<u::unquote(size)-little>>
Expand Down
16 changes: 16 additions & 0 deletions test/ch/row_binary_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,20 @@ defmodule Ch.RowBinaryTest do
assert decode_rows(<<1, 2>>, [:u8, :u8]) == [[1, 2]]
end
end

# TODO maybe use stream_data?
describe "invalid arguments" do
# https://github.com/plausible/ch/issues/166
test "for UInt8" do
assert_raise ArgumentError, "invalid UInt8: 256", fn -> encode(:u8, 256) end
assert_raise ArgumentError, "invalid UInt8: -1", fn -> encode(:u8, -1) end
assert_raise ArgumentError, "invalid UInt8: \"a\"", fn -> encode(:u8, "a") end
end

test "for Int8" do
assert_raise ArgumentError, "invalid Int8: 128", fn -> encode(:i8, 128) end
assert_raise ArgumentError, "invalid Int8: -129", fn -> encode(:i8, -129) end
assert_raise ArgumentError, "invalid Int8: \"a\"", fn -> encode(:i8, "a") end
end
end
end