From f6cfcd4da9da90394bcdce3bb7100ed90a2c3804 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Thu, 11 Feb 2021 21:04:14 +0300 Subject: [PATCH] Fix null dereference with join_use_nulls=1 Found with MSan [1], the following query triggers null dereference: ```sql SELECT Y.id - 1 FROM X RIGHT JOIN Y ON (X.id + 1) = Y.id SETTINGS join_use_nulls=1; -- { serverError 53 } ``` ``` Received signal 11 (version 21.3.1.5916, build id: 2E9E84AA32AEAAC7C8B6EB45DA3EC0B4F15E9ED4) (from thread 100) (query_id: 9ab8cb0d-be8d-445e-8498-930a7268488b) Received signal Segmentation fault (11) Address: 0x10 Access: read. Address not mapped to object. Stack trace: 0x2d079d65 0x29bf1f30 0x12b12220 0x12b13098 0x12b17b08 0x12b20459 0x2ae37913 0x2ae352d9 0x2c746072 0x2c7585dd 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 0x2c7483e3 0x2c74e63d 4. ./obj-x86_64-linux-gnu/../contrib/boost/boost/smart_ptr/intrusive_ptr.hpp:0: DB::ColumnConst::ColumnConst(COW::immutable_ptr const&, unsigned long) @ 0x2d079d65 in /workspace/clickhouse 5. ./obj-x86_64-linux-gnu/../src/Common/COW.h:0: DB::createBlockWithNestedColumns(std::__1::vector > const&) @ 0x29bf1f30 in /workspace/clickhouse 6. DB::FunctionOverloadResolverAdaptor::getReturnTypeDefaultImplementationForNulls(std::__1::vector > const&, std::__1::function (std::__1::vector > const&)> const&) @ 0x12b12220 in /workspace/clickhouse 7. DB::FunctionOverloadResolverAdaptor::getReturnTypeWithoutLowCardinality(std::__1::vector > const&) const @ 0x12b13098 in /workspace/clickhouse 8. DB::FunctionOverloadResolverAdaptor::getReturnType(std::__1::vector > const&) const @ 0x12b17b08 in /workspace/clickhouse 9. DB::FunctionOverloadResolverAdaptor::build(std::__1::vector > const&) const @ 0x12b20459 in /workspace/clickhouse ``` [1]: https://clickhouse-test-reports.s3.yandex.net/19451/64c0bf98290362fa216c05b070aa122a12af3c25/fuzzer_msan/report.html#fail1 --- src/Functions/FunctionHelpers.cpp | 15 +++++++++++++-- .../0_stateless/01710_join_use_nulls.reference | 0 .../queries/0_stateless/01710_join_use_nulls.sql | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/01710_join_use_nulls.reference create mode 100644 tests/queries/0_stateless/01710_join_use_nulls.sql diff --git a/src/Functions/FunctionHelpers.cpp b/src/Functions/FunctionHelpers.cpp index d64646ecaf1..17c28ee3343 100644 --- a/src/Functions/FunctionHelpers.cpp +++ b/src/Functions/FunctionHelpers.cpp @@ -70,8 +70,19 @@ ColumnsWithTypeAndName createBlockWithNestedColumns(const ColumnsWithTypeAndName } else if (const auto * const_column = checkAndGetColumn(*col.column)) { - const auto & nested_col = checkAndGetColumn(const_column->getDataColumn())->getNestedColumnPtr(); - res.emplace_back(ColumnWithTypeAndName{ ColumnConst::create(nested_col, col.column->size()), nested_type, col.name}); + const auto * nullable_column = checkAndGetColumn(const_column->getDataColumn()); + + ColumnPtr nullable_res; + if (nullable_column) + { + const auto & nested_col = nullable_column->getNestedColumnPtr(); + nullable_res = ColumnConst::create(nested_col, col.column->size()); + } + else + { + nullable_res = makeNullable(col.column); + } + res.emplace_back(ColumnWithTypeAndName{ nullable_res, nested_type, col.name }); } else throw Exception("Illegal column for DataTypeNullable", ErrorCodes::ILLEGAL_COLUMN); diff --git a/tests/queries/0_stateless/01710_join_use_nulls.reference b/tests/queries/0_stateless/01710_join_use_nulls.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01710_join_use_nulls.sql b/tests/queries/0_stateless/01710_join_use_nulls.sql new file mode 100644 index 00000000000..2845af8b8ed --- /dev/null +++ b/tests/queries/0_stateless/01710_join_use_nulls.sql @@ -0,0 +1,15 @@ +DROP TABLE IF EXISTS X; +DROP TABLE IF EXISTS Y; + +CREATE TABLE X (id Int) ENGINE=Memory; +CREATE TABLE Y (id Int) ENGINE=Memory; + +-- Type mismatch of columns to JOIN by: plus(id, 1) Int64 at left, Y.id Int32 at right. +SELECT + Y.id - 1 +FROM X +RIGHT JOIN Y ON (X.id + 1) = Y.id +SETTINGS join_use_nulls=1; -- { serverError 53 } + +DROP TABLE X; +DROP TABLE Y;