diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-06-02 18:43:09 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-06-02 18:49:52 +0200 |
commit | 05c80a78a3cc1371bd04575de01128b0a4bd16b0 (patch) | |
tree | 788b90cc1dd7c2e94121f6101a1e558cf517cad6 /lib.c | |
parent | daef8b35f421c5481116c27fa5132881549a76a6 (diff) |
lib: fix negative uc_index() return value on 32bit systems
The numeric return value was incorrectly stored in an unsigned size_t
variable which got later wrapped in an ucode signed 64bit integer value.
This worked by accident on 64bit systems since (int64_t)(size_t)(-1) == -1,
but it failed on 32bit ones where (int64_t)(size_t)(-1) yields 4294967295
due to the different sizes of the size_t and int64_t types.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -321,14 +321,15 @@ uc_index(uc_vm *vm, size_t nargs, bool right) { uc_value_t *stack = uc_get_arg(0); uc_value_t *needle = uc_get_arg(1); - size_t arridx, len, ret = -1; const char *sstr, *nstr, *p; + size_t arridx, len; + ssize_t ret = -1; switch (ucv_type(stack)) { case UC_ARRAY: for (arridx = 0, len = ucv_array_length(stack); arridx < len; arridx++) { if (uc_cmp(TK_EQ, ucv_array_get(stack, arridx), needle)) { - ret = arridx; + ret = (ssize_t)arridx; if (!right) break; @@ -344,7 +345,7 @@ uc_index(uc_vm *vm, size_t nargs, bool right) for (p = sstr; *p && len; p++) { if (!strncmp(p, nstr, len)) { - ret = p - sstr; + ret = (ssize_t)(p - sstr); if (!right) break; |