Zoé de Moffarts
Zoé de Moffarts

Reputation: 165

HEAP CORRUPTION DETECTED in C

I am having some problems with my program and getting this error : HEAP CORRUPTION DETECTED: before Normal block (#9873672) at 0x00968988. CRT detected that the application wrote to memory before start of heap buffer.

I have tried to look for fixes but I can't figure out what it wrong with my program, what to fix and where :( Here is the function I'm using and that is causing me problems : What I am doing is basically look into a file for a specific keyword (argument of the function getText) and printing its matching value. Sorry if most of the variables are in French, it's a project for school and our teacher require us to use French names >_<

#include "getText.h"
#include "main.h"
#include <stdlib.h>

texteLangue* ressourcesTexteLangue = NULL;
int compteur = 0;


char* getText(char* clef)
{
    char* texte = NULL;
    texte = clef; //clef is the keyword passed in the function as argument
    texteLangue temp;
    temp.clef = clef;
    texteLangue* resultat = (texteLangue*) bsearch(&temp, ressourcesTexteLangue, compteur, sizeof(texteLangue), comparerClef); //returns the value associated with the key

    if (clef != NULL)
    {
            if (resultat != NULL)
            texte = resultat->valeur;
    }
    return texte; 
}

void lectureTexte(char* langue)
{
    char nomFichierRessources[64];
    sprintf(nomFichierRessources, "ressources_%s.txt", langue); //give the file name a specific ending depending on the language chosen
    FILE* FichierRessources = fopen(nomFichierRessources, "r");
    if (FichierRessources == NULL)
    {
        system("cls");
        perror("The following error occured ");
        system("PAUSE");
        exit(42);
    }

        //allocates memory for the language resources 
    int taille = 10;
    ressourcesTexteLangue = (texteLangue *) calloc(taille, sizeof(texteLangue));
    if (ressourcesTexteLangue == NULL)
        printf("Pas assez de place mémoire pour les ressources texte");


        //gives a value to TextResource.key and TextResource.value for each line of the file
    char* ligne;
    while ((ligne = lectureLigne(FichierRessources)))
    {
        if (strlen(ligne) > 0)
        {
            if (compteur == taille)
            {
                taille += 10;
                ressourcesTexteLangue = (texteLangue *) realloc(ressourcesTexteLangue, taille * sizeof(texteLangue));
            }
            ressourcesTexteLangue[compteur].clef = ligne;

            while (*ligne != '=')
            {
                ligne++;
            }

            *ligne = '\0';
            ligne++;
            ressourcesTexteLangue[compteur].valeur = ligne;
            compteur++;
        }
     }
         //sorts out the values of TextResource obtained
     qsort(ressourcesTexteLangue, compteur, sizeof(texteLangue), comparerClef);

     fclose(FichierRessources);
}

 //reads a line and returns it
 char* lectureLigne(FILE *fichier)
 {
    int longeur = 10, i = 0, c = 0;
    char* ligne = (char*) calloc(longeur, sizeof(char));

    if (fichier)
    {
        c = fgetc(fichier);
        while (c != EOF)
        {
            if (i == longeur)
            {
                longeur += 10;
                ligne = (char*) realloc(ligne, longeur * sizeof(char));
            }
            ligne[i++] = c;
            c = fgetc(fichier);
            if ((c == '\n') || (c == '\r'))
                break;
        }

        ligne[i] = '\0';

        while ((c == '\n') || (c == '\r'))
            c = fgetc(fichier);
        if (c != EOF)
            ungetc(c,fichier);

        if ((strlen(ligne) == 0) && (c == EOF))
        {
            free(ligne);
            ligne = NULL;
        }
    }
    return ligne;
}

//frees the TextRessources
void libererTexte()
{
    if (ressourcesTexteLangue != NULL)
    {
        while (compteur--)
        {
            free(ressourcesTexteLangue[compteur].clef);

        }
        free(ressourcesTexteLangue);
    }
}

//compares the keys
int comparerClef(const void* e1, const void* e2)
{
    return strcmp(((texteLangue*) e1)->clef, ((texteLangue*) e2)->clef);
}

the structure of RessourceTextelangue (TextResources) look like this :

typedef struct texteLangue {
    char* clef;
    char* valeur;
} texteLangue;

Upvotes: 0

Views: 2129

Answers (2)

Dave Rager
Dave Rager

Reputation: 8150

Is your input guaranteed to contain a '=' in each line?

while (*ligne != '=') // if not, this will fly off the end of your buffer...
{
    ligne++;
}

*ligne = '\0'; // ...and write to unallocated heap memory

Edit

Given @Heath's comment, if your input contains a blank line (including ending with a single blank line) then the above would most certainly be triggered.

c = fgetc(fichier); // reads '\n'
while (c != EOF)
{
    ...

    ligne[i++] = c;

    ...

    ligne[i] = '\0';

ligne now contains "\n" and is returned. later it is used:

if (strlen(ligne) > 0) // strlen("\n") is greater than 0
{
    ...

    while (*ligne != '=') // oops! loop until we find a '=' somewhere
                          // in the heap or crash trying.
    {
        ligne++;
    }

    *ligne = '\0'; // corrupt the heap here

Upvotes: 1

Heath Hunnicutt
Heath Hunnicutt

Reputation: 19457

There are several potential problems with your code that could be causing the error report you see.

Here is one:

    if (i == longeur)

should be:

    if ((i+1) == longeur)

otherwise,

    ligne[i] = '\0';

can occur in conditions when

        ligne[i++] = c;

has caused i to become equal to longeur.

Here is another:

        while (*ligne != '=')
        {
            ligne++;
        }

        *ligne = '\0';

the above code should be:

        while (*ligne != '=' && *ligne != '\0')
        {
            ligne++;
        }

        *ligne = '\0';

otherwise, you will corrupt memory in the case when there is no '=' to be found in the string.

Although either of these could cause the symptom you report, I see some other oddities that make me think there is more wrong than I have seen so far. Nevertheless, fixing those two problems will at least reduce the number of possibilities you have to consider.

Upvotes: 1

Related Questions