Memory leaks problems using valgrind even after fclose - c

I'm having problems with valgrind,the problem is in this function where I have memory leaks apparently.
void lectureFichier(const char * nomFichier, message ** tete){
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))==2)
{
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
free(test);
exit(EXIT_FAILURE);
}
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
fclose(fp);
}
Take a look at valgrind result:
I can't find the problem. Do you have any suggestion?
HERE'S THE WHOLE CODE THAT CONTAINS THE FONCTION ABOVE ,WHEN I USE VALGRIND WITH THIS CODE IT GIVES THE RESULT AS SHOWN IN THE PICTURE ABOVE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tp1.h"
message * creationCellule(){
message * cellule=NULL;
free(cellule);
cellule=malloc(sizeof(message));
if(!cellule)
{
perror("malloc failes\n");
exit(EXIT_FAILURE);
}
cellule -> dateDeb = 0;
cellule -> dateFin = 0;
cellule -> suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
return cellule;
}
/*bejme recherche per date de deb se eshte trie nga date deb*/
message * recherche(message * tete, int date){
message ** prec = tete;
message * cour = *tete;
while( cour != NULL && cour -> dateDeb < date){
prec = &(cour -> suivant);
cour = cour -> suivant;
}
return prec;
}
/*la prochaine fois il faut venir avec un makefile*/
void insertion(message ** tete, message * cellule){
(void)tete;
(void)cellule;
message ** prec;
if(cellule != NULL){
prec = recherche(tete, cellule -> dateDeb);
cellule -> suivant = *prec;
*prec = cellule;
}
}
/*duhet te lexojme fichier ne fillim dhe kete fichier do e krijojme ne vete*/
void lectureFichier(const char * nomFichier, message ** tete){
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))==2)
{
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
free(test);
exit(EXIT_FAILURE);
}
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
fclose(fp);
}
void affichageListe(message ** tete)
{
if(tete)
{
message * tmp = *tete;
while( tmp )
{
//printf("jam ktu\n");
printf("DateDeb = %d \n", tmp -> dateDeb);
printf("DateFin = %d \n", tmp -> dateFin);
printf("Text = %s \n", tmp -> text);
tmp = tmp -> suivant;
}
}
}
void suppression(message**tete,int valeur,int dateDeb)
{
message **prec;
prec=recherche(tete,dateDeb);
if((*prec)!=NULL && (*prec)->dateFin==valeur)
{
(*prec)=(*prec)->suivant;
}
}
//EXERCICE 3
void supprimeObsoletes(message **tete)
{
message *pt=*tete;
time_t temps;
struct tm *date;
int intNum;
temps=time(NULL);
date=localtime(&temps);
char buffer[9];
if((date->tm_mon)<10){
sprintf(buffer,"%d0%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
else{
sprintf(buffer,"%d%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
intNum=atoi(buffer);
while(pt!=NULL)
{
if((pt->dateFin)<intNum)
{
printf("KTU HYB %s\n",pt->text);
suppression(tete,pt->dateFin,pt->dateDeb);
}
pt=pt->suivant;
}
}
void changeDate(int dateChange, int dateInit,message **tete)
{
message *point=*tete;
//printf("Kjo eshte tete %p:\n",(*point));
while(point!=NULL)
{
if((point->dateDeb)==dateInit)
{
printf("%d\n",point->dateDeb);
printf("%s\n",point->text);
point->dateDeb=dateChange;
}
point=point->suivant;
}
}
int main(int argc, char * argv[])
{
const char * name=argv[1];
message * pointeur = NULL;
//message ** tete = &pointeur;
int dateInit=19973012;
int dateChange=20003008;
lectureFichier(name, &pointeur);
supprimeObsoletes(&pointeur);
affichageListe(&pointeur);
while(pointeur)
{
message *current=pointeur;
pointeur=pointeur->suivant;
free(current);
}
return 0;
}

Your valgrind log shows "fscanf of first two" as part of the program output. That part happens when the fscanf call fails inside of the loop. You call exit but don't close the file. You need to close the file first:
if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
{
fprintf(stderr,"fscanf of first two\n");
fclose(fp);
free(test);
exit(EXIT_FAILURE);
}

void lectureFichier(const char * nomFichier, message ** tete)
{
message * test=creationCellule();
FILE * fp = fopen(nomFichier, "r");
if(!fp)
{
perror("fopen failed\n");
exit( EXIT_FAILURE );
}
while(fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin))==2)
{
// Here you are reading again. That's not what you want.
// if(fscanf(fp,"%d %d ",&(test->dateDeb), &(test->dateFin))!=2)
// {
// fprintf(stderr,"fscanf of first two\n");
// free(test);
// exit(EXIT_FAILURE);
// }
fgets(test->text,100,fp);
insertion(tete,test);
test=creationCellule();
}
// Here you can free the last test, because it is not used
free(test);
fclose(fp);
}

If your problem is memory leaks then you should use free(fp) for dyamically allocated pointers.

Related

Debugging: Problem with linked list linkage

I have a linked list implementation as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 128
typedef struct blob blob;
struct blob {
char text[CAPACITY];
struct blob* prev;
struct blob* next;
};
static blob* create_blob() {
blob* newBlob = malloc(sizeof(blob));
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
newBlob->prev = 0;
newBlob->next = 0;
memset(newBlob->text, 0, CAPACITY);
return newBlob;
}
int insertAtTail(blob* list[static 1]) {
blob* newBlob = create_blob();
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
if (!*list) {
*list = newBlob;
return 1;
}
blob* lastElement = *list;
while (lastElement->next) {
// Check if linkage until last element is okay
if (lastElement != (lastElement->next)->prev) {
fprintf(stderr, "%s\n", "The next_element->prev does not point to the current element");
} else {
fprintf(stderr, "%s\n", "Okay");
}
lastElement = lastElement->next; // Find last Blob
}
lastElement->next = newBlob;
newBlob->prev = lastElement;
return 1;
}
int reversePrintBlobs(blob const list[static 1]) {
if (!list) {
fprintf(stderr, "%s\n", "list is empty");
return 1;
}
const blob* tmp = list;
size_t blobCounter = 1;
while (tmp->next) {
blobCounter++;
tmp = tmp->next;
}
while (tmp && tmp != list) {
if ((tmp->prev)->next != tmp) {
fprintf(stderr, "%s\n", "error with the linkage");
exit(1);
}
printf("#number = %zu: %s\n", blobCounter--, tmp->text);
tmp = tmp->prev;
}
return 0;
}
When I add some blobs with the following main() function, it seems like the prev member of my struct doesn't point to the previous element. That is, there is a problem with the linkage. Can anyone point out why?
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
tmp = tmp->next;
}
}
}
On the other hand, If I use the following main() everything works fine.
int main() {
blob* list = create_blob();
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
/* code */
return 0;
}
The bug has been fixed, the new main function is the following:
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
int blobCounter = 1;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead - 1] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
blobCounter++;
tmp = tmp->next;
}
}

Binary Search Tree, recursive function Exsists throw "core dumped" in comparation strings

I have the following the following structures to define an Binary Search Tree:
typedef struct Equipo {
char nombre[50];
char marcaMoto[30];
int puntuaciones;
struct Piloto pilotos[18];
struct nodo* izquierdo;
struct nodo* derecho;
} Equipo;
typedef Equipo Arbol;
the search in the tree will be performed by the string nombre, a char [50]. For create tree and insert data I use the functions:
Equipo* CrearEquipo(Equipo* e) {
Equipo* n = (Equipo *) malloc(sizeof(Equipo));
strncpy(n->nombre, e->nombre, 50);
strncpy(n->marcaMoto, e->marcaMoto, 30);
n->puntuaciones = 0;
n->derecho = n->izquierdo = NULL;
return n;
}
void InsertarEquipo(Equipo** arbol, Equipo* e) {
if (*arbol == NULL) {
Equipo* n = CrearEquipo(e);
*arbol = n;
} else {
int comparado = strncmp(e->nombre, (*arbol)->nombre, 50);
if (comparado > 0) {
InsertarEquipo(&(*arbol)->izquierdo, e);
} else {
InsertarEquipo(&(*arbol)->derecho, e);
}
}
}
And in main i use this functions to create test elements:
Equipo* equipo = (Equipo *) malloc(sizeof(Equipo));
strcpy(equipo->nombre, "C");
strcpy(equipo->marcaMoto, "B");
Arbol *arbol = CrearEquipo(equipo);
strcpy(equipo->nombre, "B");
strcpy(equipo->marcaMoto, "B");
InsertarEquipo(&arbol, equipo);
strcpy(equipo->nombre, "A");
strcpy(equipo->marcaMoto, "B");
InsertarEquipo(&arbol, equipo);
strcpy(equipo->nombre, "E");
strcpy(equipo->marcaMoto, "B");
InsertarEquipo(&arbol, equipo);
Later, I create the recursive function for comprobate if exists in the tree:
int ExisteEquipo(Equipo* arbol, char nombre[]) {
int comparado = strncmp(arbol->nombre, nombre, 50);
if (!arbol) {
return 0;
} else if (comparado > 0) {
printf("Menor ");
return ExisteEquipo(arbol->izquierdo, nombre);
} else if (comparado < 0) {
printf("Mayor ");
return ExisteEquipo(arbol->derecho, nombre);
} else {
printf("Igual ");
return 1;
}
}
(The printf's are for test). When I call the exists function with:
void DeterminarExistencia(Equipo* arbol, char nombre[50]) {
if (ExisteEquipo(arbol, nombre)) {
printf("El nodo %s existe. \n", nombre);
} else {
printf("El nodo %s no existe. \n", nombre);
}
}
DeterminarExistencia(arbol, "E");
DeterminarExistencia(arbol, "C");
DeterminarExistencia(arbol, "H");
DeterminarExistencia(arbol, "B");
but I always get the error: Violación de segmento (core dumped) [Núcleo vaciado a un archivo]
I think the problem is here:
int comparado = strncmp(arbol->nombre, nombre, 50);
You are asking if arbol is null after operating with it with the line above, so if it is null you are accessing a wrong memory address and that is causing the error.
Put it like this:
if (!arbol) {
return 0;
int comparado = strncmp(arbol->nombre, nombre, 50);
Spanish:
Básicamente cambia el orden de lo que te he dicho arriba y deberia funcionar.

malloc in a loop using valgrind

I am having troubles with memory leaks (error detected by valgrind).
So here is my first function:
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
It returns cellule which is allocated by malloc.
Now I have this:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp, "%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
In which I call creationCellule() in a loop.
My problem is that, if I put free(test) inside a loop I lose all the context of my code and valgrind shows me ERROR SUMMARY:213 errors from 19 contexts.
What should I do ?
This is the complete code and the valgrind output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "tp1.h"
message *creationCellule() {
message *cellule;
cellule = (message *)malloc(sizeof(message));
if (cellule != NULL) {
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
memset(cellule->text, '\0', TAILLE_MAX);
}
return cellule;
}
message **recherche(message *tete, int date) {
message **prec = tete;
message *cour = *tete;
while (cour != NULL && cour->dateDeb < date) {
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message **tete, message *cellule) {
message **prec;
if (cellule != NULL) {
prec = recherche(tete, cellule->dateDeb);
cellule->suivant = *prec;
*prec = cellule;
}
}
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
message *test;
test = creationCellule();
int k = 0;
if (fp != NULL) {
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
while (k != EOF) {
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
k = fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin));
}
}
}
void affichageListe(message **tete) {
if (tete != NULL) {
message *tmp = *tete;
while (tmp != NULL) {
//printf("jam ktu\n");
printf("DateDeb = %d \n", tmp->dateDeb);
printf("DateFin = %d \n", tmp->dateFin);
printf("Text = %s \n", tmp->text);
tmp = tmp->suivant;
}
}
}
void suppression(message **tete, int valeur, int dateDeb) {
message **prec;
prec = recherche(tete, dateDeb);
//printf("Prec text: %s , prec dateFin: %d\n", (*prec)->text, (*prec)->dateFin);
if ((*prec) != NULL && (*prec)->dateFin == valeur) {
(*prec) = (*prec)->suivant;
}
}
void supprimeObsoletes(message **tete) {
message *pt = *tete;
time_t temps;
struct tm *date;
int intNum;
temps = time(NULL);
date = localtime(&temps);
char buffer[9];
if ((date->tm_mon) < 10) {
sprintf(buffer, "%d0%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
} else {
sprintf(buffer, "%d%d%d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday);
}
intNum = atoi(buffer);
while (pt != NULL) {
if ((pt->dateFin) < intNum) {
printf("KTU HYB %s\n", pt->text);
suppression(tete, pt->dateFin, pt->dateDeb);
}
pt = pt->suivant;
}
}
void changeDate(int dateChange, int dateInit, message **tete) {
message *point = *tete;
//printf("Kjo eshte tete %p:\n", (*point));
while (point != NULL) {
if ((point->dateDeb) == dateInit) {
printf("%d\n", point->dateDeb);
printf("%s\n", point->text);
point->dateDeb = dateChange;
}
point = point->suivant;
}
}
int main(int argc, char *argv[]) {
const char *name = argv[1];
message *pointeur = NULL;
message **tete = &pointeur;
int dateInit = 19973012;
int dateChange = 20003008;
FILE *fp = fopen(name, "r");
lectureFichier(name, tete);
//changeDate(dateChange, dateInit, tete);
supprimeObsoletes(tete);
affichageListe(tete);
return 0;
}
The header: tp1.h
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
typedef struct cell {
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell *suivant;
} message;
message *creationCellule();
message **recherche(message **tete, int date);
void affichageListe(message **tete);
void insertion(message **tete, message *cellule);
void lectureFichier(const char * nomFichier, message **tete);
The .txt file (added in execution)
19973012 20220512 TEXT 1
19980511 19001203 THIS
20011102 20301123 HOUSE
20020809 20301025 HELP
Valgrind output:
Function lectureFichier does not free unused nodes properly.
Here is a corrected version:
void lectureFichier(const char *nomFichier, message **tete) {
FILE *fp = fopen(nomFichier, "r");
if (fp != NULL) {
for (;;) {
message *node = creationCellule();
if (fscanf(fp, "%d%d %99s", &node->dateDeb, &node->dateFin, node->text) == 3) {
insertion(tete, node);
} else {
free(node);
break;
}
}
}
}
the following proposed code:
cleanly compiles
properly handles errors
removed unused code
does NOT cleanup the linked list when an error occurs -- you will need to add that
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifndef TP1_TEST_H
#define TP1_TEST_H
#define TAILLE_MAX 100
struct cell
{
int dateDeb;
int dateFin;
char text[TAILLE_MAX];
struct cell * suivant;
};
typedef struct cell message;
message * creationCellule( void );
message ** recherche( message ** tete, int date );
void affichageListe( message ** tete );
FILE * ecrireFichier( message ** tete );
void dateNonExpires( message ** tete );
#endif // TPI_TEST_H
message * creationCellule()
{
message * cellule = malloc(sizeof(message));
if( !cellule )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
cellule->dateDeb = 0;
cellule->dateFin = 0;
cellule->suivant = NULL;
return cellule;
}
message ** recherche( message ** tete, int date )
{
message ** prec = tete;
message * cour = *tete;
while( cour != NULL && cour->dateDeb < date )
{
prec = &(cour->suivant);
cour = cour->suivant;
}
return prec;
}
void insertion(message ** tete, message * cellule)
{
(void)tete;
(void)cellule;
// insert code that does something reasonable
}
void lectureFichier(const char * nomFichier, message ** tete)
{
FILE * fp = fopen(nomFichier, "r");
if( !fp )
{
perror( "fopen failed" );
exit( EXIT_FAILURE );
}
test = creationCellule();
while( fscanf(fp,"%d %d ", &(test->dateDeb), &(test->dateFin)) == 2 )
{
fgets(test->text, 100, fp);
insertion(tete, test);
test = creationCellule();
}
}
void affichageListe( message ** tete )
{
if( tete )
{
message * tmp = *tete;
while( tmp )
{
//printf("jam ktu\n");
printf( "DateDeb = %d \n", tmp->dateDeb );
printf( "DateFin = %d \n", tmp->dateFin );
printf( "Text = %s \n", tmp->text );
tmp = tmp->suivant;
}
}
}
void suppression( message**tete, int valeur, int dateDeb )
{
message **prec;
prec = recherche( tete, dateDeb );
//printf("Prec text: %s , prec dateFin: %d\n",(*prec)->text,(*prec)->dateFin);
if( (*prec) != NULL && (*prec)->dateFin == valeur )
{
(*prec)=(*prec)->suivant;
}
}
void supprimeObsoletes(message **tete)
{
message *pt = *tete;
time_t temps;
struct tm *date;
temps=time(NULL);
date=localtime(&temps);
char buffer[9];
if((date->tm_mon)<10)
{
sprintf(buffer,"%d0%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
else
{
sprintf(buffer,"%d%d%d",date->tm_year + 1900,date->tm_mon +1,date->tm_mday);
}
int intNum=atoi(buffer);
while( pt )
{
if( (pt->dateFin) < intNum )
{
printf( "KTU HYB %s\n", pt->text );
suppression( tete, pt->dateFin, pt->dateDeb );
}
pt=pt->suivant;
}
}
int main(int argc, char * argv[])
{
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s filename\n", argv[0] );
exit( EXIT_FAILURE );
}
const char * name = argv[1];
message * pointeur = NULL;
lectureFichier( name, &pointeur );
supprimeObsoletes( &pointeur );
affichageListe( &pointeur );
while( pointeur )
{
message *current = pointeur;
pointeur = pointeur->suivant;
free( current );
}
return 0;
}

Malloc and Realloc crash in C?

i have to do with my friend a program in C for my school.
The problem is, when i would malloc a pointer, it doesn't work, and the application will crashed.
But not in debug mod. In debug mod, it works.
This is a part of my code:
#include <bplus.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOG "log.txt"
#define LOGIDX "log.idx"
struct event {
time_t nb;
char *username;
int type;
char *message;
};
struct eventlist {
struct event event;
struct eventlist *nextelem;
};
struct eventlist *getEventList(time_t date);
void insert(struct eventlist *list, struct eventlist *event);
struct event getEventFromString(char *str);
char *getLine(FILE *file);
int file_exists(const char *fname);
char *dechiffrer(const char *pChaineChiffree);
int main(void) {
time_t timenow = time(NULL);
struct tm *tm = gmtime(&timenow);
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
time_t time = mktime(tm);
struct eventlist *list = getEventList(time);
return 0;
}
struct eventlist *getEventList(time_t date) {
int end = 0;
FILE *file = NULL;
char str[20];
char *line = NULL;
char *uncode = NULL;
ENTRY e;
IX_DESC faddress;
struct eventlist *list = NULL; // Liste a retourner
struct event *event = NULL; // Contient l'evenement
struct eventlist *templist = NULL; // Contient l'evenement a mettre dans list
// On ouvre / crée le fichier .idx
if (file_exists(LOGIDX))
open_index(LOGIDX, &faddress, 0);
else
make_index(LOGIDX, &faddress, 0);
// On ouvre le fichier de log
if ((file = fopen(LOG, "rb")) != NULL) {
// On met dans e.key le temps
sprintf(str, "%d", (int) date);
strcpy(e.key, str);
if (find_key(&e, &faddress)) { // Si la clé existe
fseek(file, e.recptr, SEEK_SET); // On se positionne
line = getLine(file); // On récupère la ligne
while (!feof(file) && !end) { // Boucle principale
printf("\ngetEventList 1");
if (line != NULL) {
uncode = dechiffrer(line); // On déchiffre la ligne
printf("\ngetEventList 2");
event = (struct event *) malloc(sizeof(struct event *) * 1); // On alloue de la place
printf("\ngetEventList 3");
if (event) {
*event = getEventFromString(uncode); // On la transforme en structure
printf("\ngetEventList 4");
if (event->nb < date + 86400) {
templist = (struct eventlist *) malloc(sizeof(struct eventlist *) * 1);
printf("\ngetEventList 5");
if (templist) {
templist->event = *event;
templist->nextelem = NULL;
printf("\ngetEventList 6");
if (list == NULL)
list = templist;
else
insert(list, templist);
printf("\ngetEventList 7");
line = getLine(file); // On récupère la ligne
printf("\ngetEventList 8");
} else {
list = NULL;
end = 1;
}
} else
end = 1;
} else {
list = NULL;
end = 1;
}
} else
end = 1;
}
} else { // Sinon, on affiche un message
list = NULL;
printf("\nErreur: Clé non trouvée !");
}
fclose(file);
} else {
list = NULL;
printf("\nErreur lors de l'ouverture du fichier !");
}
return list;
}
void insert(struct eventlist *list, struct eventlist *event) {
struct eventlist *temp = list;
struct eventlist *lasttemp = NULL;
printf("\n(%s %s)", temp->event.username, event->event.username);
while (temp->nextelem != NULL && stricmp(temp->event.username, event->event.username)) {
temp = temp->nextelem;
}
lasttemp = temp;
while (temp != NULL && !stricmp(temp->event.username, event->event.username)) {
lasttemp = temp;
temp = temp->nextelem;
}
event->nextelem = temp;
lasttemp->nextelem = event;
}
struct event getEventFromString(char *str) {
struct event event;
event.nb = 0;
event.type = 0;
event.username = NULL;
event.message = NULL;
int time;
int type;
char *username = (char *) malloc(sizeof(char *) * strlen(str));
char *message = (char *) malloc(sizeof(char *) * strlen(str));
if (sscanf(str, "%d %d %s %[^\n]s", &(time), &(type), username, message)) {
event.nb = (time_t) time;
event.type = type;
event.username = username;
event.message = message;
}
return event;
}
char *getLine(FILE *file) {
char *line = NULL;
unsigned char c;
int end = 0;
int ln = 0;
printf("\ngetLine 1");
line = (char *) malloc(sizeof(char *) * 1);
printf("\ngetLine 2");
if (line != NULL) {
while(!feof(file) && !end) {
c = fgetc(file);
if (c != '\n' && c != '\r') {
printf("\nDEBUG: %c %d %s", c, ln, line);
line = (char *) realloc(line, sizeof(char *) * (ln + 2));
if (line != NULL) {
line[ln++] = c;
line[ln] = '\0';
} else
end = 1;
} else
end = 1;
}
line[ln] = '\0';
}
if (line[0] == '\0' || line[1] == '\0')
line = NULL;
return line;
}
int file_exists(const char *fname) {
FILE *file;
int returncode = 0;
if ((file = fopen(fname, "r"))) {
fclose(file);
returncode = 1;
}
return returncode;
}
char *dechiffrer(const char *pChaineChiffree) {
char *dechiff;
unsigned char car;
unsigned int i;
dechiff = malloc(strlen(pChaineChiffree) + 1);
for (i = 0; pChaineChiffree[i] != '\0'; i++) {
car = pChaineChiffree[i];
car = (car & 0x0F) << 4 | (car & 0xF0) >> 4;
// car -= 0x55;
dechiff[i] = car;
}
dechiff[i] = '\0';
return dechiff;
}
I think it's a bufferoverflow, but i don't know where is the problem, and why it's bugged.
The crash occured in this malloc:
printf("\ngetLine 1");
line = (char *) malloc(sizeof(char *) * 1);
printf("\ngetLine 2");
Please help me
Thanks
0ddlyoko
EDIT:
Ok i've found the problem, it was with all my sizeof(XXX *), i've just changed this to sizeof(XXX).
Thanks !

Problems with this stack implementation

where is the mistake?
My code here:
typedef struct _box
{
char *dados;
struct _box * proximo;
} Box;
typedef struct _pilha
{
Box * topo;
}Stack;
void Push(Stack *p, char * algo)
{
Box *caixa;
if (!p)
{
exit(1);
}
caixa = (Box *) calloc(1, sizeof(Box));
caixa->dados = algo;
caixa->proximo = p->topo;
p->topo = caixa;
}
char * Pop(Stack *p)
{
Box *novo_topo;
char * dados;
if (!p)
{
exit(1);
}
if (p->topo==NULL)
return NULL;
novo_topo = p->topo->proximo;
dados = p->topo->dados;
free(p->topo);
p->topo = novo_topo;
return dados;
}
void StackDestroy(Stack *p)
{
char * c;
if (!p)
{
exit(1);
}
c = NULL;
while ((c = Pop(p)) != NULL)
{
free(c);
}
free(p);
}
int main()
{
int conjunto = 1;
char p[30], * v;
int flag = 0;
Stack *pilha = (Stack *) calloc(1, sizeof(Stack));
FILE* arquivoIN = fopen("L1Q3.in","r");
FILE* arquivoOUT = fopen("L1Q3.out","w");
if (arquivoIN == NULL)
{
printf("Erro na leitura do arquivo!\n\n");
exit(1);
}
fprintf(arquivoOUT,"Conjunto #%d\n",conjunto);
while (fscanf(arquivoIN,"%s", p) != EOF )
{
if (pilha->topo == NULL && flag != 0)
{
conjunto++;
fprintf(arquivoOUT,"\nConjunto #%d\n",conjunto);
}
if(strcmp(p, "return") != 0)
{
Push(pilha, p);
}
else
{
v = Pop(pilha);
if(v != NULL)
{
fprintf(arquivoOUT, "%s\n", v);
}
}
flag = 1;
}
StackDestroy(pilha);
return 0;
}
The Pop function returns the string value read from file.
But is not correct and i don't know why.
You're not allocating any storage for the strings pointed to by dados - you're just re-using one string buffer (p) and passing that around, so all your stack elements just point to this one string.

Resources