|
|
| | HOMEPAGE | INDICE FORUM | REGOLAMENTO | ::. | NEI PREFERITI | .:: | RSS Forum | RSS News | NEWS web | NEWS software | |
| | PUBBLICITA' | | | ARTICOLI | WIN XP | VISTA | WIN 7 | REGISTRI | SOFTWARE | MANUALI | RECENSIONI | LINUX | HUMOR | HARDWARE | DOWNLOAD | | | CERCA nel FORUM » | |
|
|
#1 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
[C] Segmentation fault
Sono settimane che questo problema mi tormenta. Sto facendo un progetto per l'Università che dovrò cosnegnare il 3 aprile. Ho fatto tutto, ma c'è questo segmentation fault che non ne vuole sapere di sparire. Allora praticamente mi hanno fornito un file di Test il quale appunto testerà la mia funzione e ad un test di questi mi dà l'errore. Ora posto la mia funzione (per intero) e poi il codice del test in questione: Codice:
#include "LPC_M1_Include.h"
#include "LPC_Include.h"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
int CreateDatabaseFile(char *Name, int NumFields, FIELD_DATA_t *FieldData){
int i;
header *h;
h=(header *)malloc(sizeof(header));
if((Name==NULL) || cercaErr(Name) || (NumFields<=0) || (NumFields>MAX_NUM_FIELDS)){
char Err[256];
(void) sprintf(Err, "Nome DB nullo o non valido o NumFields<=0");
LPC_GestioneErrore(LPC_BAD_ARG, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_BAD_ARG;
}
if(FieldData==NULL){
char Err[256];
(void) sprintf(Err, "Errore allocazione memoria");
LPC_GestioneErrore(LPC_NO_MEMORY, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_NO_MEMORY;
}
if(strlen(Name)>DBF_NAME_LENGTH+1)
Name[DBF_NAME_LENGTH+1]='\0';
strcat(Name,DBF_EXTENSION);
FILE *fp=fopen(Name,"wb+");
if(fp==NULL){
char Err[256];
(void) sprintf(Err, "Errore allocazione memoria");
LPC_GestioneErrore(LPC_NO_MEMORY, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_NO_MEMORY;
}
h->NumCampi=(uint16_t) htons(h->NumCampi);
h->NumCampi=NumFields;
h->NumRecords=(uint32_t) htonl(h->NumRecords);
h->NumRecords=0;
h->dimHeader=(uint16_t) htons(h->dimHeader);
h->dimHeader=DBF_FIXED_HEADER_LEN+NumFields*(DBF_FIELD_LEN+NumFields)+2;
h->dimRecord=(uint16_t) htons(h->dimRecord);
h->dimRecord=0;
h->timestamp=(uint32_t) htonl(h->timestamp);
h->timestamp=time(NULL);
for(i=0; i<NumFields; ++i){
if(FieldData[i].Name==NULL){
char Err[256];
(void) sprintf(Err, "Valore di uno dei campi non valido: nome, tipo o lunghezza");
LPC_GestioneErrore(LPC_BAD_FIELD, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_BAD_FIELD;
}
if((cercaErr(FieldData[i].Name)) || (StringaUguale(FieldData[i].Name,h,i))){
char Err[256];
(void) sprintf(Err, "Valore di uno dei campi non valido: nome, tipo o lunghezza");
LPC_GestioneErrore(LPC_BAD_FIELD, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_BAD_FIELD;
}
strcpy(h->campo[i].name,FieldData[i].Name);
if ((FieldData[i].FieldType!=CHARACTER) || (FieldData[i].FieldType!=NUMERIC) || (FieldData[i].FieldType!=DATE) || (FieldData[i].FieldType!=LOGICAL)){
char Err[256];
(void) sprintf(Err, "Valore di uno dei campi non valido: nome, tipo o lunghezza");
LPC_GestioneErrore(LPC_BAD_FIELD, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_BAD_FIELD;
}
h->campo[i].fieldtype=(uint16_t) htons(h->campo[i].fieldtype);
h->campo[i].fieldtype=FieldData[i].FieldType;
h->campo[i].fieldlen=(uint16_t) htons(h->campo[i].fieldlen);
if((h->campo[i].fieldtype==CHARACTER) && ((FieldData[i].FieldLen>=1) && (FieldData[i].FieldLen<=FIELD_CHAR_LEN)))
h->campo[i].fieldlen=FieldData[i].FieldLen;
if((h->campo[i].fieldtype==NUMERIC) && (FieldData[i].FieldLen<=FIELD_NUM_LEN))
h->campo[i].fieldlen=FieldData[i].FieldLen;
if((h->campo[i].fieldtype==DATE) && (FieldData[i].FieldLen<=FIELD_DATE_LEN))
h->campo[i].fieldlen=FieldData[i].FieldLen;
if((h->campo[i].fieldtype==LOGICAL) && (FieldData[i].FieldLen<=FIELD_LOGI_LEN))
h->campo[i].fieldlen=FieldData[i].FieldLen;
h->dimRecord+=(h->campo[i].fieldlen+1);
}
int ret=fwrite(h,sizeof(h),1,fp);
if(ret!=h->dimHeader){
char Err[256];
(void) sprintf(Err, "Errore di sistema [%d] in scrittura su file: [%s]", errno, strerror(errno));
LPC_GestioneErrore(LPC_ERR_WRITE, "CreateDatabaseFile", Err);
if(h!=NULL)
free(h);
return LPC_ERR_WRITE;
}
fclose(fp);
return LPC_OK;
putchar('\n');
}
Codice:
#define MAX_TEST 100
typedef struct {
int testid;
int weight;
char desc[256];
int passed;
} TEST_t;
static int numtest;
static TEST_t test[MAX_TEST];
int ret= 0;
int NumFields = 0;
FILE *fp;
FIELD_DATA_t FieldData[MAX_NUM_FIELDS];
int len, len1;
char buf[500], buftest[500];
printf("Test CREATE\n");
...
...
...
// Campo non valido
/*** Test del Nome, Tipo, Lunghezza *****/
test[numtest].testid = 10107;
test[numtest].weight = 1;
strcpy(test[numtest].desc, "CREATE: Nome campo non valido");
printf("Test:[%d] Peso:[%d] - [%s]\n", test[numtest].testid, test[numtest].weight,test[numtest].desc);
NumFields = 1;
strcpy(FieldData[0].Name, "_Campo1");
FieldData[0].FieldType = CHARACTER;
FieldData[0].FieldLen = 10;
ret = CreateDatabaseFile("Prova", NumFields, FieldData);
printf("Ritorno della create: [%d]\n\n", ret);
if (ret != LPC_OK)
test[numtest].passed = 1;
numtest++;
test[numtest].testid = 10108;
test[numtest].weight = 3;
strcpy(test[numtest].desc, "CREATE: Tipo campo non valido");
printf("Test:[%d] Peso:[%d] - [%s]\n", test[numtest].testid, test[numtest].weight,test[numtest].desc);
NumFields = 1;
strcpy(FieldData[0].Name, "Campo1");
FieldData[0].FieldType = 15;
FieldData[0].FieldLen = 10;
ret = CreateDatabaseFile("Prova", NumFields, FieldData);
printf("Ritorno della create: [%d]\n\n", ret);
if (ret != LPC_OK)
test[numtest].passed = 1;
numtest++;
test[numtest].testid = 10109;
test[numtest].weight = 2;
strcpy(test[numtest].desc, "CREATE: Lunghezza campo non valida");
printf("Test:[%d] Peso:[%d] - [%s]\n", test[numtest].testid, test[numtest].weight,test[numtest].desc);
NumFields = 1;
strcpy(FieldData[0].Name, "Campo1");
FieldData[0].FieldType = CHARACTER;
FieldData[0].FieldLen = FIELD_CHAR_LEN+1;
ret = CreateDatabaseFile("Prova", NumFields, FieldData);
printf("Ritorno della create: [%d]\n\n", ret);
if (ret != LPC_OK)
test[numtest].passed = 1;
numtest++;
test[numtest].testid = 10110;
test[numtest].weight = 2;
strcpy(test[numtest].desc, "CREATE: Campo gia' esistente");
printf("Test:[%d] Peso:[%d] - [%s]\n", test[numtest].testid, test[numtest].weight,test[numtest].desc);
NumFields = 2;
strcpy(FieldData[0].Name, "Campo1");
FieldData[0].FieldType = CHARACTER;
FieldData[0].FieldLen = 10;
strcpy(FieldData[1].Name, "Campo1");
FieldData[1].FieldType = CHARACTER;
FieldData[1].FieldLen = 10;
ret = CreateDatabaseFile("Prova", NumFields, FieldData);
printf("Ritorno della create: [%d]\n\n", ret);
if (ret != LPC_OK)
test[numtest].passed = 1;
numtest++;
Ultima modifica di Manugal : 28-03-2006 alle ore 19.52.59 |
|
|
|
|
|
#2 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Nessuno???
|
|
|
|
|
|
#3 |
|
WT Assistant
Registrato: 19-12-2000
Loc.: Cambiano (Torino)
Messaggi: 591
![]() |
com'è definito header?
Perché dichiari "char Err[256];" all'interno dei blocchi if()? Hai provato ad usare un memory debugger tipo valgrind? O anche solo gdb?
___________________________________
There are 10 kinds of people in this world, those who can read binary and those who can't. Care to say "thank you"? |
|
|
|
|
|
#4 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Purtroppo non è che sappia usarli bene, comunque se ti va di provarlo l'ho uppato su Rapidshare. Dichiaro char err[256] dentro gli if perché le specifiche che mi hanno dato lo richiedono.
http://rapidshare.de/files/16712699/DBF.tar.gz.html Lo estrai e per compilarlo vai nella cartella Modulo1 e digita make test. Per eseguire poi i test, una volta compilato, vai nella cartella test (sempre dentro Modulo1) e lanci TestM1. Grazie. |
|
|
|
|
|
#5 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Mi sono un po' imparato ad usare valgrind(). Ecco il risultato facendo un --leack-check=full
Codice:
==17259== ==17259== Process terminating with default action of signal 11 (SIGSEGV) ==17259== Bad permissions for mapped region at address 0x804B3FD ==17259== at 0x401C481: strcat (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so) ==17259== by 0x804A86D: CreateDatabaseFile (LPC_CreateDBF.c:32) ==17259== by 0x8049354: TestCreate (TestM1.c:201) ==17259== by 0x8048AF2: main (TestM1.c:94) ==17259== ==17259== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1) ==17259== malloc/free: in use at exit: 4,108 bytes in 1 blocks. ==17259== malloc/free: 7 allocs, 6 frees, 28,756 bytes allocated. ==17259== For counts of detected errors, rerun with: -v ==17259== searching for pointers to 1 not-freed blocks. ==17259== checked 86,372 bytes. ==17259== ==17259== LEAK SUMMARY: ==17259== definitely lost: 0 bytes in 0 blocks. ==17259== possibly lost: 0 bytes in 0 blocks. ==17259== still reachable: 4,108 bytes in 1 blocks. ==17259== suppressed: 0 bytes in 0 blocks. ==17259== Reachable blocks (those to which a pointer was found) are not shown. ==17259== To see them, rerun with: --show-reachable=yes Segmentation fault |
|
|
|
|
|
#6 |
|
WT Assistant
Registrato: 19-12-2000
Loc.: Cambiano (Torino)
Messaggi: 591
![]() |
Modulo1 è vuota...
___________________________________
There are 10 kinds of people in this world, those who can read binary and those who can't. Care to say "thank you"? |
|
|
|
|
|
#7 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
accidenti hai ragione perché ti ho mandato solo la struttura delle directory. Aspetta allora che riuppo tutto.
|
|
|
|
|
|
#8 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
|
|
|
|
|
|
#9 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Sono riuscito a togliere un Segmentation Fault, adesso però me lo dà sull'ultimo test.
(Grazie a valgrind sono riuscito a scovarlo)Questo è l'output di valgrind per quest'ultimo errore (e qui non riesco a scovarlo): Codice:
Test:[10113] Peso:[10] - [CREATE: Verifica correttezza] ==17837== ==17837== Invalid read of size 4 ==17837== at 0x408E77E: fread (in /lib/tls/libc-2.3.5.so) ==17837== by 0x8049C97: TestCreate (TestM1.c:316) ==17837== by 0x8048AF2: main (TestM1.c:94) ==17837== Address 0x0 is not stack'd, malloc'd or (recently) free'd ==17837== ==17837== Process terminating with default action of signal 11 (SIGSEGV) ==17837== Access not within mapped region at address 0x0 ==17837== at 0x408E77E: fread (in /lib/tls/libc-2.3.5.so) ==17837== by 0x8049C97: TestCreate (TestM1.c:316) ==17837== by 0x8048AF2: main (TestM1.c:94) ==17837== ==17837== ERROR SUMMARY: 16 errors from 16 contexts (suppressed: 11 from 1) ==17837== malloc/free: in use at exit: 2,112 bytes in 6 blocks. ==17837== malloc/free: 21 allocs, 15 frees, 52,464 bytes allocated. ==17837== For counts of detected errors, rerun with: -v ==17837== searching for pointers to 6 not-freed blocks. ==17837== checked 87,372 bytes. ==17837== ==17837== ==17837== 2,112 bytes in 6 blocks are still reachable in loss record 1 of 1 ==17837== at 0x401A43A: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so) ==17837== by 0x408E21E: __fopen_internal (in /lib/tls/libc-2.3.5.so) ==17837== by 0x408E2DC: fopen@@GLIBC_2.1 (in /lib/tls/libc-2.3.5.so) ==17837== by 0x804A86D: CreateDatabaseFile (LPC_CreateDBF.c:32) ==17837== by 0x8049354: TestCreate (TestM1.c:201) ==17837== by 0x8048AF2: main (TestM1.c:94) ==17837== ==17837== LEAK SUMMARY: ==17837== definitely lost: 0 bytes in 0 blocks. ==17837== possibly lost: 0 bytes in 0 blocks. ==17837== still reachable: 2,112 bytes in 6 blocks. ==17837== suppressed: 0 bytes in 0 blocks. Segmentation fault |
|
|
|
|
|
#10 |
|
WT Assistant
Registrato: 19-12-2000
Loc.: Cambiano (Torino)
Messaggi: 591
![]() |
ho guardato il makefile, non hai impostato i warning:
CCFLAGS= -c $(DEBUG) -Wall sono molto utili per scoprire problemi in fase di compilazione. Vedo ad esempio alcuni errori di confronto tra signed e unsigned int. Io poi di solito uso g++ come compiler: CC=g++ Il makefile non compila il file contenente LPC_GestioneErrore() In LPC_CreateDBF.c non hai dichiarato la variabile tmp, e probabilmente devi assegnarle il risultato di strcat() In TestM1.c non fai nessun controllo sull'apertura dei files: LINE 311: Codice:
fp = fopen("Prova.dbf", "r");
if (fp == NULL) {
//oops...
exit(EXIT_FAILURE);
}
___________________________________
There are 10 kinds of people in this world, those who can read binary and those who can't. Care to say "thank you"? |
|
|
|
|
|
#11 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Il Makefile e TestM1 non li ho fatti io ma me li hanno dati i miei professori e dicono che sono funzionanti e testati. Io ho creato solamente quello che c'è nella cartella src.
|
|
|
|
|
|
#12 |
|
WT Assistant
Registrato: 19-12-2000
Loc.: Cambiano (Torino)
Messaggi: 591
![]() |
sarà, però mancano tutti i controlli...
comunque: il segfault è in LPC_CreateDBF.c, nella riga che ti ho suggerito sopra: stai cercando di aprire un file il cui nome è contenuto nella variabile tmp, che non è definita. Inoltre ci dev'essere qualche problema nella strcat() alla riga 36, sostituiscila con questo codice: Codice:
//dichiarazioni char *filename; int newlen; //linea 36 newlen = strlen(Name) + strlen(DBF_EXTENSION) + 1; filename = (char *)malloc(sizeof(char) * newlen); sprintf(filename, "%s%s", Name, DBF_EXTENSION); FILE *fp=fopen(filename,"wb+"); // <== "filename", non "tmp"!!!
___________________________________
There are 10 kinds of people in this world, those who can read binary and those who can't. Care to say "thank you"? |
|
|
|
|
|
#13 |
|
WT Assistant
Registrato: 19-12-2000
Loc.: Cambiano (Torino)
Messaggi: 591
![]() |
ricordati: gdb è il tuo migliore amico:
$ gdb Test1 r (run) inoltre, mandami l'output del test. Qui mi da' molti errori... sono voluti? Perché non usate una testsuite tipo CUNIT?
___________________________________
There are 10 kinds of people in this world, those who can read binary and those who can't. Care to say "thank you"? |
|
|
|
|
|
#14 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Grazie, ma quel problema come avevo scritto poco fa l'avevo risolto (a dire la verità neanche mi serviva aggiungere l'espressione lì). Il problema è ora nell'ultimo test. valgrind mi da questo output:
Codice:
Test:[10113] Peso:[10] - [CREATE: Verifica correttezza]
==17837==
==17837== Invalid read of size 4
==17837== at 0x408E77E: fread (in /lib/tls/libc-2.3.5.so)
==17837== by 0x8049C97: TestCreate (TestM1.c:316)
==17837== by 0x8048AF2: main (TestM1.c:94)
==17837== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==17837==
==17837== Process terminating with default action of signal 11 (SIGSEGV)
==17837== Access not within mapped region at address 0x0
==17837== at 0x408E77E: fread (in /lib/tls/libc-2.3.5.so)
==17837== by 0x8049C97: TestCreate (TestM1.c:316)
==17837== by 0x8048AF2: main (TestM1.c:94)
==17837==
==17837== ERROR SUMMARY: 16 errors from 16 contexts (suppressed: 11 from 1)
==17837== malloc/free: in use at exit: 2,112 bytes in 6 blocks.
==17837== malloc/free: 21 allocs, 15 frees, 52,464 bytes allocated.
==17837== For counts of detected errors, rerun with: -v
==17837== searching for pointers to 6 not-freed blocks.
==17837== checked 87,372 bytes.
==17837==
==17837==
==17837== 2,112 bytes in 6 blocks are still reachable in loss record 1 of 1
==17837== at 0x401A43A: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==17837== by 0x408E21E: __fopen_internal (in /lib/tls/libc-2.3.5.so)
==17837== by 0x408E2DC: fopen@@GLIBC_2.1 (in /lib/tls/libc-2.3.5.so)
==17837== by 0x804A86D: CreateDatabaseFile (LPC_CreateDBF.c:32)
==17837== by 0x8049354: TestCreate (TestM1.c:201)
==17837== by 0x8048AF2: main (TestM1.c:94)
==17837==
==17837== LEAK SUMMARY:
==17837== definitely lost: 0 bytes in 0 blocks.
==17837== possibly lost: 0 bytes in 0 blocks.
==17837== still reachable: 2,112 bytes in 6 blocks.
==17837== suppressed: 0 bytes in 0 blocks.
Segmentation fault
|
|
|
|
|
|
#15 |
|
Hero Member
Registrato: 15-10-2000
Messaggi: 588
![]() |
Gli errori non lo so se sono voluti sinceramente. Io ho programmato secondo le specifiche (spero di averlo fatto decentemente
|
|
|
|
![]() |
| Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti) | |
| Strumenti discussione | |
|
|
Discussioni simili
|
||||
| Discussione | Autore discussione | Forum | Risposte | Ultimo messaggio |
| Parole in musica | gemma | Chiacchiere in libertà | 415 | 03-06-2007 05.24.48 |
| GENERAL PROTECTION FAULT | Slith | Hardware e Overclock | 8 | 24-06-2005 20.51.57 |
| Quiz: Versi di canzoni | tisifone | Chiacchiere in libertà | 55 | 02-07-2004 19.13.03 |
| fault in ms-dos extender | marcofitaly | Windows 9x/Me/NT4/2000 | 1 | 27-05-2004 23.45.27 |