c - 如何纠正与结构指针相关的溢出问题?
问题描述
我没有分享我所有的代码,因为它很长,所以我希望你能帮助我解决我对 valgrind 所做的调试和溢出问题。我只是想在另一个函数中检索我在这个指向结构中分配的数据。
struct paramAL{
int noVersion;
int noligne;
char tligne[100];
};
这是 readTrans 函数的一部分,其中我将数据分配给了指向的结构
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 "r" (equiv. "rt") : [r]ead [t]ext
f = fopen(nomFichier, "rt");
if (f==NULL)
error(2, "readTrans: Erreur lors de l'ouverture du fichier.");
//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, " .", &sp);
//Branchement selon le type de transac
if(strcmp(tok, "AL") == 0){
//Extraction des paramètres
int noVersion = atoi(strtok_r(NULL, " ", &sp));
int noligne = atoi(strtok_r(NULL, " ", &sp));
char *tligne = strtok_r(NULL, "\n", &sp);
//Appel de la fonction associée
struct paramAL *ptr = (struct paramAL*) malloc(sizeof(struct paramAL));
ptr->noVersion = noVersion;
ptr->noligne = noligne;
strcpy(ptr->tligne,(const char *)tligne);
pthread_create(&tid[nbThread++], NULL, addItemL,(void*) ptr);
} else if(strcmp(tok, "CV") == 0) {
//Extraction des paramètres
int noVersion = atoi(strtok_r(NULL, "\n ", &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<nbThread;i++)
pthread_join(tid[i], NULL);
//Fermeture du fichier
fclose(f);
//Retour
return NULL;
}
这是关注代码的一部分:
void* addItemL(void* parama){
char tl[100];
int noVersion, nl;
struct paramAL *param = parama;
noVersion = param->noVersion;
nl = param->noligne;
strcpy(tl,(const char*)param->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->ligne.noligne = nl;
strcpy(ni->ligne.ptrligne, tl);
if((ptrINS == NULL) && (nl == 1)) // ajout au debut de la liste vide
{
// premiere ligne premier noeud
ni->suivant= NULL;
ptrV->finL = ptrV-> debutL = ni;
}
else if ((ptrINS == NULL) && (nl > 1)) // ajout a la fin de la liste
{
struct noeudL* tptr = ptrV->finL;
ni->suivant= NULL;
ptrV->finL = ni;
tptr->suivant = ni;
}
else
{
struct noeudL* tptr = ptrINS;
if(tptr == ptrV->debutL) // ajout a la tete de la liste
ptrV->debutL = ni;
else
{
struct noeudL* ptrPREV = findPrevL(noVersion, nl);
ptrPREV->suivant = ni;
}
ni->suivant = tptr;
while (tptr!=NULL) {
//Est-ce le prédécesseur de l'item recherché?
tptr->ligne.noligne++;
//On retourne un pointeur sur l'item précédent
//Déplacement du pointeur de navigation
tptr=tptr->suivant;
}
}
return NULL;
}
以下是 valgrind 检测到的错误:
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)
解决方案
推荐阅读
- reactjs - React 甚至设置了静态 defaultProps,但是报错为什么?
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 8 中的字节 0xc3:序数不在范围内(128)
- sql-server - Logstash 到 SQL Server 连接失败
- php - 滑块未以正确格式显示
- python - 如果日期时间缺少一分钟或更长时间的条目,则无法重新采样
- mysql - 两个表的where子句中的count()
- reactjs - 嵌套开关可以使用换页动画吗?
- c# - 如何让 ASP.NET Core 返回 XML 结果?
- mysql - InnoDB:找不到“表”的有效表空间文件?
- html - 隐藏内容时保持自动高度 DIV 的高度