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 1ue2hM-004KZC-Vu for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 02:25:45 +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 1ue2hL-004TDW-Vz for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 02:25:44 +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 1ue2hL-004TDO-MZ for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 02:25:43 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ue2hI-00093B-1h for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 02:25:43 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-32b7f41d3e6so47291551fa.1 for ; Mon, 21 Jul 2025 19:25:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753151139; x=1753755939; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=e+kyrhbA1QdqKn//yQ/IfhG1yMluoyYvFWBnUog3p1g=; b=LixO6rxbOBzIZdnbuNu+vdZzd4aUj/KeHLaIdjJKZGTevAcSvr/Imk/DDKtZ8M2QZM skPkC6IadrbXR2HgOuFKpZ5Gd+LM7fSE7dC45ArIH0QKyQkCHh9JQQQ+HAnKz9BsXVHw rU7LPf1VaBpxZwm3r6J7FhkPYa173XPX1yIgT5CtZxUhCdH9CXC9FCXRwj8/23cQlZOi e2kKGotw0bYB3RsXCgY49laIryHOHpZEbcXCAwFc7fH8GjkvT2BXNE+HqyqOQ5azcgRy fp3UlbGA+w8rNh5Ta5xrF402X+ao+j1zkgs+F2YvwnUkmB5bXgJKtQ//41pGfqwWhvDI D/Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753151139; x=1753755939; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e+kyrhbA1QdqKn//yQ/IfhG1yMluoyYvFWBnUog3p1g=; b=IJo7OPWqg/FtK1u46bwdrMePG+EqTxofkjboWzzrs9P7gSF5spBjpLKUAvQzxM95Za t/OZPxuNhWAOeEpH0mNsh8o3UK0NInG5NkbudVbiEtkMGawcv3DftOlEpg0qeK3rpPr9 EvQlnSb+giit6YOUgNvDs526s0eAbVOU/ZS9tgbBzgdP7NDu5+n0y0X6hL9A3sEPUkKy evuq8PCRq4QS4xdk2Z8Bz/S9pbozWne/GlVwXnnqAoeKQlrjMeo7rW+PjWzQAKCtDW3t Uu32hh0dlK45ctQy5UQeHypuget75h4iF8PuGaYRaQCRIM4zxV1XQ2jwuVkRniH+vbcW 0wqg== X-Forwarded-Encrypted: i=1; AJvYcCXvhfpJGe2JpMUhozM+x/FwKR7s5fA/eTvuTEL7kmuXC6PvdHsx4ejiRIgmydaor5KWrS6g4kgcAFl7BVwZ@lists.postgresql.org X-Gm-Message-State: AOJu0YwaRMLKtKt96HRtlq3DA+NfE1hRweQ/P1t2To4/L4DJurxgY0fa teO1L0aRLUhBwD05Dr9gaKy3CLW49Zv/HGRBPbr/h3uzFAl5pRNT20BGqBmbZWtrChCgaDMk2J6 f5jQFI7Uqa1XQT46p62jWu3Ym7Koxn70= X-Gm-Gg: ASbGncuXngKb5xqOAiK4A+ccOw+4GumvnNKeM06dA/kui/2PeMCfsxtbyzPFFUzpHzC D1RqOn7ABjEHB4yfP3XDu2LKN7pFlCl1DgwtKcsK4IIPF4RAYN98L4uvVFahtmqLuIvD2zF0zBR RoOgQ4ebRAlaQkHMPaZruIWHamBcrfVp6UMA/2RJ7bU9/6h3oX93MF8T5buwCYw2C859fyaUhWZ DJNwniUx+tr03hxd+olh6Py7vqn9Z4JO9iMTEQq21tBueDJSgx7 X-Google-Smtp-Source: AGHT+IGyLX+d7O83lVuzezevxabjOwMt58hW6Gzjzv6EmnlijmstL5Sn5iJGPX5RpS9xOAlrtKfQFMyOjHBxEfhnHao= X-Received: by 2002:a05:651c:399:b0:32f:22f8:a79a with SMTP id 38308e7fff4ca-330d25372d8mr4031581fa.2.1753151138984; Mon, 21 Jul 2025 19:25:38 -0700 (PDT) MIME-Version: 1.0 References: <98e18ffc-c402-4710-ae28-6d6dbcf08e44@altlinux.org> <18bda8da-1ab0-4a4d-9aca-826610a244a8@altlinux.org> In-Reply-To: From: David Rowley Date: Tue, 22 Jul 2025 14:25:27 +1200 X-Gm-Features: Ac12FXxQ6wSRjz_d2G77k62l8e2AOS-GWHayyvG1nR_Fv_pOU8tHbg1xCXpLsOE Message-ID: Subject: Re: [PATCH] Check for TupleTableSlot nullness before dereferencing To: Jacob Champion Cc: kuznetsovam@altlinux.org, Daniel Gustafsson , PostgreSQL Developers , egori@altlinux.org, nickel@altlinux.org, Nikolay Shaplov Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 22 Jul 2025 at 10:20, Jacob Champion wrote: > > On Fri, Dec 13, 2024 at 12:54=E2=80=AFAM Alexander Kuznetsov > > 1. slot2 is NULL at line 968, > > 2. The while loop at line 971 executes once, filling slot1 (slot2 still= remains NULL), > > 3. No changes occur to slot2 thereafter, up to line 1003, > > 4. At line 1003, slot2 is swapped with slot1 (assuming numDistinctCols = > 0), > > 5. At line 1016, slot1 is dereferenced with conent of slot2 (NULL). > > assumes that (numDistinctCols > 0) can be true at the same time that > (slot2 =3D=3D NULL), but it's not clear to me that this can happen in > practice. See build_pertrans_for_aggref(), where these are assigned. I also can't see how this could happen. uniqslot is created in build_pertrans_for_aggref() when "numDistinctCols > 0", and the swap of slot1 and slot2 also only occurs when "numDistinctCols > 0". i.e. the uniqslot will always be allocated when the aggregate has DISTINCT (unless it's a presorted aggregate, in which case process_ordered_aggregate_multi() isn't used). If numDistinctCols > 0 then so is numSortCols > 0 per "numSortCols =3D numDistinctCols =3D list_length(sortlist);" in build_pertrans_for_aggref(). The code in this area gets a bit of exercise with: create table ab(a text, b text); insert into ab values('1','1'),('1','1'); set enable_presorted_aggregate=3D0; select string_agg(distinct a,b) from ab; David