But the point seems invalid. There is no extra cost. Apple does make the test, for no other purpose than to abort. Everyone pays that price. And what do they get for it: users of obsolete code get aborts. Authors of obsolute code have to do tedious debugging for lack of a proper diagnostic message. And users of perfectly compliant programs get a slowdown.wgarvin wrote:I think maybe you missed the point... The optimization that Ronald reported, where his compiler replaced the two strcpy calls with a strpcpy and a memcpy -- THAT optimization, and probably some similar other optimizations it has, only works if the two strings don't overlap. THAT is what you're giving up if you decide to change the API specification of strcpy so that strings may now overlap. That optimization becomes unsafe and has to be disabled, or at least gated behind a run-time test and alternate codepath, with significant extra costs.
None of that would have been needed if they had simply skipped the test, and let the UB run its course. But given that they do the test, they could just as eaisly have the code path that failed the test just refrain from the optimization. Users of compliant programs would not even notice that, as their control flows along the other path, which could still safely contain all these optimizations. There is no 'extra cost' for them, other than the cost that Apple charges them to pester users of obsolete code. And the other code path could have perfectly and 100% securely handled all other cases too. Then at least the users would have gotten something back for the price they payed, in terms of increased reliability and security.
Not true, as demonstrated above.Several times during the debate, you or bob have claimed that there was no possible performance benefit to forbidding overlapping copies, becuase of Linus's argument that memmove could be implemented as efficiently as memcpy (at least for the non-overlapping cases). This example from Ronald convincingly refutes that argument.
I think you are refuting the wrong point. The issue is not whether UB in the strcpy specs could be good. It is whether the Apple implementation, to test for overlap and abort if it finds it, brings anything for anyone. All optimizations you mention here could still be done without testing for overlap (and thus without aborting) at better performance in every compliant case.Its a nice performance optimization that is only possible because the length of the string is known not to change, which is only easy to know because the two strings don't overlap. So 25 years ago, the C spec was written to forbid overlapping copies, by declaring them as undefined behavior. And here we see an actual clever compiler optimization that can actually make real-world programs faster, and is only possible because of that restriction. It seems to me to be a clear and convincing demonstration of the value that such restrictions contribute to the possible performance.
OTOH, the must-not-overlap restriction and similar other UB restrictions (signed overflow etc.) do also come with a real-world cost: they confuse programmers, or programmers forget about them, or just accidently violate them without noticing. And then we get UB and broken programs, which is obviously bad. I'm not trying to claim the optimization benefit necessarily outweighs these bad costs. But I do think the argument that "there is zero benefit" has been convincingly refuted now.