summaryrefslogtreecommitdiffstats
path: root/jv.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-08-09 19:04:34 -0500
committerNicolas Williams <nico@cryptonector.com>2014-08-09 19:15:50 -0500
commit20ca21cb0d0025c69b8bf41d4d93541c289a8ddf (patch)
treeb9882e11e91e5b465be59e25ae3165a82c7de1b4 /jv.c
parent2d1a75f550a14cb18cc85014430d33aa1c617a5b (diff)
Out of bounds negative array indices should raise
Diffstat (limited to 'jv.c')
-rw-r--r--jv.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/jv.c b/jv.c
index bf7a0c51..cdd01630 100644
--- a/jv.c
+++ b/jv.c
@@ -230,13 +230,9 @@ static jv* jvp_array_read(jv a, int i) {
}
static jv* jvp_array_write(jv* a, int i) {
+ assert(i >= 0);
jvp_array* array = jvp_array_ptr(*a);
- if (i < 0)
- i = array->length + i;
- if (i < 0)
- i = 0;
-
int pos = i + jvp_array_offset(*a);
if (pos < array->alloc_length && jvp_refcnt_unshared(a->u.ptr)) {
// use existing array space
@@ -325,6 +321,14 @@ jv jv_array_get(jv j, int idx) {
jv jv_array_set(jv j, int idx, jv val) {
assert(jv_get_kind(j) == JV_KIND_ARRAY);
+
+ if (idx < 0)
+ idx = jvp_array_length(j) + idx;
+ if (idx < 0) {
+ jv_free(j);
+ jv_free(val);
+ return jv_invalid_with_msg(jv_string("Out of bounds negative array index"));
+ }
// copy/free of val,j coalesced
jv* slot = jvp_array_write(&j, idx);
jv_free(*slot);