title: Valgrind invalid write size of 8
body: <p>I have some code that is suppose to break apart a string that looks like the example below and store it into a data structure. </p>
  
  <pre><code>ORGANIZER;CN=John Doe;ON=Another Person;SN=Maybe another
  </code></pre>
  
  <p>Here is what my function looks like:</p>
  
  <pre><code>CalError parseOptionalParam(char * paramString, CalParam * param) {
    char * parseString = malloc(strlen(paramString) + sizeof(char*));
    strcpy(parseString, paramString);
  
    char * tokenSemi;
    tokenSemi = strtok(parseString, ";");
    if(tokenSemi == NULL) return SYNTAX;
    int i = 0;
    while(tokenSemi != NULL) {
      tokenSemi = strtok(NULL, ";");
      if(tokenSemi == NULL) return SYNTAX;
  
      char * tokenEqual = strtok(tokenSemi, "=");
      param-&gt;name = malloc(strlen(tokenEqual) + sizeof(char*));
      strcpy(param-&gt;name, tokenEqual);
  
      param = realloc(param, sizeof(param) + sizeof(char*));
      tokenEqual = strtok(tokenSemi, "=");
      param-&gt;value[i] = malloc(sizeof(char*) + strlen(tokenEqual));
      strcpy(param-&gt;value[i], tokenEqual);
  
      i++;
    }
    free(parseString);
    return OK;
  }
  </code></pre>
  
  <p>And here is what valgrind is telling me:</p>
  
  <pre><code>==7925== Invalid write of size 8
  ==7925==    at 0x400E56: parseOptionalParam (calutil.c:79)
  ==7925==    by 0x400CED: parseCalProp (calutil.c:50)
  ==7925==    by 0x400B0B: main (testfile.c:8)
  ==7925==  Address 0x5202508 is 8 bytes after a block of size 16 alloc'd
  ==7925==    at 0x4C2DD9F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==7925==    by 0x400E13: parseOptionalParam (calutil.c:77)
  ==7925==    by 0x400CED: parseCalProp (calutil.c:50)
  ==7925==    by 0x400B0B: main (testfile.c:8)
  </code></pre>
  
  <p>The line 79 is the line that starts with <code>param-&gt;value[i] =</code> and then valgrind is referring to the <code>realloc</code> that is 2 lines above. I am confused what is wrong here? Within the <code>param</code> structure there is a flexible array member at the end of the structure which is <code>value</code>. I am simply trying to allocate another array position in the structure and then use that position for a string.</p>
  
  <p>At some point in those two lines I am guessing I am doing something incorrect regarding the memory, but I am unsure what it is yet. </p>