Skip to content

Commit 4372adf

Browse files
Fix possible NULL pointer dereference in GetNamedDSMSegment().
GetNamedDSMSegment() doesn't check whether dsm_attach() returns NULL, which creates the possibility of a NULL pointer dereference soon after. To fix, emit an ERROR if dsm_attach() returns NULL. This shouldn't happen, but it would be nice to avoid a segfault if it does. In passing, tidy up the surrounding code. Reported-by: Tom Lane Reviewed-by: Michael Paquier, Bharath Rupireddy Discussion: https://postgr.es/m/3348869.1705854106%40sss.pgh.pa.us
1 parent cdd8634 commit 4372adf

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/backend/storage/ipc/dsm_registry.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,22 @@ GetNamedDSMSegment(const char *name, size_t size,
177177
(errmsg("requested DSM segment size does not match size of "
178178
"existing segment")));
179179
}
180-
else if (!dsm_find_mapping(entry->handle))
180+
else
181181
{
182-
/* Attach to existing segment. */
183-
dsm_segment *seg = dsm_attach(entry->handle);
182+
dsm_segment *seg = dsm_find_mapping(entry->handle);
183+
184+
/* If the existing segment is not already attached, attach it now. */
185+
if (seg == NULL)
186+
{
187+
seg = dsm_attach(entry->handle);
188+
if (seg == NULL)
189+
elog(ERROR, "could not map dynamic shared memory segment");
190+
191+
dsm_pin_mapping(seg);
192+
}
184193

185-
dsm_pin_mapping(seg);
186194
ret = dsm_segment_address(seg);
187195
}
188-
else
189-
{
190-
/* Return address of an already-attached segment. */
191-
ret = dsm_segment_address(dsm_find_mapping(entry->handle));
192-
}
193196

194197
dshash_release_lock(dsm_registry_table, entry);
195198
MemoryContextSwitchTo(oldcontext);

0 commit comments

Comments
 (0)