title: Getting Bus Error in specific cases with this Python C Extension
body: <p>I'm learning C and also at the same time attempting to implement a Python C Extension, this works perfectly until I pass it a list that is rather large...</p>
  
  <p>Example..</p>
  
  <pre><code>&gt;&gt;&gt; import shuffle
  &gt;&gt;&gt; shuffle.riffle(range(100))
  </code></pre>
  
  <p>Works Great!</p>
  
  <pre><code>&gt;&gt;&gt; shuffle.riffle(range(1000))
  Bus Error: 10
  </code></pre>
  
  <p>Any ideas as to what my problem is?</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  static PyObject *shuffle_riffle(PyObject *self, PyObject *args)
  {
      int const MAX_STREAK = 10;
      int m, f, l, end_range, streak, *current_ptr;
      double length;
  
      PyObject * origList;
      PyObject * shuffledList;
      srand((int)time(NULL));
  
      // parse args to list
      if (! PyArg_ParseTuple( args, "O!", &amp;PyList_Type, &amp;origList) )
      {
          return NULL;
      }
  
      length = (int)PyList_Size(origList);
      current_ptr = (rand() % 2) ? &amp;f : &amp;l;
      end_range = (int)(length / 2) + (rand() % (length &gt; 10 ? (int)(.1 * length) : 2));
      shuffledList = PyList_New((int)length);
  
      for(m = 0, f = 0, l = (end_range + 1), streak = 0; m &lt; length &amp;&amp; l &lt; length &amp;&amp; f &lt; end_range + 1; m++, *current_ptr += 1)
      {
          double remaining = 1 - m / length;
          double test = rand() / (double)RAND_MAX;
  
          if (test &lt; remaining || streak &gt; MAX_STREAK)
          {
              current_ptr = (current_ptr == &amp;f ? &amp;l : &amp;f);
              streak = 0;
          }
  
          PyList_SetItem(shuffledList, m, PyList_GetItem(origList, *current_ptr));
          streak += 1;
      }
  
      // change the pointer to the one that didn't cause the for to exit
      current_ptr = (current_ptr == &amp;f ? &amp;l : &amp;f);
  
      while(m &lt; length)
      {
          PyList_SetItem(shuffledList, m, PyList_GetItem(origList, *current_ptr));
          m++;
          *current_ptr += 1;
      }
  
  
  
      return Py_BuildValue("O", shuffledList);
  
  }
  
  static PyMethodDef ShuffleMethods[] = {
      {"riffle", shuffle_riffle, METH_VARARGS, "Simulate a Riffle Shuffle on a List."},
      {NULL, NULL, 0, NULL}
  };
  
  void initshuffle(void){
      (void) Py_InitModule("shuffle", ShuffleMethods);
  }
  </code></pre>