Well, this then obviously must be a version of strcpy(a,b) that does NOT use the while(*a++=*b++);implementation my statement clearly referred to. The output posted by Bob is even weirder, btw, as the later printf does print strings that do not match with their strlen().wgarvin wrote:This post by Dann Corbit in the openchess thread of 2 weeks ago seems to me to be evidence that overlapping calls to strcpy can malfunction without necessarily causing a segfault.hgm wrote:Except that the programs that really suffered buffer overflow because of this already had been fixed long before. After all, doing a strcpy(a, b) as while(*a++ = *b++); can only end in two ways: either it works perfectly, or a lies within the sting b, and it will lead to an infinite repetitive string, certainly causing a segfault.
No buffer overflow would ever be caught by this measure that would not have segfaulted by itself.
It only makes a difference for the harmless cases, that worked absolutely correctly.Code: Select all
GCC gave me this: dcorbit@dcorbit /q/cc $ cat bozo.c #include <string.h> #include <stdio.h> int main(int argc, char* argv[]) { char b[32]; strcpy(b, "123456789012345"); strcpy(b + 1, b); printf("[%s]\n", b); return 0; } dcorbit@dcorbit /q/cc $ gcc -Wall -ansi -pedantic bozo.c dcorbit@dcorbit /q/cc $ ./a [1123456788012345] Look at it carefully, is it what you expected?
Still there is nothing to weaken my criticism. No matter how they implemented their strcpy(), it must test for overlap to exhibit the abort behavior in case there is overlap. Given that it does make this test, it could without any extra performance loss have made it work for any overlap, simply reversing copy direction when the overlap was detected that otherwise would cause a buffer overrun. If they felt that a safeguard was needed because of their unusual implementation (locating the end and copying back to front) might cause crashes for the opposite case as people are used to, it is still malicious to let it abort in stead. Arguments that this is preferable over buffer overruns are just beside the point: there would not have to be any buffer overruns ever, without any performance loss. They could have made it such that strcpy would make a faithful copy for any kind of overlap, for free. In stead they decided to make the program abort on cases that on most other compilers would work, and thus would be very likely to be around in abundance. They found a loophole that allowed them to inflict great damage on their customers and software suppliers while hiding behind the standard, and jumped to the occasion.
I think it would be pretty naive to think that they would have to do this only for a transition period, until 'everyone has fixed his code', and then switch back to a faster implementation that again allows the buffer overruns. As soon as they would stop enforcing this, things would of course degenerate into chaos again very quickly.