summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-06-02 18:43:09 +0200
committerJo-Philipp Wich <jo@mein.io>2021-06-02 18:49:52 +0200
commit05c80a78a3cc1371bd04575de01128b0a4bd16b0 (patch)
tree788b90cc1dd7c2e94121f6101a1e558cf517cad6
parentdaef8b35f421c5481116c27fa5132881549a76a6 (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>
-rw-r--r--lib.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/lib.c b/lib.c
index 98307f6..f0f934d 100644
--- a/lib.c
+++ b/lib.c
@@ -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;