Further reduce dependence on -fwrapv semantics in jsonb.
authorNathan Bossart <[email protected]>
Fri, 16 Aug 2024 20:06:40 +0000 (15:06 -0500)
committerNathan Bossart <[email protected]>
Fri, 16 Aug 2024 20:06:40 +0000 (15:06 -0500)
Commit 108d2adb9e missed updating a few places in the jsonb code
that rely on signed integer wrapping for correctness.  These can
also be fixed by using pg_abs_s32() to negate a signed integer
(that is known to be negative) for comparison with an unsigned
integer.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/bfff906f-300d-81ea-83b7-f2c93845e7f2%40gmail.com

src/backend/utils/adt/jsonfuncs.c

index 1f8ea51e6adfb5b731595f1c86d6c448d171139c..62a17a266798004fd36cea1c39a8e3ca0370e09a 100644 (file)
@@ -990,7 +990,7 @@ jsonb_array_element_text(PG_FUNCTION_ARGS)
    {
        uint32      nelements = JB_ROOT_COUNT(jb);
 
-       if (-element > nelements)
+       if (pg_abs_s32(element) > nelements)
            PG_RETURN_NULL();
        else
            element += nelements;
@@ -4811,7 +4811,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
 
    if (idx < 0)
    {
-       if (-idx > n)
+       if (pg_abs_s32(idx) > n)
            idx = n;
        else
            idx = n + idx;