Gderu
Gderu

Reputation: 131

My program unexpectedly stops running, and can't free a variable

I have made a program that scans for the contents of a file (in bytes) in another file. It used to work well, but then I learned that the output has to be sorted, so I added the variable sortOrder, and changed my program a bit so that it saves the data instead of just printing it out. The problem is that now my program reaches an error at random points while running. Another problem that I have is that for some reason I can't free the strings inside sortOrder in main, it just returns an error. I'm pretty sure the problem is either in main or statusOfFiles, although I could be wrong since I don't really understand the problem.

#define _CRT_SECURE_NO_WARNINGS

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>

#define ADD_NAME 1
#define MAX_LEN 200
#define ZERO_END 1
#define VIRUS 2
#define CLEAN 0
#define INFECTED 1
#define FIRST_PART 2
#define LAST_PART 3

void addResults(int foundDetails, char*** sortOrder, int i)
{
    if (foundDetails == CLEAN)
    {
        strcat((*sortOrder)[i], "Clean");
    }
    else if (foundDetails == INFECTED)
    {
        strcat((*sortOrder)[i], "Infected!");
    }
    else if (foundDetails == FIRST_PART)
    {
        strcat((*sortOrder)[i], "Infected! (first 20%%)");
    }
    else
    {
        strcat((*sortOrder)[i], "Infected! (last 20%%)");
    }
}

void statusOfFiles(char * filePath, char ** files, int lenFiles, char*** sortOrder)
{
    char file[MAX_LEN] = { 0 };
    int i = 0;
    for (i = 0; i < lenFiles; i++)
    {
        strcpy(file, filePath);
        strcat(file, "/");
        strcat(file, files[i]);
        *sortOrder = (char**)realloc((*sortOrder), (i + ADD_NAME) * sizeof(char*));
        (*sortOrder)[i] = (char*)malloc((strlen(file) + ZERO_END) * sizeof(char));
        strcat(strcpy((*sortOrder)[i], file), " - ");
        addResults(0, sortOrder, i);
    }
}

void printResultsInOrder(char** sortOrder, int len)
{
    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%s\n", sortOrder[i]);
    }
}

int main(void)
{
    int i = 0, len = 5;
    char *fileNames[] = {"adgv.txt", "fsagda", "adagda", "adga", "adgastgaet"}, **sortOrder = (char**)malloc(0);
    statusOfFiles("C:/folder", fileNames, len, &sortOrder);
    printResultsInOrder(sortOrder, len);
    for (i = 0; i < len; i++)
    {
        free(sortOrder[i]);
    }
    free(sortOrder);
    getchar();
    return 0;
}

Thanks for the help!

Upvotes: 0

Views: 55

Answers (1)

One Guy Hacking
One Guy Hacking

Reputation: 1301

Here:

(*sortOrder)[i] = (char*)malloc((strlen(file) + ZERO_END) * sizeof(char));

you malloc enough space to hold the string file plus 1 for the terminating null. The very next line:

strcat(strcpy((*sortOrder)[i], file), " - ");

you copy file into it (filling the space entirely) and then add " - " to the end of it, overrunning the buffer.

A style note that would likely have helped you find this on your own: packing as much code as you can into a single line makes code hard to read and offers no speed savings with modern compilers. I would have written this as

(*sortOrder)[i] = malloc((strlen(file) + ZERO_END) * sizeof(char));
strcpy((*sortOrder)[i], file);
strcat((*sortOrder)[i], " - ");

which makes the bug much more visible. Actually, because strdup() does the alloc and copy for you, I'd have probably written it as

(*sortOrder)[i] = strdup(file);
strcat((*sortOrder)[i], " - ");

which is pretty obviously wrong.

Upvotes: 1

Related Questions