Skip to content

Commit

Permalink
[fix](coalesce) fix 'heap-use-after-free' of function coalesce (#42666)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktengg committed Oct 29, 2024
1 parent d5c43c5 commit 58cf980
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions be/src/vec/functions/function_coalesce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class FunctionCoalesce : public IFunction {
public:
static constexpr auto name = "coalesce";

mutable DataTypePtr result_type;
mutable FunctionBasePtr func_is_not_null;

static FunctionPtr create() { return std::make_shared<FunctionCoalesce>(); }
Expand All @@ -70,26 +69,25 @@ class FunctionCoalesce : public IFunction {
size_t get_number_of_arguments() const override { return 0; }

DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DataTypePtr res;
for (const auto& arg : arguments) {
if (!arg->is_nullable()) {
result_type = arg;
res = arg;
break;
}
}

result_type = result_type ? result_type : arguments[0];
return result_type;
res = res ? res : arguments[0];

const ColumnsWithTypeAndName is_not_null_col {{nullptr, make_nullable(res), ""}};
func_is_not_null = SimpleFunctionFactory::instance().get_function(
"is_not_null_pred", is_not_null_col, std::make_shared<DataTypeUInt8>());

return res;
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) const override {
if (!func_is_not_null) [[unlikely]] {
const ColumnsWithTypeAndName is_not_null_col {
{nullptr, make_nullable(result_type), ""}};
func_is_not_null = SimpleFunctionFactory::instance().get_function(
"is_not_null_pred", is_not_null_col, std::make_shared<DataTypeUInt8>(),
{.enable_decimal256 = context->state()->enable_decimal256()});
}
DCHECK_GE(arguments.size(), 1);
DataTypePtr result_type = block.get_by_position(result).type;
ColumnNumbers filtered_args;
Expand Down

0 comments on commit 58cf980

Please sign in to comment.