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.96) (envelope-from ) id 1w89a7-000G81-2g for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 04:23:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w89a6-003tbq-1f for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Apr 2026 04:22:58 +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.96) (envelope-from ) id 1w89a6-003tbi-0k for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 04:22:58 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w89a4-000000007ln-3PwO for pgsql-hackers@lists.postgresql.org; Thu, 02 Apr 2026 04:22:57 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 6324MmSA3190648; Thu, 2 Apr 2026 00:22:48 -0400 From: Tom Lane To: David Rowley cc: PostgreSQL Developers Subject: Re: Small and unlikely overflow hazard in bms_next_member() In-reply-to: References: Comments: In-reply-to David Rowley message dated "Thu, 02 Apr 2026 17:09:00 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3190646.1775103768.1@sss.pgh.pa.us> Date: Thu, 02 Apr 2026 00:22:48 -0400 Message-ID: <3190647.1775103768@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > When testing some ERROR code I added to ensure we don't get an > excessively large left shift value and end up with members higher than > INT32_MAX, I discovered that bms_next_member() can't handle that > value, as "prevbit++" will wrap to INT32_MIN and then we'll try to > access a negative array index, i.e. seg fault. > I appreciate that such a large member is quite unlikely, I think it's impossible, and if it's not then this is not the only place in bitmapset.c that could theoretically overflow. As an example, bms_prev_member does Assert(prevbit <= a->nwords * BITS_PER_BITMAPWORD); but if the bitmapset were large enough to accommodate INT_MAX as a member then a->nwords * BITS_PER_BITMAPWORD must overflow. I don't think we should add cycles here for this purpose. If it makes you feel better, maybe add Asserts to bms_make_singleton and bms_add_member to constrain the maximum member value to somewhat less than INT_MAX? regards, tom lane