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 panic on zero/nil value for net.IP for IPv4 and IPv6 types #1118

Merged
merged 1 commit into from
Oct 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/column/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ func (col *IPv4) AppendRow(v any) (err error) {
col.col.Append(0)
}
case net.IP:
col.col.Append(proto.ToIPv4(netIPToNetIPAddr(v)))
switch {
case len(v) == 0:
col.col.Append(0)
default:
col.col.Append(proto.ToIPv4(netIPToNetIPAddr(v)))
}
case *net.IP:
switch {
case v != nil:
Expand Down
4 changes: 4 additions & 0 deletions lib/column/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ func (col *IPv6) Encode(buffer *proto.Buffer) {
}

func IPv6ToBytes(ip net.IP) [16]byte {
if ip == nil {
return [16]byte{}
}

if len(ip) == 4 {
ip = ip.To16()
}
Expand Down
38 changes: 38 additions & 0 deletions tests/issues/1106_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package issues

import (
"context"
"net"
"testing"

"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
)

func Test1106(t *testing.T) {
var (
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
"max_execution_time": 60,
"allow_experimental_object_type": true,
}, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
)
ctx := context.Background()
require.NoError(t, err)
const ddl = "CREATE TABLE test_1106 (col1 IPv6, col2 IPv6, col3 IPv4, col4 IPv4) Engine MergeTree() ORDER BY tuple()"
require.NoError(t, conn.Exec(ctx, ddl))
defer func() {
conn.Exec(ctx, "DROP TABLE IF EXISTS test_1106")
}()

batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO test_1106")
require.NoError(t, err)

var ip net.IP
var ipPtr *net.IP

require.NoError(t, batch.Append(ip, ipPtr, ip, ipPtr))
require.NoError(t, batch.Send())
}