Bonjour,
je suis en train de programmer un serveur web en C. Le tout fonctionne à peu prés bien mais je rencontre un problème dans une de mes fonctions. En effet j'ai besoin de lister le contenu d'un dossier si celui ci ne contient pas de fichier index.html
Voila ma fonction censé faire ceci
Je lui passe le chemin du dossier et elle me retourne le texte que j'écris ensuite dans ma socket. La fonction fonctionne mais dans ma console j'ai une erreur mémoire.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 char *listFolder(char *path) //liste le contenu d'un dossier grace à son chemin { char hostNerrorP[32]; char *listFolderDef; char pageAppend[]="</html>"; gethostname(hostNerrorP,sizeof(hostNerrorP)); char bodyList[]="<html><center><h1>Liste des fichiers du répértoire<h1></center><hr>"; char *startLink; startLink = realloc(NULL, strlen("") + 1); sprintf(startLink, ""); char href[] = "<a href=\""; char middleLink[] = "\">"; char endLink[] = "</a>"; char br[] = "<br/>"; char *huh = strstr(path, "/www/"); huh = strstr(huh, "www/"); huh = strstr(huh, "/"); if(huh[strlen(huh)-1] != '/'){ strcat(huh, "/"); } printf("%s\n", huh); struct dirent *lecture; DIR *rep; rep = opendir((char *)path); while ((lecture = readdir(rep))) { char tmp[strlen(href)+strlen(huh)+ strlen(lecture->d_name)+strlen(middleLink)+strlen(lecture->d_name)+strlen(endLink)+strlen(br)]; sprintf(tmp, "%s%s%s%s%s%s%s", href, huh, lecture->d_name, middleLink, lecture->d_name, endLink, br); startLink = realloc(startLink, strlen(tmp) + strlen(startLink) + 1); strcat(startLink, tmp); } closedir(rep); listFolderDef=malloc((sizeof(bodyList)+sizeof(startLink)+sizeof(pageAppend))*(sizeof(char))); sprintf(listFolderDef,"%s%s%s",bodyList,startLink,pageAppend); return listFolderDef; }
Ceci indique un mauvais accès mémoire au niveau de cette ligne je pense.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 *** glibc detected *** ./test: malloc(): memory corruption: 0x08dbe080 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6[0xc0dff1] /lib/tls/i686/cmov/libc.so.6[0xc10bb3] /lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x58)[0xc12868] /lib/tls/i686/cmov/libc.so.6[0xbfeddf] /lib/tls/i686/cmov/libc.so.6(fopen+0x2c)[0xbfeeac] ./test[0x804991a] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xbb9b56] ./test[0x8048c01] ======= Memory map: ======== 00689000-0068a000 r-xp 00000000 00:00 0 [vdso] 00a16000-00a31000 r-xp 00000000 08:05 186741 /lib/ld-2.10.1.so 00a31000-00a32000 r--p 0001a000 08:05 186741 /lib/ld-2.10.1.so 00a32000-00a33000 rw-p 0001b000 08:05 186741 /lib/ld-2.10.1.so 00ba3000-00ce1000 r-xp 00000000 08:05 397518 /lib/tls/i686/cmov/libc-2.10.1.so 00ce1000-00ce3000 r--p 0013e000 08:05 397518 /lib/tls/i686/cmov/libc-2.10.1.so 00ce3000-00ce4000 rw-p 00140000 08:05 397518 /lib/tls/i686/cmov/libc-2.10.1.so 00ce4000-00ce7000 rw-p 00000000 00:00 0 00dd1000-00ded000 r-xp 00000000 08:05 170831 /lib/libgcc_s.so.1 00ded000-00dee000 r--p 0001b000 08:05 170831 /lib/libgcc_s.so.1 00dee000-00def000 rw-p 0001c000 08:05 170831 /lib/libgcc_s.so.1 08048000-0804a000 r-xp 00000000 08:05 495498 /home/allian/Bureau/server/test 0804a000-0804b000 r--p 00001000 08:05 495498 /home/allian/Bureau/server/test 0804b000-0804c000 rw-p 00002000 08:05 495498 /home/allian/Bureau/server/test 08dbe000-08ddf000 rw-p 00000000 00:00 0 [heap] b7600000-b7621000 rw-p 00000000 00:00 0 b7621000-b7700000 ---p 00000000 00:00 0 b77dc000-b77dd000 rw-p 00000000 00:00 0 b77ed000-b77f0000 rw-p 00000000 00:00 0 bff7f000-bff94000 rw-p 00000000 00:00 0 [stack]
Quelqu'un pourrait il m'éclairer sur les raisons de cette erreur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 char tmp[strlen(href)+strlen(huh)+ strlen(lecture->d_name)+strlen(middleLink)+strlen(lecture->d_name)+strlen(endLink)+strlen(br)]; sprintf(tmp, "%s%s%s%s%s%s%s", href, huh, lecture->d_name, middleLink, lecture->d_name, endLink, br); startLink = realloc(startLink, strlen(tmp) + strlen(startLink) + 1); strcat(startLink, tmp);
Merci.
Partager