title: Error in `python': corrupted size vs. prev_size
body: <p>I use c to write a python extension.<br/>
  I use the c program to encrypt a string.<br/>
  My c program:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  
  static const char *codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  
  static const unsigned char map[256] = {
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255,
          255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
          52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
          255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
          7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
          19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
          255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
          37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
          49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
          255, 255, 255, 255
  };
  
  int length(char *str){
      int i = 0;
      while (str[i] != '\0') i++;
      return i;
  }
  
  char *str_encrypt(char *in) {
      unsigned long len = (unsigned long)length(in);
      unsigned long index, l_even;
      char *p;
      char *out = (char *) PyMem_Malloc (len * 4 / 3 + 1);
      p = out;
      /* valid output size ? */
      l_even = 3 * (len / 3);
      for (index = 0; index &lt; l_even; index += 3) {
          *p++ = codes[in[0] &gt;&gt; 2];
          *p++ = codes[((in[0] &amp; 3) &lt;&lt; 4) + (in[1] &gt;&gt; 4)];
          *p++ = codes[((in[1] &amp; 0xf) &lt;&lt; 2) + (in[2] &gt;&gt; 6)];
          *p++ = codes[in[2] &amp; 0x3f];
          in += 3;
      }
      /* Pad it if necessary...  */
      if (index &lt; len) {
          unsigned a = (unsigned)in[0];
          unsigned b = (unsigned)((index+1 &lt; len) ? in[1] : 0);
          unsigned c = 0;
          *p++ = codes[a &gt;&gt; 2];
          *p++ = codes[((a &amp; 3) &lt;&lt; 4) + (b &gt;&gt; 4)];
          *p++ = (char)((index+1 &lt; len) ? codes[((b &amp; 0xf) &lt;&lt; 2) + (c &gt;&gt; 6)] : '=');
          *p++ = '=';
      }
      /* append a NULL byte */
      *p = '\0';
      return out;
  }
  
  static PyObject *wrap_encrypt_str(PyObject *self, PyObject *args) {
      char *input_str;
      if (!PyArg_ParseTuple(args, "s", &amp;input_str)) {
          return NULL;
      }
      char *result = str_encrypt(input_str);
      PyObject *pyObject = Py_BuildValue("s", result);
      PyMem_Free(result);
      return pyObject;
  }
  
  /* registration table  */
  static PyMethodDef wrap_methods[] ={
          {"encrypt_str", wrap_encrypt_str, METH_VARARGS},       /* method name, C func ptr, always-tuple */
          {NULL, NULL}                   /* end of table marker */
  };
  
  /* module initializer */
  PyMODINIT_FUNC initencryption(void)                       /* called on first import */
  {                                      /* name matters if loaded dynamically */
      (void) Py_InitModule("encryption", wrap_methods);   /* mod name, table ptr */
  }
  </code></pre>
  
  <p>And then I write a file named setup.py. <br/>
  The program in setup.py is here:</p>
  
  <pre><code>from setuptools import setup, Extension
  setup(
      name="extest",
      packages=["extest", "extest.math", "extest.test"],
      version="0.0.1",
      zip_safe=False,
      ext_modules=[
          Extension("extest.test.encryption", sources=['extest/test/encryption.c'], extra_compile_args=["-Wno-char-subscripts"])
      ],
      long_description='',
      classifiers=[
          "Environment :: Web Environment",
          "Intended Audience :: Developers",
          "Operating System :: OS Independent",
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 2.6',
          'Programming Language :: Python :: 2.7'
      ]
  )
  </code></pre>
  
  <p>I run <code>python setup.py install</code> and install the extension successfully.<br/>
  I import the module in python program.<br/>
  like this:</p>
  
  <pre><code>from extest.test.encryption import encrypt_str
  a = encrypt_str("asdfg")
  print a
  </code></pre>
  
  <p>run successfully.<br/>
  But when I replace <code>a = encrypt_str("asdfg")</code> with <code>a = encrypt_str("/static/hello.js")</code>, I got an error and the program crashed!
  The error info is here:</p>
  
  <pre><code>*** Error in `python': corrupted size vs. prev_size: 0x0000000000aaca00 ***
  </code></pre>
  
  <p>What is the reason? How to fix it?</p>