|
Here you go: int main(int argc, char *argv[])
{
int offset = -63;
char input[] = { 1, 1, 1 };
char *output = input + offset;
safercopy(3, output, 3, input);
return 0;
}
This is running on a Mac with 10.10.1 and Xcode 6.1.1, compiled without optimizations. The offset value may need to be different on other architectures. With optimizations on, the approach may need to change. Don't give me any guff about the conditions needed, since that's the whole point of undefined behavior: it depends on context that should be irrelevant.There's no need to run it for 24 hours. Just run it, then pause in the debugger and step through a few loops. It'll be evident that nothing changes. If you need help getting it to work properly on your own setup, let me know. |
https://gist.github.com/zedshaw/64b3fb6b7ed653852619
I officially concede that because you can work two pointers on a computer to overwrite another location of memory to alter a for-loop (incidentally, there's not UB listed in ANSI for 'alter the variable of a for-loop') that everyone should go back to writing their C code just as K&R intended.
Please, you all should rely on only the '\0' byte terminator of all strings, don't do any bounds checking, don't check the return code of functions, and you will be totally safe.
Because, UB means "I ain't gotta fix it."
Enjoy, now I'm going painting.