Skip to content

Commit

Permalink
Fixed crash in pg_show_all_settings (#10261)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitstn authored Oct 10, 2024
1 parent 0fb3542 commit f6e0d96
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
1 change: 1 addition & 0 deletions ydb/library/yql/parser/pg_catalog/safe_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"jsonb_path_query_first_tz",
"jsonb_typeof",

"pg_show_all_settings",//pgadmin
"pg_is_in_recovery",//pgadmin
"pg_is_wal_replay_paused",//pgadmin
"has_database_privilege",//pgadmin
Expand Down
72 changes: 54 additions & 18 deletions ydb/library/yql/parser/pg_wrapper/comp_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,15 @@ class TPgResolvedCallBase : public TMutableComputationNode<TDerived> {
}

FInfo.fn_expr = ArgsExprBuilder.Build(ProcDesc);

if (StructType) {
StructTypeDesc.reserve(StructType->GetMembersCount());
for (ui32 i = 0; i < StructType->GetMembersCount(); ++i) {
auto itemType = StructType->GetMemberType(i);
auto type = AS_TYPE(TPgType, itemType)->GetTypeId();
StructTypeDesc.emplace_back(NPg::LookupType(type));
}
}
}

private:
Expand All @@ -1268,6 +1277,7 @@ class TPgResolvedCallBase : public TMutableComputationNode<TDerived> {
const TVector<TType*> ArgTypes;
const TStructType* StructType;
TVector<NPg::TTypeDesc> ArgDesc;
TVector<NPg::TTypeDesc> StructTypeDesc;

TPgArgsExprBuilder ArgsExprBuilder;
};
Expand Down Expand Up @@ -1370,7 +1380,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
public:
TIterator(TMemoryUsageInfo* memInfo, const std::string_view& name, const TUnboxedValueVector& args,
const TVector<NPg::TTypeDesc>& argDesc, const NPg::TTypeDesc& retTypeDesc, const NPg::TProcDesc& procDesc,
const FmgrInfo* fInfo, const TStructType* structType, const THolderFactory& holderFactory)
const FmgrInfo* fInfo, const TStructType* structType, const TVector<NPg::TTypeDesc>& structTypeDesc, const THolderFactory& holderFactory)
: TComputationValue<TIterator>(memInfo)
, Name(name)
, Args(args)
Expand All @@ -1379,6 +1389,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
, ProcDesc(procDesc)
, CallInfo(argDesc.size(), fInfo)
, StructType(structType)
, StructTypeDesc(structTypeDesc)
, HolderFactory(holderFactory)
{
auto& callInfo = CallInfo.Ref();
Expand Down Expand Up @@ -1457,20 +1468,38 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
tuplestore_select_read_pointer(RSInfo.Ref().setResult, readPtr);
return CopyTuple(value);
} else {
YQL_ENSURE(!StructType);
if (RSInfo.Ref().isDone == ExprEndResult) {
FinishAndFree();
return false;
}

if (callInfo.isnull) {
value = NUdf::TUnboxedValuePod();
if (StructType) {
YQL_ENSURE(!callInfo.isnull);
auto tuple = DatumGetHeapTupleHeader(ret);
YQL_ENSURE(HeapTupleHeaderGetNatts(tuple) == StructType->GetMembersCount());
HeapTupleData tmptup;
tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple);
ItemPointerSetInvalid(&(tmptup.t_self));
tmptup.t_tableOid = InvalidOid;
tmptup.t_data = tuple;

NUdf::TUnboxedValue* itemsPtr;
value = HolderFactory.CreateDirectArrayHolder(StructType->GetMembersCount(), itemsPtr);
for (ui32 i = 0; i < StructType->GetMembersCount(); ++i) {
bool isNull;
auto datum = heap_getattr(&tmptup,i + 1,RSInfo.Ref().expectedDesc,&isNull);
itemsPtr[StructIndicies[i]] = CloneTupleItem(i, isNull, datum);
}
} else {
if (RetTypeDesc.PassByValue) {
value = ScalarDatumToPod(ret);
if (callInfo.isnull) {
value = NUdf::TUnboxedValuePod();
} else {
auto cloned = datumCopy(ret, false, RetTypeDesc.TypeLen);
value = PointerDatumToPod(cloned);
if (RetTypeDesc.PassByValue) {
value = ScalarDatumToPod(ret);
} else {
auto cloned = datumCopy(ret, false, RetTypeDesc.TypeLen);
value = PointerDatumToPod(cloned);
}
}
}

Expand Down Expand Up @@ -1513,26 +1542,30 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
return true;
}

NUdf::TUnboxedValuePod CloneTupleItem(ui32 index) {
if (TupleSlot->tts_isnull[index]) {
NUdf::TUnboxedValuePod CloneTupleItem(ui32 index, bool isNull, Datum datum) {
if (isNull) {
return NUdf::TUnboxedValuePod();
} else {
auto datum = TupleSlot->tts_values[index];
if (RetTypeDesc.PassByValue) {
const auto& desc = StructType ? StructTypeDesc[StructIndicies[index]] : RetTypeDesc;
if (desc.PassByValue) {
return ScalarDatumToPod(datum);
} else if (RetTypeDesc.TypeLen == -1) {
} else if (desc.TypeLen == -1) {
const text* orig = (const text*)datum;
return PointerDatumToPod((Datum)MakeVar(GetVarBuf(orig)));
} else if(RetTypeDesc.TypeLen == -2) {
} else if(desc.TypeLen == -2) {
const char* orig = (const char*)datum;
return PointerDatumToPod((Datum)MakeCString(orig));
} else {
const char* orig = (const char*)datum;
return PointerDatumToPod((Datum)MakeFixedString(orig, RetTypeDesc.TypeLen));
return PointerDatumToPod((Datum)MakeFixedString(orig, desc.TypeLen));
}
}
}

NUdf::TUnboxedValuePod CloneTupleItem(ui32 index) {
return CloneTupleItem(index, TupleSlot->tts_isnull[index], TupleSlot->tts_values[index]);
}

void FinishAndFree() {
if (TupleSlot) {
ExecDropSingleTupleTableSlot(TupleSlot);
Expand All @@ -1552,6 +1585,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
TExprContextHolder ExprContextHolder;
TFunctionCallInfo CallInfo;
const TStructType* StructType;
const TVector<NPg::TTypeDesc>& StructTypeDesc;
const THolderFactory& HolderFactory;
TReturnSetInfo RSInfo;
bool IsFinished = false;
Expand All @@ -1562,7 +1596,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
TListValue(TMemoryUsageInfo* memInfo, TComputationContext& compCtx,
const std::string_view& name, TUnboxedValueVector&& args, const TVector<NPg::TTypeDesc>& argDesc,
const NPg::TTypeDesc& retTypeDesc, const NPg::TProcDesc& procDesc, const FmgrInfo* fInfo,
const TStructType* structType, const THolderFactory& holderFactory)
const TStructType* structType, const TVector<NPg::TTypeDesc>& structTypeDesc, const THolderFactory& holderFactory)
: TCustomListValue(memInfo)
, CompCtx(compCtx)
, Name(name)
Expand All @@ -1572,13 +1606,14 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
, ProcDesc(procDesc)
, FInfo(fInfo)
, StructType(structType)
, StructTypeDesc(structTypeDesc)
, HolderFactory(holderFactory)
{
}

private:
NUdf::TUnboxedValue GetListIterator() const final {
return CompCtx.HolderFactory.Create<TIterator>(Name, Args, ArgDesc, RetTypeDesc, ProcDesc, FInfo, StructType, CompCtx.HolderFactory);
return CompCtx.HolderFactory.Create<TIterator>(Name, Args, ArgDesc, RetTypeDesc, ProcDesc, FInfo, StructType, StructTypeDesc, CompCtx.HolderFactory);
}

TComputationContext& CompCtx;
Expand All @@ -1589,6 +1624,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
const NPg::TProcDesc& ProcDesc;
const FmgrInfo* FInfo;
const TStructType* StructType;
const TVector<NPg::TTypeDesc>& StructTypeDesc;
const THolderFactory& HolderFactory;
};

Expand All @@ -1607,7 +1643,7 @@ class TPgResolvedMultiCall : public TPgResolvedCallBase<TPgResolvedMultiCall> {
args.push_back(value);
}

return compCtx.HolderFactory.Create<TListValue>(compCtx, Name, std::move(args), ArgDesc, RetTypeDesc, ProcDesc, &FInfo, StructType, compCtx.HolderFactory);
return compCtx.HolderFactory.Create<TListValue>(compCtx, Name, std::move(args), ArgDesc, RetTypeDesc, ProcDesc, &FInfo, StructType, StructTypeDesc, compCtx.HolderFactory);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ show_all_settings(PG_FUNCTION_ARGS)
funcctx->attinmeta = attinmeta;

/* collect the variables, in sorted order */
guc_vars = get_guc_variables(&num_vars);
guc_vars = NULL;
num_vars = 0;

/* use user_fctx to remember the array location */
funcctx->user_fctx = guc_vars;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2961,9 +2961,9 @@
],
"test.test[pg_catalog-pg_description_pg_syntax-default.txt-Results]": [
{
"checksum": "c67fbc7d8d6fb7fe3eff3f34c32a5b7a",
"checksum": "ab8e21760da4bf353ac12f633213227b",
"size": 2768,
"uri": "https://{canondata_backend}/1809005/6d5702cdf5f37d18a9deb9127ecce08cfe554c8a/resource.tar.gz#test.test_pg_catalog-pg_description_pg_syntax-default.txt-Results_/results.txt"
"uri": "https://{canondata_backend}/1689644/24cc2a34d7ebf1958abe0cd6c74c6af03994cd3e/resource.tar.gz#test.test_pg_catalog-pg_description_pg_syntax-default.txt-Results_/results.txt"
}
],
"test.test[pg_catalog-pg_timezone_abbrevs-default.txt-Debug]": [
Expand Down

0 comments on commit f6e0d96

Please sign in to comment.