Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 53d4f5f

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 2e0fedf commit 53d4f5f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/backend/utils/cache/relmapper.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,15 @@ write_relmap_file(bool shared, RelMapFile *newmap,
928928
}
929929
}
930930

931-
/* Success, update permanent copy */
932-
memcpy(realmap, newmap, sizeof(RelMapFile));
931+
/*
932+
* Success, update permanent copy. During bootstrap, we might be working
933+
* on the permanent copy itself, in which case skip the memcpy() to avoid
934+
* invoking nominally-undefined behavior.
935+
*/
936+
if (realmap != newmap)
937+
memcpy(realmap, newmap, sizeof(RelMapFile));
938+
else
939+
Assert(!send_sinval); /* must be bootstrapping */
933940

934941
/* Critical section done */
935942
if (write_wal)

0 commit comments

Comments
 (0)