summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-12-18 15:46:44 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2020-12-18 15:46:44 -0500
commitb70cab8a2c835f37cabcbe85ad480b7ce3889ee1 (patch)
tree70332bed9933fe40e476ec2a886dcdc176af175e /src
parent9595b06ef7363bf6ca42906e0007a82ab5da0950 (diff)
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 5b630501e, d3f4e8a8a, ad7b48ea0, 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
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/cache/relmapper.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/backend/utils/cache/relmapper.c b/src/backend/utils/cache/relmapper.c
index 8d4215cb981..e4d59774850 100644
--- a/src/backend/utils/cache/relmapper.c
+++ b/src/backend/utils/cache/relmapper.c
@@ -846,8 +846,15 @@ write_relmap_file(bool shared, RelMapFile *newmap,
}
}
- /* Success, update permanent copy */
- memcpy(realmap, newmap, sizeof(RelMapFile));
+ /*
+ * Success, update permanent copy. During bootstrap, we might be working
+ * on the permanent copy itself, in which case skip the memcpy() to avoid
+ * invoking nominally-undefined behavior.
+ */
+ if (realmap != newmap)
+ memcpy(realmap, newmap, sizeof(RelMapFile));
+ else
+ Assert(!send_sinval); /* must be bootstrapping */
/* Critical section done */
if (write_wal)