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 1sW1V3-00CpXR-Kd for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jul 2024 22:27:21 +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 1sW1V1-007XjO-2n for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jul 2024 22:27:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sW1V0-007XjF-PI for pgsql-hackers@lists.postgresql.org; Mon, 22 Jul 2024 22:27:19 +0000 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sW1Uy-000w1D-EW for pgsql-hackers@lists.postgresql.org; Mon, 22 Jul 2024 22:27:17 +0000 Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-7f70a708f54so208959639f.3 for ; Mon, 22 Jul 2024 15:27:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721687235; x=1722292035; 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=jL7a6WPBQPqP9fuA1amHvSO4ADuiQcd09wT2P0M4/SU=; b=gnjpK+y4ssQ9R63V0D5bUu+X+EazLXsYXkeSaMCpDBrvXtyTiNIhSe7UP29QfgZ/UN cGUYVJDMTgSMAmFof/mIpB+yfzkcY992wzJye7xahOxaJjJ+a6ZYLgIz1ocQUphtfHzN MPR4kvq5mlkquUWytpk5q3Hsl44hr5frKqPGBb0n6R1q2gKQgOmaiiHmTdJGjC7loAwd ARGLvQvIFO6LuFIomtKV2ben/mV0Xpedka0PK+vNyHyMK7kOeldsQYojPNTcMO8TUFt2 X2S09NK+iB9lOCZue0TspTU9/y3nHNT0Sg8JfDXggm6U2zaCSk9zv0nPydBl23NFqQ0d TtSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721687235; x=1722292035; 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=jL7a6WPBQPqP9fuA1amHvSO4ADuiQcd09wT2P0M4/SU=; b=Os2wVKjV/AHfVnHGnAxX+gkgKjz/eVJe6nbdVVeVq4jEUXFX6bMe+KFFPx3DyNL0Uo tumo/n5v06/y7adW7fPoVeFHRYfQEcwIJwQtTSwg/ryv1MZJhUlRdRAXE/qu22UnsebW Qic8PcHFCS6L0EacxoNIyGh/iFPoN3IEj8usqpzZGZ+0eRo3cBfFySjA5Uc8isxIwYlM fjcJEbWaxqzB8bF5zqV49AoxdxANeH2r2GmZbBckVtND1oi9+D6wexg0BsY7+neFI1+g fDrtlF9ML9brRyNIyuRuFvSV4EqUt4QjgA8W0uOAppCIKcbNe+niK1iRXMHULmcdkNXn izVw== X-Forwarded-Encrypted: i=1; AJvYcCV1cYGNIZni+YjflNDs0Y6NlP6GrAMYYSfsl9RG5/oElGBQlyRHXklT0SnaKdhyRtQn2vY8Paa+rQPYWbaT4IVAhHOYPEO/J7+SdH/R2qXr5kTp X-Gm-Message-State: AOJu0Yytq/tZCIp6M6WSBqgc9Wx8Mm8alTAZOOfmSHm6axLr8Fjo3r50 HU6moES/fTIMuNUYDLrzIQx2oIKYdlmiKQnsYufvBaSgllsKcAzJ X-Google-Smtp-Source: AGHT+IGmjAWEOdPaycXO1RPPw00ubd4Zc4MqFcj16gFFwRJ7q1zJCtaPNrHXqdZQuCnRQDlQNPANPQ== X-Received: by 2002:a6b:7d05:0:b0:803:f182:e0e8 with SMTP id ca18e2360f4ac-81ea4d836bemr136767439f.13.1721687235580; Mon, 22 Jul 2024 15:27:15 -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-4c239105e04sm1670202173.40.2024.07.22.15.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 15:27:15 -0700 (PDT) Date: Mon, 22 Jul 2024 17:27:13 -0500 From: Nathan Bossart To: Joseph Koshakow Cc: jian he , Alexander Lakhin , Tom Lane , PostgreSQL Hackers , Andres Freund Subject: Re: Remove dependence on integer wrapping Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="3HSg2+A/6oTs+Iqd" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --3HSg2+A/6oTs+Iqd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Jul 22, 2024 at 04:36:33PM -0500, Nathan Bossart wrote: > Okay. I'll plan on committing v13-0002 in the next couple of days, then. Actually, I think my concerns about prohibiting more than necessary go away if we do the subtraction first. If "upperIndx[i] - lowerIndx[i]" overflows, we know the array size is too big. Similarly, if adding one to that result overflows, we again know the the array size is too big. This appears to be how the surrounding code handles this problem (e.g., ReadArrayDimensions()). Thoughts? -- nathan --3HSg2+A/6oTs+Iqd Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v14-0001-Remove-overflow-from-array_set_slice.patch" From 8794c740492bd5113d405638b03518693dfeb0db Mon Sep 17 00:00:00 2001 From: Joseph Koshakow Date: Sat, 6 Jul 2024 14:35:00 -0400 Subject: [PATCH v14 1/1] Remove overflow from array_set_slice This commit removes an overflow from array_set_slice that allows seting absurd slice ranges. --- src/backend/utils/adt/arrayfuncs.c | 9 ++++++++- src/test/regress/expected/arrays.out | 6 ++++++ src/test/regress/sql/arrays.sql | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index d6641b570d..e5c7e57a5d 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -2887,7 +2887,14 @@ array_set_slice(Datum arraydatum, errdetail("When assigning to a slice of an empty array value," " slice boundaries must be fully specified."))); - dim[i] = 1 + upperIndx[i] - lowerIndx[i]; + /* compute "upperIndx[i] - lowerIndx[i] + 1", detecting overflow */ + if (pg_sub_s32_overflow(upperIndx[i], lowerIndx[i], &dim[i]) || + pg_add_s32_overflow(dim[i], 1, &dim[i])) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("array size exceeds the maximum allowed (%d)", + (int) MaxArraySize))); + lb[i] = lowerIndx[i]; } diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.out index 23404982f7..7301c96bba 100644 --- a/src/test/regress/expected/arrays.out +++ b/src/test/regress/expected/arrays.out @@ -2699,3 +2699,9 @@ SELECT array_sample('{1,2,3,4,5,6}'::int[], -1); -- fail ERROR: sample size must be between 0 and 6 SELECT array_sample('{1,2,3,4,5,6}'::int[], 7); --fail ERROR: sample size must be between 0 and 6 +-- overflow in array slicing +CREATE TEMP TABLE arroverflowtest (i int[]); +INSERT INTO arroverflowtest(i[0:2147483647]) VALUES ('{1}'); +ERROR: array size exceeds the maximum allowed (134217727) +INSERT INTO arroverflowtest(i[-2147483648:1]) VALUES ('{1}'); +ERROR: array size exceeds the maximum allowed (134217727) diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sql index 50aa539fdc..d6ef059a1d 100644 --- a/src/test/regress/sql/arrays.sql +++ b/src/test/regress/sql/arrays.sql @@ -825,3 +825,8 @@ SELECT array_dims(array_sample('[-1:2][2:3]={{1,2},{3,NULL},{5,6},{7,8}}'::int[] SELECT array_dims(array_sample('{{{1,2},{3,NULL}},{{5,6},{7,8}},{{9,10},{11,12}}}'::int[], 2)); SELECT array_sample('{1,2,3,4,5,6}'::int[], -1); -- fail SELECT array_sample('{1,2,3,4,5,6}'::int[], 7); --fail + +-- overflow in array slicing +CREATE TEMP TABLE arroverflowtest (i int[]); +INSERT INTO arroverflowtest(i[0:2147483647]) VALUES ('{1}'); +INSERT INTO arroverflowtest(i[-2147483648:1]) VALUES ('{1}'); -- 2.39.3 (Apple Git-146) --3HSg2+A/6oTs+Iqd--