summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Dolan <mu@netsoc.tcd.ie>2013-05-15 00:22:44 +0100
committerStephen Dolan <mu@netsoc.tcd.ie>2013-05-15 00:22:44 +0100
commitc496a924ce71317aa5560da0cbf5a4524bb8c226 (patch)
tree1144c05d96cb7c014bf31501de10a19c31b69e85
parent81be37b23601a948164f0eb4a4f0331fa83ccf15 (diff)
Bugfix for array slices.
-rw-r--r--jv_aux.c3
-rw-r--r--tests/all.test4
2 files changed, 5 insertions, 2 deletions
diff --git a/jv_aux.c b/jv_aux.c
index 28755644..68811cd0 100644
--- a/jv_aux.c
+++ b/jv_aux.c
@@ -26,9 +26,12 @@ static int parse_slice(jv array, jv slice, int* pstart, int* pend) {
int end = (int)jv_number_value(end_jv);
if (start < 0) start = len + start;
if (end < 0) end = len + end;
+
if (start < 0) start = 0;
+ if (start > len) start = len;
if (end > len) end = len;
if (end < start) end = start;
+ assert(0 <= start && start <= end && end <= len);
*pstart = start;
*pend = end;
return 1;
diff --git a/tests/all.test b/tests/all.test
index 4eaa9b61..0119dad6 100644
--- a/tests/all.test
+++ b/tests/all.test
@@ -170,9 +170,9 @@ null
# Slices
#
-[.[3:2], .[-5:4], .[:-2], .[-2:]]
+[.[3:2], .[-5:4], .[:-2], .[-2:], .[3:3][1:], .[10:]]
[0,1,2,3,4,5,6]
-[[], [2,3], [0,1,2,3,4], [5,6]]
+[[], [2,3], [0,1,2,3,4], [5,6], [], []]
del(.[2:4],.[0],.[-2:])
[0,1,2,3,4,5,6,7]