diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-07-27 14:38:30 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-07-27 14:38:30 +0200 |
commit | c79ff390fa4696543057a25ea7bf93c7775652f5 (patch) | |
tree | 88765dbc6e87ef8a49a067b9789b4a69c354e021 /types.c | |
parent | 3315b1f31831a82176885861f78190c15c803b4b (diff) |
types: handle conversion errors when dealing with negative error indexes
Also apply the same logic to array set operations.
Fixes: 3315b1f ("types: allow negative array indexes")
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'types.c')
-rw-r--r-- | types.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -1948,7 +1948,7 @@ ucv_key_to_index(uc_value_t *val) d = ucv_double_get(val); if ((double)(int64_t)(d) != d) - return -1; + return INT64_MIN; return (int64_t)d; } @@ -1961,12 +1961,12 @@ ucv_key_to_index(uc_value_t *val) idx = strtoll(k, &e, 0); if (errno != 0 || e == k || *e != 0) - return -1; + return INT64_MIN; return idx; } - return -1; + return INT64_MIN; } uc_value_t * @@ -1980,7 +1980,7 @@ ucv_key_get(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key) if (ucv_type(scope) == UC_ARRAY) { idx = ucv_key_to_index(key); - if (idx < 0 && idx >= -INT64_MAX && llabs(idx) <= ucv_array_length(scope)) + if (idx < 0 && idx > INT64_MIN && llabs(idx) <= ucv_array_length(scope)) idx += ucv_array_length(scope); if (idx >= 0 && (uint64_t)idx < ucv_array_length(scope)) @@ -2017,6 +2017,9 @@ ucv_key_set(uc_vm_t *vm, uc_value_t *scope, uc_value_t *key, uc_value_t *val) if (ucv_type(scope) == UC_ARRAY) { idx = ucv_key_to_index(key); + if (idx < 0 && idx > INT64_MIN && llabs(idx) <= ucv_array_length(scope)) + idx += ucv_array_length(scope); + if (idx < 0 || !ucv_array_set(scope, idx, val)) return NULL; |