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

Fix IPv6 encoding in protobuf #49933

Merged
merged 4 commits into from May 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Formats/ProtobufSerializer.cpp
Expand Up @@ -1852,25 +1852,26 @@ namespace

write_function = [this](IPv6 value)
{
ipToString(value, text_buffer);
text_buffer = String(IPV6_BINARY_LENGTH, '\0');
memcpy(text_buffer.data(), &value.toUnderType(), IPV6_BINARY_LENGTH);
writeStr(text_buffer);
};

read_function = [this]() -> IPv6
{
readStr(text_buffer);
return parse<IPv6>(text_buffer);
if (text_buffer.size() != IPV6_BINARY_LENGTH)
throw Exception(ErrorCodes::PROTOBUF_BAD_CAST,
"Could not convert bytes field {} to IPv6 for inserting into column {} - field size {} is not equal to IPv6 size {}",
field_descriptor.full_name(), column_name, text_buffer.size(), IPV6_BINARY_LENGTH);
IPv6 value;
memcpy(&value.toUnderType(), text_buffer.data(), IPV6_BINARY_LENGTH);
return value;
};

default_function = [this]() -> IPv6 { return parse<IPv6>(field_descriptor.default_value_string()); };
}

static void ipToString(const IPv6 & ip, String & str)
{
WriteBufferFromString buf{str};
writeText(ip, buf);
}

std::function<void(IPv6)> write_function;
std::function<IPv6()> read_function;
std::function<IPv6()> default_function;
Expand Down
2 changes: 2 additions & 0 deletions tests/queries/0_stateless/02751_protobuf_ipv6.reference
@@ -0,0 +1,2 @@
::ffff:1.2.3.4
::ffff:1.2.3.4
14 changes: 14 additions & 0 deletions tests/queries/0_stateless/02751_protobuf_ipv6.sh
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Tags: no-fasttest

CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh

SCHEMADIR=$CURDIR/format_schemas


echo -ne '\x12\x1a\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x01\x02\x03\x04' | $CLICKHOUSE_LOCAL --input-format Protobuf --format_schema="$SCHEMADIR/02751_protobuf_ipv6:Message" --structure="ipv6_bytes IPv6" -q "select * from table"

$CLICKHOUSE_LOCAL -q "select '::ffff:1.2.3.4'::IPv6 as ipv6_bytes format Protobuf settings format_schema = '$SCHEMADIR/02751_protobuf_ipv6:Message'" | $CLICKHOUSE_LOCAL --input-format Protobuf --format_schema="$SCHEMADIR/02751_protobuf_ipv6:Message" --structure="ipv6_bytes IPv6" -q "select * from table"

@@ -0,0 +1,6 @@
syntax = "proto3";

message Message
{
bytes ipv6_bytes = 3;
}