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 1sVunb-00CILW-9v for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jul 2024 15:18:03 +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 1sVunZ-003SQY-Fh for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jul 2024 15:18:01 +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 1sVunZ-003SQQ-6J for pgsql-hackers@lists.postgresql.org; Mon, 22 Jul 2024 15:18:01 +0000 Received: from mail-io1-xd30.google.com ([2607:f8b0:4864:20::d30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sVunX-000tyL-CP for pgsql-hackers@lists.postgresql.org; Mon, 22 Jul 2024 15:18:01 +0000 Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-8036ce6631bso177123539f.1 for ; Mon, 22 Jul 2024 08:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721661477; x=1722266277; darn=lists.postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=ciA+5VlNTqO+381iqOL715iz5rBAVYxlgmutsThSe9Y=; b=B3tpj2oiZJwCrw8k/IDsb9GgycfkOyIEwW3+qgmgTNHnJWSfB02LBGPoyljJwDDiMg mLz5XADS7p6frcXdFq0DwprvenHCyQXv4nEIlsPWrdzqKxQT2za0OM8QB0b9KNQs8W6c TpPPqz321DxU+rDIB/td4/lKZi9q7+Aqk8U7ATMXs7sPKYubMzoek18Ogm3TsqnfVBdL hd6nYmgZCeN0xkL1Yt0RidAu8IQoWMYtNEOG6vWQH1kZNjrB4IWrivku/+ozOrtBvB3+ E5nSyM3+wJpgkEmAJRsSqJE5sYS210J2X9WSOGol8GD8ajiXguA4WSLavQsom99Ue28Z eUyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721661477; x=1722266277; h=in-reply-to:content-transfer-encoding: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=ciA+5VlNTqO+381iqOL715iz5rBAVYxlgmutsThSe9Y=; b=nV3Jt8rDtyiOJTNdm5q48EuMLptdvZW1NDxEH++ZN/E2CHtP3wYOvUVuWYXJMfOYfV tqeikT6ujfS2fsyx5Cec4UBIzvKwmScGgqoDWANxNALANfNwERPQ7Pj7mzyvf2pdNoHG 8CgkAqSmJt+5bsFHXlrv0F4abVi83fr2YL2Ou3TXijnuctqX+iurbHJ47n7pR0SqdZ+I 4Q+tUhIVcbE8+I0QZrpsa2zW7e/isIQutVk7VN8fchmneXVasBpPD/QOaklitn68ZXjd 3HwC3w5su0n+4Vkkx04Q2F9FyTL3/5li17C9UYlNgfUEuIyaIlbgJcaeObJDSvycJ10E oIyA== X-Forwarded-Encrypted: i=1; AJvYcCX2Dqnx2n9LZJeMA6aWyrrXYToY2ctq5YtMbDYwBLDHLU/g4gcPGRFEBpVTKWwAvs8VN7CRMclb9R0/gquXNrab1+cTGLjmQIJLAOT18sKI6qh0 X-Gm-Message-State: AOJu0YyJYuDWR4YVWUlcibMWwIHOZthaH/uPOsatTmcHvVjO1Qfan4H9 qRt41pL4t2hkj1LsiPyZhX2BxzKvdMyB1K1uarwbYQ40ZKSOgLch X-Google-Smtp-Source: AGHT+IHCUAMl1Iiw91NuMb4ZdE+XNBDatycc2936N0MS3/mupMAeqlrkPVAdkjx2ijvhId8343E7Ag== X-Received: by 2002:a05:6602:1510:b0:7fb:790c:a317 with SMTP id ca18e2360f4ac-81ea3668181mr16729639f.6.1721661477525; Mon, 22 Jul 2024 08:17:57 -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-4c2343aa933sm1674850173.99.2024.07.22.08.17.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 08:17:57 -0700 (PDT) Date: Mon, 22 Jul 2024 10:17:55 -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: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jul 19, 2024 at 07:32:18PM -0400, Joseph Koshakow wrote: > On Fri, Jul 19, 2024 at 2:45 PM Nathan Bossart > wrote: >> + /* dim[i] = 1 + upperIndx[i] - lowerIndx[i]; */ >> + if (pg_add_s32_overflow(1, upperIndx[i], &dim[i])) >> + ereport(ERROR, >> + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), >> + errmsg("array upper bound is too large: %d", >> + upperIndx[i]))); >> + if (pg_sub_s32_overflow(dim[i], lowerIndx[i], &dim[i])) >> + ereport(ERROR, >> + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), >> + errmsg("array size exceeds the maximum allowed > (%d)", >> + (int) MaxArraySize))); >> >> I think the problem with fixing it this way is that it prohibits more than >> is necessary. > > My understanding is that 2147483647 (INT32_MAX) is not a valid upper > bound, which is what the first overflow check is checking. Any query of > the form > `INSERT INTO arroverflowtest(i[:2147483647]) VALUES ('{...}');` > will fail with an error of > `ERROR: array lower bound is too large: ` > > The reason is the following bounds check found in arrayutils.c > > /* > * Verify sanity of proposed lower-bound values for an array > * > * The lower-bound values must not be so large as to cause overflow when > * calculating subscripts, e.g. lower bound 2147483640 with length 10 > * must be disallowed. We actually insist that dims[i] + lb[i] be > * computable without overflow, meaning that an array with last > subscript > * equal to INT_MAX will be disallowed. I see. I'm still not sure that this is the right place to enforce this check, especially given we explicitly check the bounds later on in construct_md_array(). Am I understanding correctly that the main behavioral difference between these two approaches is that users will see different error messages? -- nathan