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

Fixing missing life rage protection for the reverse filter (and others). #246

Merged
merged 2 commits into from
Feb 22, 2024
Merged
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
46 changes: 32 additions & 14 deletions src/filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte

if (!isConverted)
return result;

auto ProtectedValue = [&baseVal](InternalValue value) {
if (baseVal.ShouldExtendLifetime())
value.SetParentData(baseVal);
return value;
};

InternalValue attrName = GetArgumentValue("attribute", context);
InternalValue isCsVal = GetArgumentValue("case_sensitive", context, InternalValue(false));
Expand All @@ -482,23 +488,23 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
{
case FirstItemMode:
if (listSize)
result = list.GetValueByIndex(0);
result = ProtectedValue( list.GetValueByIndex(0) );
else
{
auto it = list.begin();
if (it != list.end())
result = *it;
result = ProtectedValue(*it);
}
break;
case LastItemMode:
if (listSize)
result = list.GetValueByIndex(listSize.value() - 1);
result = ProtectedValue(list.GetValueByIndex(listSize.value() - 1));
else
{
auto it = list.begin();
auto end = list.end();
for (; it != end; ++it)
result = *it;
result = ProtectedValue(*it);
}
break;
case LengthMode:
Expand All @@ -514,7 +520,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
if (listSize)
{
std::uniform_int_distribution<> dis(0, static_cast<int>(listSize.value()) - 1);
result = list.GetValueByIndex(dis(gen));
result = ProtectedValue(list.GetValueByIndex(dis(gen)));
}
else
{
Expand All @@ -525,7 +531,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
{
bool doCopy = count == 0 || std::uniform_int_distribution<size_t>(0, count)(gen) == 0;
if (doCopy)
result = *it;
result = ProtectedValue(*it);
}
}
break;
Expand All @@ -535,15 +541,15 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
auto b = list.begin();
auto e = list.end();
auto p = std::max_element(list.begin(), list.end(), lessComparator);
result = p != e ? *p : InternalValue();
result = p != e ? ProtectedValue(*p) : InternalValue();
break;
}
case MinItemMode:
{
auto b = list.begin();
auto e = list.end();
auto p = std::min_element(b, e, lessComparator);
result = p != e ? *p : InternalValue();
result = p != e ? ProtectedValue(*p) : InternalValue();
break;
}
case ReverseMode:
Expand All @@ -553,7 +559,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
auto size = listSize.value();
InternalValueList resultList(size);
for (std::size_t n = 0; n < size; ++n)
resultList[size - n - 1] = list.GetValueByIndex(n);
resultList[size - n - 1] = ProtectedValue( list.GetValueByIndex(n) );
result = ListAdapter::CreateAdapter(std::move(resultList));
}
else
Expand All @@ -562,7 +568,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
auto it = list.begin();
auto end = list.end();
for (; it != end; ++it)
resultList.push_back(*it);
resultList.push_back( ProtectedValue(*it) );

std::reverse(resultList.begin(), resultList.end());
result = ListAdapter::CreateAdapter(std::move(resultList));
Expand Down Expand Up @@ -625,7 +631,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
std::stable_sort(items.begin(), items.end(), [](auto& i1, auto& i2) { return i1.idx < i2.idx; });

for (auto& i : items)
resultList.push_back(list.GetValueByIndex(i.idx));
resultList.push_back( ProtectedValue( list.GetValueByIndex(i.idx) ));

result = ListAdapter::CreateAdapter(std::move(resultList));
break;
Expand Down Expand Up @@ -656,6 +662,12 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
if (!isConverted)
return result;

auto ProtectedValue = [&baseVal](InternalValue value) {
if (baseVal.ShouldExtendLifetime())
value.SetParentData(baseVal);
return value;
};

InternalValue sliceLengthValue = GetArgumentValue("slices", context);
int64_t sliceLength = ConvertToInt(sliceLengthValue);
InternalValue fillWith = GetArgumentValue("fill_with", context);
Expand All @@ -673,7 +685,7 @@ InternalValue Slice::Filter(const InternalValue& baseVal, RenderContext& context
sublist.clear();
sublistItemIndex %= sliceLength;
}
sublist.push_back(item);
sublist.push_back( ProtectedValue(item) );
++sublistItemIndex;
}
if (!IsEmpty(fillWith))
Expand Down Expand Up @@ -707,7 +719,13 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)

InternalValueList resultList;
resultList.reserve(linecount);


auto ProtectedValue = [&baseVal](InternalValue value) {
if (baseVal.ShouldExtendLifetime())
value.SetParentData(baseVal);
return value;
};

const auto remainder = elementsCount % linecount;
const auto columns = elementsCount / linecount + (remainder > 0 ? 1 : 0);
for (std::size_t line = 0, idx = 0; line < linecount; ++line)
Expand All @@ -718,7 +736,7 @@ InternalValue Slice::Batch(const InternalValue& baseVal, RenderContext& context)
std::fill_n(std::back_inserter(row), columns, fillWith);

for (std::size_t column = 0; column < elems; ++column)
row[column] = list.GetValueByIndex(idx++);
row[column] = ProtectedValue( list.GetValueByIndex(idx++) );

resultList.push_back(ListAdapter::CreateAdapter(std::move(row)));
}
Expand Down
Loading