From 5254ca22f3dcfa3bd1993d85e57d68ba1a4f4f34 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 24 Oct 2010 10:15:31 +0200 Subject: [PATCH] Fix a memcpy() warning under Valgrind Fix warning: "Source and destination overlap in memcpy" This happens when we call multiple time do_move() with the same state, for instance when we don't need to undo the move. This is what valgrind docs say: You don't want the two blocks to overlap because one of them could get partially overwritten by the copying. You might think that Memcheck is being overly pedantic reporting this in the case where 'dst' is less than 'src'. For example, the obvious way to implement memcpy() is by copying from the first byte to the last. However, the optimisation guides of some architectures recommend copying from the last byte down to the first. Also, some implementations of memcpy() zero 'dst' before copying, because zeroing the destination's cache line(s) can improve performance. In addition, for many of these functions, the POSIX standards have wording along the lines "If copying takes place between objects that overlap, the behavior is undefined." Hence overlapping copies violate the standard. The moral of the story is: if you want to write truly portable code, don't make any assumptions about the language implementation. Signed-off-by: Marco Costalba --- src/position.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/position.cpp b/src/position.cpp index 23ffbaa4..be9ece11 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -765,7 +765,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI Value npMaterial[2]; }; - memcpy(&newSt, st, sizeof(ReducedStateInfo)); + if (&newSt != st) + memcpy(&newSt, st, sizeof(ReducedStateInfo)); + newSt.previous = st; st = &newSt; -- 2.39.2