title: stack corruption detected when use memset in c++ from JNI Android
body: <p>I am developing an Android application using C++ native code.</p>
  <p>I have C++ code (XTTEA Algorithm in C++ native) which perfectly runs online with C++ compiler and I can get the output, but when I try to use that class method using JNI cpp class, it give me the error below:</p>
  <pre class="lang-none prettyprint-override"><code>A/libc: stack corruption detected (-fstack-protector)
  A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 13328 (example.ndkdemo), pid 13328 (example.ndkdemo)
  </code></pre>
  <p>Below is the JNI cpp code where I pass the data as input.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>extern &quot;C&quot; JNIEXPORT jint JNICALLJava_com_example_ndkdemo_MainActivity_encryptDataInt(JNIEnv *env, jobject thiz /* this */){
  //    return add(5,3);
  
      int len;
  
      unsigned char input[8] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
  
      sg_xxtea_encrypt (input, 8);
  
      return 1;
  }
  </code></pre>
  <p>Below is the method that I call from above JNI code. I debug the code and find out that the error occurs in the <code>memset()</code> function inside the first <code>if</code> conditon.</p>
  <pre class="lang-cpp prettyprint-override"><code>int
  sg_xxtea_encrypt (unsigned char *data, int data_size)
  {
      int i = 0;
      int block_size = ((data_size + 2) / 4 + 1) * 4;
      memset (sg_data_buff, 0, ENCRYPT_INT_BUFF * 4);
  
      if (block_size &gt; data_size)
      {
          memset (&amp;data[data_size], 0, block_size - data_size); // We get error in this line
      }
      for (i = data_size + 1; i &gt;= 2; i--)
      {
          data[i] = data[i - 2];
      }
      data[0] = (data_size &gt;&gt; 8) &amp; 0xff;
      data[1] = (data_size &gt;&gt; 0) &amp; 0xff;
      for (i = 0; i &lt; block_size / 4; i++)
      {
          sg_data_buff[i] = bytesTOint (&amp;data[i * 4]);
      }
      TEA_EncryptCore (block_size, sg_data_buff, sg_key_buff);
      for (i = 0; i &lt; block_size / 4; i++)
      {
          intTobyte (sg_data_buff[i], &amp;data[i * 4]);
      }
      return block_size;
  }
  </code></pre>
  <p>Can anyone please suggest what could be the issue.</p>