Skip to content

Commit 21b2ee6

Browse files
committed
Avoid memcpy() with same source and destination during relmapper init.
A narrow reading of the C standard says that memcpy(x,x,n) is undefined, although it's hard to envision an implementation that would really misbehave. However, analysis tools such as valgrind might whine about this; accordingly, let's band-aid relmapper.c to not do it. See also 5b63050, d3f4e8a, ad7b48e, and other similar fixes. Apparently, none of those folk tried valgrinding initdb? This has been like this for long enough that I'm surprised it hasn't been reported before. Back-patch, just in case anybody wants to use a back branch on a platform that complains about this; we back-patched those earlier fixes too. Discussion: https://postgr.es/m/161790.1608310142@sss.pgh.pa.us
1 parent 48cbed8 commit 21b2ee6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/backend/utils/cache/relmapper.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,15 @@ write_relmap_file(bool shared, RelMapFile *newmap,
847847
}
848848
}
849849

850-
/* Success, update permanent copy */
851-
memcpy(realmap, newmap, sizeof(RelMapFile));
850+
/*
851+
* Success, update permanent copy. During bootstrap, we might be working
852+
* on the permanent copy itself, in which case skip the memcpy() to avoid
853+
* invoking nominally-undefined behavior.
854+
*/
855+
if (realmap != newmap)
856+
memcpy(realmap, newmap, sizeof(RelMapFile));
857+
else
858+
Assert(!send_sinval); /* must be bootstrapping */
852859

853860
/* Critical section done */
854861
if (write_wal)

0 commit comments

Comments
 (0)