title: Why does this intentionally incorrect use of strcpy not fail horribly?
body: <p>Why does the below C code using <code>strcpy</code> work just fine for me? I tried to make it fail in two ways:</p>
  
  <p>1) I tried <code>strcpy</code> from a string literal into allocated memory that was too small to contain it. It copied the whole thing and didn't complain.</p>
  
  <p>2) I tried <code>strcpy</code> from an array that was not <code>NUL</code>-terminated. The <code>strcpy</code> and the <code>printf</code> worked just fine. I had thought that <code>strcpy</code> copied <code>char</code>s until a <code>NUL</code> was found, but none was present and it still stopped.</p>
  
  <p>Why don't these fail? Am I just getting "lucky" in some way, or am I misunderstanding how this function works? Is it specific to my platform (OS X Lion), or do most modern platforms work this way?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main() {
      char *src1 = "123456789";
      char *dst1 = (char *)malloc( 5 );
  
      char src2[5] = {'h','e','l','l','o'};
      char *dst2 = (char *)malloc( 6 );
  
      printf("src1: %s\n", src1);
      strcpy(dst1, src1);
      printf("dst1: %s\n", dst1);
      strcpy(dst2, src2);
      printf("src2: %s\n", src2);
      dst2[5] = '\0';
      printf("dst2: %s\n", dst2);
  
      return 0;
  }
  </code></pre>
  
  <p>The output from running this code is:</p>
  
  <pre><code>$ ./a.out   
  src1: 123456789
  dst1: 123456789
  src2: hello 
  dst2: hello
  </code></pre>