Skip to content

Commit 4004640

Browse files
author
ilnurkh
committed
fix coredump by select + first
commit_hash:351845650cfb323aceb7b5ee0157adaba578705a
1 parent 87f9713 commit 4004640

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--- a/src/filters.cpp (index)
2+
+++ b/src/filters.cpp (working tree)
3+
@@ -481,7 +481,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
4+
switch (m_mode)
5+
{
6+
case FirstItemMode:
7+
- if (listSize)
8+
+ if (listSize && *listSize > 0)
9+
result = list.GetValueByIndex(0);
10+
else
11+
{
12+
@@ -491,7 +491,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
13+
}
14+
break;
15+
case LastItemMode:
16+
- if (listSize)
17+
+ if (listSize && *listSize > 0)
18+
result = list.GetValueByIndex(listSize.value() - 1);
19+
else
20+
{
21+
@@ -502,7 +502,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
22+
}
23+
break;
24+
case LengthMode:
25+
- if (listSize)
26+
+ if (listSize && *listSize > 0)
27+
result = static_cast<int64_t>(listSize.value());
28+
else
29+
result = static_cast<int64_t>(std::distance(list.begin(), list.end()));
30+
@@ -511,7 +511,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
31+
{
32+
std::random_device rd;
33+
std::mt19937 gen(rd());
34+
- if (listSize)
35+
+ if (listSize && *listSize > 0)
36+
{
37+
std::uniform_int_distribution<> dis(0, static_cast<int>(listSize.value()) - 1);
38+
result = list.GetValueByIndex(dis(gen));
39+
@@ -548,7 +548,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
40+
}
41+
case ReverseMode:
42+
{
43+
- if (listSize)
44+
+ if (listSize && *listSize > 0)
45+
{
46+
auto size = listSize.value();
47+
InternalValueList resultList(size);

contrib/libs/jinja2cpp/src/filters.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
481481
switch (m_mode)
482482
{
483483
case FirstItemMode:
484-
if (listSize)
484+
if (listSize && *listSize > 0)
485485
result = list.GetValueByIndex(0);
486486
else
487487
{
@@ -491,7 +491,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
491491
}
492492
break;
493493
case LastItemMode:
494-
if (listSize)
494+
if (listSize && *listSize > 0)
495495
result = list.GetValueByIndex(listSize.value() - 1);
496496
else
497497
{
@@ -502,7 +502,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
502502
}
503503
break;
504504
case LengthMode:
505-
if (listSize)
505+
if (listSize && *listSize > 0)
506506
result = static_cast<int64_t>(listSize.value());
507507
else
508508
result = static_cast<int64_t>(std::distance(list.begin(), list.end()));
@@ -511,7 +511,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
511511
{
512512
std::random_device rd;
513513
std::mt19937 gen(rd());
514-
if (listSize)
514+
if (listSize && *listSize > 0)
515515
{
516516
std::uniform_int_distribution<> dis(0, static_cast<int>(listSize.value()) - 1);
517517
result = list.GetValueByIndex(dis(gen));
@@ -548,7 +548,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
548548
}
549549
case ReverseMode:
550550
{
551-
if (listSize)
551+
if (listSize && *listSize > 0)
552552
{
553553
auto size = listSize.value();
554554
InternalValueList resultList(size);

0 commit comments

Comments
 (0)