strcpy在ios​​7上表现不同

Mar*_*rre 3 c string crash strcpy ios7

IOS7似乎带有字符串strcpy的新实现(可能是优化).之前我能够从阵列的任何位置复制字符串,但现在如果我从任何位置开始复制(i%4!= 0)它将崩溃.

为了表明这一点,我在iOS6和7中运行了这个代码,它在7上崩溃了应用程序:

  char *x = malloc(1024);
  strcpy(x, "hello world");
  char *x2 = x + 1;
  strcpy(x, x2);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

ver*_*ose 6

C11标准在§7.24.2.3中说:

The strcpy function copies the string pointed to by s2 (including the terminating 
null character) into the array pointed to by s1. If copying takes place between 
objects that overlap, the behavior is undefined.
Run Code Online (Sandbox Code Playgroud)

未定义的行为意味着任何事情都可能发生 - 代码可以完美地工作,它可以崩溃,或者它可以在一天工作正常并在下一个崩溃.由于代码中存在xx2重叠,因此在iOS 6中工作的事实只是抽奖的运气.