title: sprintf stack buffer overflow
body: <p>I'm trying to store a string in a char variable using sprintf. The code compiles, but when I run it I get a stack buffer overflow error. My compiler gives me information about why there was an error, but I can't tell what is actually wrong.</p>
  
  <p>int numbers[] is an array with length 6, and matchHighest is an integer = 0.</p>
  
  <p>I called match6 in this function:</p>
  
  <pre><code>int match(int numbers[], int matchHighest){
      int matchArray[] = {0, 0, 0, 0, 0};
      int i = 0;
      char m6[100] = "";
      char *m6p = m6;
      match6(&amp;numbers[i], matchArray, &amp;m6[100]);   
  </code></pre>
  
  <p>Here is where the error is occuring:</p>
  
  <pre><code>int match6 (int numbers[], int matchArray[5], char *m6){
      int i=0;
      while((numbers[i]==numbers[i+1]) &amp;&amp; (i&lt;5)){
          i++;
      }
      if(i == 5){
          matchArray[4] = 6 * numbers[0] + 27;
          sprintf(m6, "Rule match-6(%d) - score %d", numbers[0], matchArray[4]);
          printf("%s\n", m6);
      }
      return matchArray[4];
  }
  </code></pre>
  
  <p>When it runs, I get this error (all the values at the bottom are correct and as expected):</p>
  
  <p>draft6.c:98 runtime error - stack buffer overflow</p>
  
  <p>dcc explanation: access past the end of a local variable.
    Make sure the size of your array is correct.
    Make sure your array indices are correct.</p>
  
  <p>Execution stopped here in match6(4) - score 51") in draft6.c at line 98:</p>
  
  <pre><code>if(i == 5){
      matchArray[4] = 6 * numbers[0] + 27;
  --&gt;     sprintf(m6, "Rule match-6(%d) - score %d", numbers[0], matchArray[4]);
      printf("%s\n", m6);
  }
  </code></pre>
  
  <p>Values when execution stopped:</p>
  
  <pre><code>i = 5
  m6 = "Rule match-6(4) - score 51"
  matchArray[4] = 51
  numbers[0] = 4
  </code></pre>