In an interview, I was asked to write an implementation of strcpy and then fix it so that it properly handles overlapping strings. My implementation is below and it is very naiv
if (a>= b && a <= b+strlen(b))) || (b+strlen(b) >= a && b+strlen(b) <= a + strlen(b))
(*) you should cache strlen(b) to improve performance
What it does:
checks if the a+len
[address of a + extra len bytes] is inside the string, or a
[address of a] is inside the string, these are the only possibilities for a string overlapping.