title: Invalid Read of size 1 on running Valgrind
body: <pre><code>The following is the error that I got after running Valrind    
  valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./out
  
  
  
    ==12140== Invalid read of size 1
      ==12140==    at 0x4C2DF84: strncat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==12140==    by 0x40100D: createSortedRuns (final.c:178)
      ==12140==    by 0x401297: main (final.c:249)
      ==12140==  Address 0x51fd549 is 0 bytes after a block of size 9 alloc'd
      ==12140==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==12140==    by 0x400EFD: createSortedRuns (final.c:150)
      ==12140==    by 0x401297: main (final.c:249)
      ==12140== 
  </code></pre>
  
  <p>The other error that I am getting is:</p>
  
  <pre><code>==12140== Invalid write of size 1
  ==12140==    at 0x4C2DFD0: strncat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==12140==    by 0x40100D: createSortedRuns (final.c:178)
  ==12140==    by 0x401297: main (final.c:249)
  ==12140==  Address 0x51fd54d is 4 bytes after a block of size 9 alloc'd
  ==12140==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==12140==    by 0x400EFD: createSortedRuns (final.c:150)
  ==12140==    by 0x401297: main (final.c:249)
  ==12140== 
  ==12140== 
  ==12140== 250 errors in context 7 of 13:
  </code></pre>
  
  <p>The other error that I am getting is:</p>
  
  <pre><code>==12140== Invalid read of size 1
  ==12140==    at 0x4C2DF84: strncat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==12140==    by 0x40100D: createSortedRuns (final.c:178)
  ==12140==    by 0x401297: main (final.c:249)
  ==12140==  Address 0x51fd549 is 0 bytes after a block of size 9 alloc'd
  ==12140==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==12140==    by 0x400EFD: createSortedRuns (final.c:150)
  ==12140==    by 0x401297: main (final.c:249)
  ==12140== 
  </code></pre>
  
  <p>The following is the snippet of the program:</p>
  
  <pre><code>  void createSortedRuns(int start, int end, char file[]){
  
          FILE *givenFile = NULL;
          givenFile=fopen(file,"r+b");
          rewind(givenFile);
          int i=0;
          /*printf("File:%s\n",file);
          printf("File Length:%zu\n",strlen(file));*/
          char *temp = (char *)malloc(sizeof(char)*strlen(file));
  
  
  
          for(i=start; i&lt;end; i++){
              fseek(givenFile,0,SEEK_CUR);
              fread(&amp;inputBuffer[act_inputBufferSize],sizeof(int),1,givenFile);
              act_inputBufferSize++;
  
              if((act_inputBufferSize==max_inputBufferSize) ||(i==(end-1))){
  
                  //sort
                  qsort(inputBuffer,act_inputBufferSize,sizeof(int),compare);
  
                  // write to file
  
                  strncpy(temp,file,strlen(file));
  
  
                  //int k=0;
                  char counter[4]={0};
                  /*for(k=0;k&lt;4;k++){
                      counter[k]=0;
                  }*/
  
  
                  snprintf(counter,5, ".%03d", totalSortedFiles);
                  counter[strlen(counter)] = '\0';
                  strncat(temp,counter,4);
  
                  FILE *int_File = NULL;
                  int_File=fopen(temp,"w+b");
                  rewind(int_File);
                  fwrite(&amp;inputBuffer,sizeof(int),act_inputBufferSize,int_File);
  
                  // register the sub_fo information
                  small_fo=register_sub_fo(small_fo,totalSortedFiles,temp, 0 ,act_inputBufferSize,act_inputBufferSize);
  
  
                  // increase the totalSortedFiles
                  totalSortedFiles+=1;
  
  
                  // Reinitialize
                              int j=0;
                              for(j=0;j&lt;max_inputBufferSize;j++){
                                  inputBuffer[j]=0;
                              }
                              act_inputBufferSize=0;
  
                              int len = strlen(temp);
                              temp[len-4] = '\0';
                 // Garbage
                              fclose(int_File);
                              int_File = NULL;
  
  
  
              }
          } //for_end
          fclose(givenFile);
          givenFile=NULL;
  
          free(temp);
          temp=NULL;
  
  
      } // func_end
  </code></pre>
  
  <p>Valgrind is showing that the error is at line "strncat(temp,counter,4);"    Can someone please explain me the error ?</p>