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 1rRg4L-008cQN-Ps for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Jan 2024 22:13:34 +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 1rRg4I-00GgpO-CB for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Jan 2024 22:13:30 +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 1rRg4I-00GgpF-2s for pgsql-hackers@lists.postgresql.org; Sun, 21 Jan 2024 22:13:30 +0000 Received: from mail-il1-x130.google.com ([2607:f8b0:4864:20::130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rRg4D-0031E2-56 for pgsql-hackers@postgresql.org; Sun, 21 Jan 2024 22:13:29 +0000 Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-3606f3f2f37so12573715ab.0 for ; Sun, 21 Jan 2024 14:13:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705875203; x=1706480003; darn=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=xXHh92POPmMgcDg9QghkVJx/m55tiU5L9k30ux4bjT4=; b=SJ0JwB9LKoYoOeUd4J15cRaqWHCq1uhJExiY1qvcu17f+GME1J9g0Ct+5syemCCLeO w1NaDaElCZCJooVA+4tv1k4FJWQzcl3UUq2F7aVxwOaBGsRKCIEoHbnmJipxjX5gFKXH tRo+4jTvnp8mseWw0nTalcZJrauKfomrEA3sPQUuHWMuHuVLei9KJZuAlTnMYvwvEbxE 2UfiWb3K4KdsQKszVW8twxdca9R3B8Jvu8+K9IzOLGtoG2KHb4cXLnkFZxcpx6hmEfM3 eUUi/VLQJFl/HiIq/eCJ4cTIGhaaNjJ/FHaDaEAPcD0J5aa5/xXzB6zOTJ1SDiJ9d+uz kqNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705875203; x=1706480003; 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=xXHh92POPmMgcDg9QghkVJx/m55tiU5L9k30ux4bjT4=; b=JYhoLhRUWRW4Pt/iX/Uu8Q/uFalPAEDsH3esW8og//VmcLmSRNcAfGyN0WjZ8G0EAz pPgelZpvLuod4aJfllpwOS6bi8pWRwstCVxJbHeiP5VvAaCbIW0y+ncVdaajVFZFxnv+ mw0zCIDyU4uk0qxCpVWsHNOsTxTwzoL21oEiy8RtSDhE2PuF7maAG6XJ/e2kvgK1uB9z G5v6Gk10kPg0qMQvWo93h0PPzPh/ZlgLgDcxMsNbabNc5jE7dPjxbuJZrQpwtw9TvTlt ELBer3fagSsdxvNdddq0e9ikpSkqvQpFBDe8TzfiwpRD/51YkYWmX3ZS+OX1jAe1ESjq YcOg== X-Gm-Message-State: AOJu0YxN5xk9jFPWCcxh83ql1R0EORE25FBU/lrGpo0fO1ar+dOe+csP Fm9d8qTKVwsLHVduiLL/oqp3eQ1KMFiJgxrF5+3OTDGk8PimQrE6 X-Google-Smtp-Source: AGHT+IE5XWuQ8nlle2nmncFWiQFav60LInMj9jHRu6I28597V+Frlu6psmTotQDb1Nyp9sMgvLvBpw== X-Received: by 2002:a05:6e02:4ab:b0:361:a0ba:ad09 with SMTP id e11-20020a056e0204ab00b00361a0baad09mr2260655ils.12.1705875203292; Sun, 21 Jan 2024 14:13:23 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id ck12-20020a056e02370c00b003627a7f95a7sm461553ilb.0.2024.01.21.14.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 14:13:22 -0800 (PST) Date: Sun, 21 Jan 2024 16:13:20 -0600 From: Nathan Bossart To: Tom Lane Cc: Bharath Rupireddy , Abhijit Menon-Sen , Michael Paquier , Robert Haas , pgsql-hackers@postgresql.org Subject: Re: introduce dynamic shared memory registry Message-ID: <20240121221320.GA1682397@nathanxps13> References: <20240112172152.GA3803561@nathanxps13> <20240112194555.GA3804945@nathanxps13> <20240113214124.GA4162765@nathanxps13> <20240116160748.GB100542@nathanxps13> <20240119204636.GA1178173@nathanxps13> <3348869.1705854106@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ZPt4rx8FFjLCG7dd" Content-Disposition: inline In-Reply-To: <3348869.1705854106@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sun, Jan 21, 2024 at 11:21:46AM -0500, Tom Lane wrote: > Coverity complained about this: > > *** CID 1586660: Null pointer dereferences (NULL_RETURNS) > /srv/coverity/git/pgsql-git/postgresql/src/backend/storage/ipc/dsm_registry.c: 185 in GetNamedDSMSegment() > 179 } > 180 else if (!dsm_find_mapping(entry->handle)) > 181 { > 182 /* Attach to existing segment. */ > 183 dsm_segment *seg = dsm_attach(entry->handle); > 184 >>>> CID 1586660: Null pointer dereferences (NULL_RETURNS) >>>> Dereferencing a pointer that might be "NULL" "seg" when calling "dsm_pin_mapping". > 185 dsm_pin_mapping(seg); > 186 ret = dsm_segment_address(seg); > 187 } > 188 else > 189 { > 190 /* Return address of an already-attached segment. */ > > I think it's right --- the comments for dsm_attach explicitly > point out that a NULL return is possible. You need to handle > that scenario in some way other than SIGSEGV. Oops. I've attached an attempt at fixing this. I took the opportunity to clean up the surrounding code a bit. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --ZPt4rx8FFjLCG7dd Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-fix-coverity-complaint.patch" From f4c1c7a7ce8bccf7251e384f895f34beb33f839e Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Sun, 21 Jan 2024 16:05:16 -0600 Subject: [PATCH v1 1/1] fix coverity complaint --- src/backend/storage/ipc/dsm_registry.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index ac11f51375..c178173653 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -177,19 +177,22 @@ GetNamedDSMSegment(const char *name, size_t size, (errmsg("requested DSM segment size does not match size of " "existing segment"))); } - else if (!dsm_find_mapping(entry->handle)) + else { - /* Attach to existing segment. */ - dsm_segment *seg = dsm_attach(entry->handle); + dsm_segment *seg = dsm_find_mapping(entry->handle); + + /* If the existing segment is not already attached, attach it now. */ + if (seg == NULL) + { + seg = dsm_attach(entry->handle); + if (seg == NULL) + elog(ERROR, "could not map dynamic shared memory segment"); + + dsm_pin_mapping(seg); + } - dsm_pin_mapping(seg); ret = dsm_segment_address(seg); } - else - { - /* Return address of an already-attached segment. */ - ret = dsm_segment_address(dsm_find_mapping(entry->handle)); - } dshash_release_lock(dsm_registry_table, entry); MemoryContextSwitchTo(oldcontext); -- 2.25.1 --ZPt4rx8FFjLCG7dd--