title: how to correct an overflow problem related to the structure pointer?
body: <p>I did not share all of my code since it's long, so I hope you can help me with the debugging I did with valgrind and it's overflow problem.
  I just wanted to retrieve data that I assigned in this pointed structure in another function.</p>
  <pre><code>struct paramAL{
      int noVersion;
      int noligne;
      char tligne[100];
      };
  </code></pre>
  <p>Here is the part of the readTrans function in which I assigned data to the pointed structure</p>
  <pre><code>void* readTrans(char* nomFichier){
          FILE *f;
          char buffer[100];
          char *tok, *sp;
          pthread_t tid[1000];
          int nbThread = 0;
          int i;
      
          //Ouverture du fichier en mode &quot;r&quot; (equiv. &quot;rt&quot;) : [r]ead [t]ext
          f = fopen(nomFichier, &quot;rt&quot;);
          if (f==NULL)
              error(2, &quot;readTrans: Erreur lors de l'ouverture du fichier.&quot;);
      
          //Lecture (tentative) d'une ligne de texte
          fgets(buffer, 100, f);
          
          //Pour chacune des lignes lues
          while(!feof(f)){
      
              //Extraction du type de transaction 2 cas: commande avec arguments et sans argument
              tok = strtok_r(buffer, &quot; .&quot;, &amp;sp);
      
              //Branchement selon le type de transac  
              if(strcmp(tok, &quot;AL&quot;) == 0){
                  //Extraction des paramètres
                  int noVersion = atoi(strtok_r(NULL, &quot; &quot;, &amp;sp));
                  int noligne = atoi(strtok_r(NULL, &quot; &quot;, &amp;sp));
                  char *tligne = strtok_r(NULL, &quot;\n&quot;, &amp;sp);
                  //Appel de la fonction associée
                  struct paramAL *ptr = (struct paramAL*) malloc(sizeof(struct paramAL));
                  ptr-&gt;noVersion = noVersion;
                  ptr-&gt;noligne = noligne;
                  strcpy(ptr-&gt;tligne,(const char *)tligne);
                  pthread_create(&amp;tid[nbThread++], NULL, addItemL,(void*) ptr);   
              } else if(strcmp(tok, &quot;CV&quot;) == 0) {
                  //Extraction des paramètres
                  int noVersion = atoi(strtok_r(NULL, &quot;\n &quot;, &amp;sp));
                  //Appel de la fonction associée
                  copyItemV(VRAI, noVersion); 
              }
      
              //Lecture (tentative) de la prochaine ligne de texte
              fgets(buffer, 100, f);
          }
          for(i=0; i&lt;nbThread;i++)
              pthread_join(tid[i], NULL);
          //Fermeture du fichier
          fclose(f);
          //Retour
          return NULL;
      }
  </code></pre>
  <p>This is the part of the code of concern:</p>
  <pre><code>void* addItemL(void* parama){  
      char tl[100]; 
      int noVersion, nl;
      
      struct paramAL *param = parama;
          
      noVersion = param-&gt;noVersion;
      nl = param-&gt;noligne;
      strcpy(tl,(const char*)param-&gt;tligne);
      free(param);
      struct noeudV * ptrV;
      
      ptrV = findItemV(noVersion);
  
      // Verifier si la version existe
      if (ptrV==NULL)
          return NULL;
  
      //Création de l'enregistrement en mémoire
      struct noeudL* ni = (struct noeudL*)malloc(sizeof(struct noeudL));
  
      struct noeudL* ptrINS = findItemL(noVersion, nl);
  
      //Affectation des valeurs des champs
      ni-&gt;ligne.noligne   = nl;
      strcpy(ni-&gt;ligne.ptrligne, tl);
  
      if((ptrINS == NULL) &amp;&amp; (nl == 1)) // ajout au debut de la liste vide
      {
          // premiere ligne premier noeud 
          ni-&gt;suivant= NULL;
          ptrV-&gt;finL = ptrV-&gt; debutL = ni;
  
      }
      else if ((ptrINS == NULL) &amp;&amp; (nl &gt; 1)) // ajout a la fin de la liste
      {
          struct noeudL* tptr = ptrV-&gt;finL;
          ni-&gt;suivant= NULL;
          ptrV-&gt;finL = ni;
          tptr-&gt;suivant = ni;
      }
      else
      {
          struct noeudL* tptr = ptrINS;
          if(tptr == ptrV-&gt;debutL) // ajout a la tete de la liste
              ptrV-&gt;debutL = ni;
          else
          {
              struct noeudL* ptrPREV = findPrevL(noVersion, nl);
              ptrPREV-&gt;suivant = ni;
          }
          ni-&gt;suivant = tptr;
          
          while (tptr!=NULL) {
              //Est-ce le prédécesseur de l'item recherché?
              tptr-&gt;ligne.noligne++;
              //On retourne un pointeur sur l'item précédent
  
              //Déplacement du pointeur de navigation
              tptr=tptr-&gt;suivant;
          }
      }
      return NULL;
  }
  </code></pre>
  <p>Here are the errors detected by the valgrind :</p>
  <pre><code>valgrind --leak-check=full ./gestionCVS_MAIN t2.txt                                                                                                                                                                            
  ==50153== Memcheck, a memory error detector                                                                                                                                                                                      
  ==50153== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.                                                                                                                                                        
  ==50153== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info                                                                                                                                                    
  ==50153== Command: ./gestionCVS_MAIN t2.txt                                                                                                                                                                                      
  ==50153==                                                                                                                                                                                                                        
  noVersion  Nom Version                                  
  ======= ================================================
  1        V1
  
  2        
  
  3        
  
  4        
  
  ======= ===============================================
  
  noVersion noligne       texte                                          
  =======   ===========   ==================================
  =========================================================
  
  noVersion noligne       texte                                          
  =======   ===========   ==================================
  =========================================================
  
  noVersion noligne       texte                                          
  =======   ===========   ==================================
  =========================================================
  
  ==50153== Invalid read of size 4
  ==50153==    at 0x400E04: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==  Address 0x576f150 is 0 bytes inside a block of size 108 free'd
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==
  ==50153== Invalid read of size 4
  ==50153==    at 0x400E13: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==  Address 0x576f154 is 4 bytes inside a block of size 108 free'd
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==
  ==50153== Invalid read of size 1
  ==50153==    at 0x4C2E1C7: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E35: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==  Address 0x576f158 is 8 bytes inside a block of size 108 free'd
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==
  ==50153== Invalid read of size 1
  ==50153==    at 0x4C2E1E4: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E35: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==  Address 0x576f159 is 9 bytes inside a block of size 108 free'd
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==
  ==50153== Invalid free() / delete / delete[] / realloc()
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==  Address 0x576f150 is 0 bytes inside a block of size 108 free'd
  ==50153==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==50153==    by 0x400E44: addItemL (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x4012E4: copyItemV (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x402129: readTrans (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==    by 0x400BAE: main (in /home/mi136/Documents/testtp/test5/exempleCVS-A20/gestionCVS_MAIN)
  ==50153==
  noVersion  Nom Version                                  
  ======= ================================================
  1        V1
  
  2        
  
  3        
  
  4        
  
  ======= ===============================================
  
  noVersion noligne       texte                                          
  =======   ===========   ==================================
  =========================================================
  
  noVersion noligne       texte                                          
  =======   ===========   ==================================
  =========================================================
  
  -rw-r--r-- 1 mi136 www-data  89 sep 29 21:16 V1.c
  -rw-rw-r-- 1 mi136 www-data  44 oct 25 17:22 V1?.c
  -rw-r--r-- 1 mi136 www-data 108 sep 29 21:16 V2.c
  -rw-rw-r-- 1 mi136 www-data   0 oct 13 12:46 V2?.c
  
   Mon Premier Programme CVS
  
   Mon Premier programme CVS non pas le Dernier
  ==50153==
  ==50153== HEAP SUMMARY:
  ==50153==     in use at exit: 557,952 bytes in 4,104 blocks
  ==50153==   total heap usage: 8,215 allocs, 4,113 frees, 2,888,760 bytes allocated
  ==50153==
  ==50153== LEAK SUMMARY:
  ==50153==    definitely lost: 0 bytes in 0 blocks
  ==50153==    indirectly lost: 0 bytes in 0 blocks
  ==50153==      possibly lost: 0 bytes in 0 blocks
  ==50153==    still reachable: 557,952 bytes in 4,104 blocks
  ==50153==         suppressed: 0 bytes in 0 blocks
  ==50153== Reachable blocks (those to which a pointer was found) are not shown.
  ==50153== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  ==50153==
  ==50153== For counts of detected and suppressed errors, rerun with: -v
  ==50153== ERROR SUMMARY: 46 errors from 5 contexts (suppressed: 0 from 0)
  </code></pre>