Skip to content

Commit 94aaa79

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 da7edca commit 94aaa79

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
@@ -839,8 +839,15 @@ write_relmap_file(bool shared, RelMapFile *newmap,
839839
}
840840
}
841841

842-
/* Success, update permanent copy */
843-
memcpy(realmap, newmap, sizeof(RelMapFile));
842+
/*
843+
* Success, update permanent copy. During bootstrap, we might be working
844+
* on the permanent copy itself, in which case skip the memcpy() to avoid
845+
* invoking nominally-undefined behavior.
846+
*/
847+
if (realmap != newmap)
848+
memcpy(realmap, newmap, sizeof(RelMapFile));
849+
else
850+
Assert(!send_sinval); /* must be bootstrapping */
844851

845852
/* Critical section done */
846853
if (write_wal)

0 commit comments

Comments
 (0)