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

[FB4] Fix of problems with passing an array in UDF (#7892) #7895

Open
wants to merge 3 commits into
base: v4.0-release
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions src/jrd/Function.epp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,19 @@ Function* Function::loadMetadata(thread_db* tdbb, USHORT id, bool noscan, USHORT
if (parameter->prm_desc.dsc_dtype == dtype_cstring)
parameter->prm_desc.dsc_length++;

length += (parameter->prm_desc.dsc_dtype == dtype_blob) ?
sizeof(udf_blob) : FB_ALIGN(parameter->prm_desc.dsc_length, FB_DOUBLE_ALIGN);
if (parameter->prm_fun_mechanism == FUN_scalar_array)
{
//scalar_array_desc is always allocated separately. See get_scalar_array in fun.epp
}
else
if (parameter->prm_desc.dsc_dtype == dtype_blob)
{
length += sizeof(udf_blob);
}
else
{
length += FB_ALIGN(parameter->prm_desc.dsc_length, FB_DOUBLE_ALIGN);
}

count = MAX(count, size_t(Y.RDB$ARGUMENT_POSITION));
}
Expand Down
48 changes: 36 additions & 12 deletions src/jrd/fun.epp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ enum UdfError
static SSHORT blob_get_segment(blb*, UCHAR*, USHORT, USHORT*);
static void blob_put_segment(blb*, const UCHAR*, USHORT);
static SLONG blob_lseek(blb*, USHORT, SLONG);
static ULONG get_scalar_array(thread_db* tdbb, const Parameter*, DSC*,
scalar_array_desc*, UCharStack&);
static scalar_array_desc* get_scalar_array(thread_db* tdbb, const Parameter*, DSC*, UCharStack&);
static void invoke(thread_db* tdbb,
const Function* function,
const Parameter* return_ptr,
Expand Down Expand Up @@ -478,8 +477,10 @@ void FUN_evaluate(thread_db* tdbb, const Function* function, const NestValueArra
}
else if (parameter->prm_fun_mechanism == FUN_scalar_array)
{
length = get_scalar_array(tdbb, parameter, input, (scalar_array_desc*) temp_ptr,
array_stack);
scalar_array_desc* const scalar_array = get_scalar_array(tdbb, parameter, input, array_stack);

*arg_ptr++ = reinterpret_cast<UDF_ARG*>(scalar_array);
continue;
}
else
{
Expand Down Expand Up @@ -976,11 +977,10 @@ static SSHORT blob_get_segment(blb* blob, UCHAR* buffer, USHORT length, USHORT*
}


static ULONG get_scalar_array(thread_db* tdbb,
const Parameter* arg,
DSC* value,
scalar_array_desc* scalar_desc,
UCharStack& stack)
static scalar_array_desc* get_scalar_array(thread_db* const tdbb,
const Parameter* const arg,
DSC* const value,
UCharStack& stack)
{
/**************************************
*
Expand All @@ -1007,6 +1007,14 @@ static ULONG get_scalar_array(thread_db* tdbb,
blob->BLB_get_data(tdbb, data, array_desc->iad_total_length);
const USHORT dimensions = array_desc->iad_dimensions;

if (dimensions == 0) // todo: correct this message
status_exception::raise(Arg::Gds(isc_random) << "Array dimension can not be zero.");

static_assert(MAX_ARRAY_DIMENSIONS == 16, "");

if (MAX_ARRAY_DIMENSIONS < dimensions)
status_exception::raise(Arg::Gds(isc_random) << "Array has more than 16 dimensions.");

// Convert array, if necessary

dsc to = arg->prm_desc;
Expand All @@ -1031,11 +1039,22 @@ static ULONG get_scalar_array(thread_db* tdbb,
}

// Fill out the scalar array descriptor
size_t const scalar_array_desc_sz
= sizeof(scalar_array_desc) + (dimensions - 1u) * sizeof(scalar_array_desc::sad_repeat);

AutoPtr<UCHAR, ArrayDelete> scalar_array_desc_mem(FB_NEW_POOL(pool) UCHAR[scalar_array_desc_sz]);

scalar_array_desc* const scalar_desc = (scalar_array_desc*) scalar_array_desc_mem.get();

// check a pointer align
fb_assert((((uintptr_t)scalar_desc) % sizeof(void*)) == 0);

// scalar_desc->sad_rpt has one element
static_assert(sizeof(scalar_desc->sad_rpt) == sizeof(scalar_desc->sad_rpt[0]), "");

scalar_desc->sad_desc = arg->prm_desc;
scalar_desc->sad_desc.dsc_address = data;
scalar_desc->sad_dimensions = dimensions;
stack.push(data.release());

const Ods::InternalArrayDesc::iad_repeat* tail1 = array_desc->iad_rpt;
scalar_array_desc::sad_repeat* tail2 = scalar_desc->sad_rpt;
Expand All @@ -1046,8 +1065,13 @@ static ULONG get_scalar_array(thread_db* tdbb,
tail2->sad_lower = tail1->iad_lower;
}

return static_cast<ULONG>(sizeof(scalar_array_desc) +
(dimensions - 1u) * sizeof(scalar_array_desc::sad_repeat));
stack.push(data.get());
data.release();

stack.push(scalar_array_desc_mem.get());
scalar_array_desc_mem.release();

return scalar_desc;
}


Expand Down