title: what happens if buffer's data type mismatches (int * versus unsigned int *) when calling a function?
body: <p>So suppose one has function:</p>
  
  <pre><code>int somefunc(int* buf)
  </code></pre>
  
  <p>but we call somefunc by:</p>
  
  <pre><code>somefunc(buf2)
  </code></pre>
  
  <p>where <code>buf2</code> is heap buffer, allocated by <code>alloca</code>, that has data of type <code>unsigned int</code> (thus pointer <code>buf2</code> is of type <code>unsigned int*</code>).</p>
  
  <p>What is the danger of doing this? What would be the problem here? I do think this might have to do with heap(buffer) overflow, but I am not sure what that would be. There may be some other problems with the code, and if so, I do want to know about these problems too.</p>
  
  <p>Example code:</p>
  
  <pre><code>void typecastfunc(FILE *p, int *buf, unsigned int index) {
      unsigned int a;
      for (a = 0; a &lt; index; a++) {
          if (fread(&amp;buf[a], sizeof(unsigned int), 1, p) &lt; 1) {
              break;
          }
      }
  }
  
  void caller(char *filen) {
      FILE *p = fopen(filen, "rb");
      if (!p) {
          return;
      }
      unsigned int index;
      fread(&amp;index, sizeof(unsigned int), 1, p);
      unsigned int *buf = alloca(index * sizeof(unsigned int));
      if (!buf) {
          return;
      }
      typecastfunc(p, buf, index);
  }
  
  int main(int argc, char *argv[]) {
      caller(argv[1]);
  }
  </code></pre>
  
  <p>The question is being asked, because by randomly fuzzing, I was able to get the signs of overflow, but I do not know how overflow can occur by type mismatch. Other codes seem harmless, so I assume it must be because of type mismatch.</p>