Skip to content

Commit

Permalink
Ignore too large indices in parse_slice
Browse files Browse the repository at this point in the history
Fixes #4127.
  • Loading branch information
faho authored and Kurtis Rader committed Jun 21, 2017
1 parent 44f2f37 commit 897dba9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector<long
}
// debug( 0, L"Push idx %d", tmp );

long i1 = tmp > -1 ? tmp : (long)array_size + tmp + 1;
long i1 = tmp > -1 ? tmp : size + tmp + 1;
pos = end - in;
while (in[pos] == INTERNAL_SEPARATOR) pos++;
if (in[pos] == L'.' && in[pos + 1] == L'.') {
Expand All @@ -667,6 +667,13 @@ static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector<long

// debug( 0, L"Push range %d %d", tmp, tmp1 );
long i2 = tmp1 > -1 ? tmp1 : size + tmp1 + 1;
// Clamp to array size, but only when doing a range,
// and only when just one is too high.
if (i1 > size && i2 > size) {
continue;
}
i1 = i1 < size ? i1 : size;
i2 = i2 < size ? i2 : size;
// debug( 0, L"Push range idx %d %d", i1, i2 );
short direction = i2 < i1 ? -1 : 1;
for (long jjj = i1; jjj * direction <= i2 * direction; jjj += direction) {
Expand Down
4 changes: 4 additions & 0 deletions tests/expansion.in
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ show "$foo[1 2]"
show $foo[1 2]
show "$foo[2 1]"
show $foo[2 1]
set -l foo a b c
show $foo[17]
show $foo[-17]
show $foo[17..18]

echo "$foo[d]"
echo $foo[d]
Expand Down
3 changes: 3 additions & 0 deletions tests/expansion.out
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@
0
1
0
0
0
0

Catch your breath

0 comments on commit 897dba9

Please sign in to comment.