Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sei7F-00B0xC-QF for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Aug 2024 21:34:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sei7C-001Xc1-E2 for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Aug 2024 21:34:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sei7C-001Xbt-4K for pgsql-hackers@lists.postgresql.org; Thu, 15 Aug 2024 21:34:38 +0000 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sei79-00536b-9G for pgsql-hackers@lists.postgresql.org; Thu, 15 Aug 2024 21:34:37 +0000 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-70c9cda7f1cso654794a34.3 for ; Thu, 15 Aug 2024 14:34:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723757673; x=1724362473; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=J1V4VklIBVLQOjFYp9TM2pnXxItcQPiCa04UZr0rzXU=; b=ONiRMR86fBgN49zmLbF06zH6tSd9R5is59PQ3IQ9szdar2Ict2QrGbqnGWyd+Kmhyl zSvkowk/NYA67iWW6HZtZbvsFBRMMIxUx4TdHEZcvrwKC8bHC6uow3dIhVYWPV9YMyfM n4n375wE4dpqvjsrqqK8Pz77UbQDjnWRkd06ocp1oKcJwywBjRWsREuJkhAN9PdkhgQ9 cU1mf3uWUrb2tUizgI6fN7a8v763E87CpuF84+ADqycb/JMVKAU/miiFEpr1xCNBRqbV QoYOOgtz4NkNp8C4MZ45KqFjwP7D6nnxM9cjKwnehNh277jrDTB0knLUp2u7LZOCIuQI N9zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723757673; x=1724362473; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=J1V4VklIBVLQOjFYp9TM2pnXxItcQPiCa04UZr0rzXU=; b=IcG35p0OoaDKvjr/ibK+g/hN6RyM7xtPFqa88peTCG4Pl90UFWAG7uBrtefpI+RcAf IfWWPN0ZCvPQo7SRtFuxWzm5Ll9LfT7NGPmyjcImOS+BmMMSD18p/gljaBL7rZbyq8rh +iN7wRsMsRb5cMLIWRAZnMT73D5Ef35vCduoaYXtc1V941Q0bFHr3/lscaJimEC/6Joa zdzS8ZHJbAwqpY3xPNZxZCPyauR0ybYcycYo7Ii3OPNPTVaC7e7Oe/bpdQr6ACDb13JP wRCxiHSg+7kMoCBRnWSYSHMVLyifNRSFzsu4URGlrYGq4bQv1+OGzRsNrlg254InuWFD Kjwg== X-Forwarded-Encrypted: i=1; AJvYcCVMgay00O1z974F4vpfv8v+7uzY/0SOoJxoaQgb0Rk3LseFyGZ/rwE+qBVWT5ZfvSKww7wi6H8M4mIS02wCeoWB/VF3H38hZdwiRBNoLgMGDYik X-Gm-Message-State: AOJu0Yw782G3ioHQz0J+mT/ebdf5iu7iTl8b3Xz7tPs9hX4AQ65lynbP qspkERXcCBTLW3/c27e3xU+u15IoJ44dxqUOxImn+I8ZBXCK8q9M X-Google-Smtp-Source: AGHT+IFkSkbUSDNLNULdFAN8Usc671lULdIUXFWDH6anzB2c04dr+obqLGAHYHp127iEsRqFoOAEsw== X-Received: by 2002:a05:6830:6081:b0:709:3431:94c3 with SMTP id 46e09a7af769-70cac89c20amr1363630a34.23.1723757672632; Thu, 15 Aug 2024 14:34:32 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4ccd6eb71a7sm752302173.83.2024.08.15.14.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2024 14:34:32 -0700 (PDT) Date: Thu, 15 Aug 2024 16:34:30 -0500 From: Nathan Bossart To: jian he Cc: Joseph Koshakow , Heikki Linnakangas , Matthew Kim , Alexander Lakhin , Tom Lane , PostgreSQL Hackers , Andres Freund Subject: Re: Remove dependence on integer wrapping Message-ID: References: <875ad3ec-f216-487e-baad-cdbaf32c1fcf@iki.fi> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="XFoT1JusIWT5wkIc" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --XFoT1JusIWT5wkIc Content-Type: text/plain; charset=us-ascii Content-Disposition: inline I've committed 0001. Now to 0002... - if (-element > nelements) + if (element == PG_INT32_MIN || -element > nelements) This seems like a good opportunity to use our new pg_abs_s32() function, and godbolt.org [0] seems to indicate that it might produce better code, too (at least to my eye). I've attached an updated version of the patch with this change. Barring additional feedback, I plan to commit this one shortly. [0] https://godbolt.org/z/57P4vvGYf -- nathan --XFoT1JusIWT5wkIc Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v23-0001-Remove-dependence-on-integer-wrapping-for-jsonb.patch" From 62677b351cf18dee37dc0d9253bd694fe6fbf26a Mon Sep 17 00:00:00 2001 From: Joseph Koshakow Date: Sat, 6 Jul 2024 15:41:09 -0400 Subject: [PATCH v23 1/1] Remove dependence on integer wrapping for jsonb This commit updates various jsonb operators and functions to no longer rely on integer wrapping for correctness. Not all compilers support -fwrapv, so it's best not to rely on it. --- src/backend/utils/adt/jsonfuncs.c | 7 ++++--- src/test/regress/expected/jsonb.out | 12 ++++++++++++ src/test/regress/sql/jsonb.sql | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 5ecb9fffae..1f8ea51e6a 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -18,6 +18,7 @@ #include "access/htup_details.h" #include "catalog/pg_type.h" +#include "common/int.h" #include "common/jsonapi.h" #include "common/string.h" #include "fmgr.h" @@ -946,7 +947,7 @@ jsonb_array_element(PG_FUNCTION_ARGS) { uint32 nelements = JB_ROOT_COUNT(jb); - if (-element > nelements) + if (pg_abs_s32(element) > nelements) PG_RETURN_NULL(); else element += nelements; @@ -5426,7 +5427,7 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls, if (idx < 0) { - if (-idx > nelems) + if (pg_abs_s32(idx) > nelems) { /* * If asked to keep elements position consistent, it's not allowed @@ -5438,7 +5439,7 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls, errmsg("path element at position %d is out of range: %d", level + 1, idx))); else - idx = INT_MIN; + idx = PG_INT32_MIN; } else idx = nelems + idx; diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index e66d760189..a9d93052fc 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -680,6 +680,18 @@ select '"foo"'::jsonb -> 'z'; (1 row) +select '[]'::jsonb -> -2147483648; + ?column? +---------- + +(1 row) + +select jsonb_delete_path('{"a":[]}', '{"a",-2147483648}'); + jsonb_delete_path +------------------- + {"a": []} +(1 row) + select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text; ?column? ---------- diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index 97bc2242a1..6a18577ead 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -204,6 +204,8 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z'; select '{"a": "c", "b": null}'::jsonb -> 'b'; select '"foo"'::jsonb -> 1; select '"foo"'::jsonb -> 'z'; +select '[]'::jsonb -> -2147483648; +select jsonb_delete_path('{"a":[]}', '{"a",-2147483648}'); select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int; -- 2.39.3 (Apple Git-146) --XFoT1JusIWT5wkIc--