Ben Ravenhall
Ben Ravenhall

Reputation: 1

Stack Smashing Detected - Aborted (core Dumped)

I cannot seem to find why this is stack smashing, the code is meant to read in some files, read each line and cat other lines on the end. but i get a stack smashing detected error at the very end of code.

Any ideas?

Code is :

void main (int argc, char *argv[])
{

char lineCount;
int count = 0;
size_t buffer_size = 40;
char *buffer =malloc(buffer_size * sizeof(char));
char *buffer2 =malloc(buffer_size * sizeof(char));
char *buffer3 =malloc(buffer_size * sizeof(char));
char *buffer4 =malloc(buffer_size * sizeof(char));
FILE *Dictionary, *Names;
Dictionary = fopen ("/home/overdog/Documents/Coding/dictionary.txt","r");
Names = fopen ("/home/overdog/Documents/Coding/rawnames.txt","r");
    while(-1 != getline(&buffer,&buffer_size,Dictionary))
    {
        count = count + 1;

        for (int i =1; i <= 10; i++)
        {
            memcpy(buffer2,buffer,buffer_size);
            char num[1];
            RemoveEndLine(buffer2);
            sprintf(num,"%d",i);
            strcat(buffer2,num);
            printf("%s\n",buffer2);
                while(-1 != getline(&buffer3,&buffer_size,Names))
                {
                    memcpy(buffer4,buffer2,buffer_size);
                    printf("before break\n");
                    strcat(buffer4,buffer3);
                    printf("%s",buffer4);




                }


        }



    }
printf("Lines = %d \n",count);
free(buffer);
free(buffer2);
free(buffer3);
free(buffer4);
fclose(Dictionary);
fclose(Names);
printf("test\n");
}

The output seems OK, and the print of "test" at the end of the code prints. And then the Stack smashing error is seen.

Upvotes: 0

Views: 4989

Answers (2)

Ben Ravenhall
Ben Ravenhall

Reputation: 1

thanks for all the help, what Some Programmer Dude said did help i think but i still had an issue. I found that the issue was the line

strcat(buffer4,buffer3);

As the buffer size for both was the same, it was creating a string which requires a buffer of 80?

i amended the line

char *buffer4 =malloc(buffer_size * sizeof(char));

to read

char *buffer4 =malloc(80 * sizeof(char));

And this now works without stack smashing

Thanks!

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409432

Lets take a close look at these two lines:

char num[1];
...
sprintf(num,"%d",i);

You declare num as an array of a single character, forgetting that (char) strings in C are really called null terminated byte strings. That means a string of a single character needs space for two char elements, to fit the terminator.

Since you don't have space for the terminator then sprintf will write out of bounds of your array, leading to undefined behavior and your smashed stack.

If you are certain that the number will never be more than a single digit (which it wont, it will include the two-digit number 10), then you need to have an array of at least two character elements.

I also recommend you use snprintf to avoid buffer overflows like that.

Upvotes: 4

Related Questions