Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is __stack_chk_fail happening in my code?

Tags:

c

macos

Regarding following function, my debugger shows me __stack_chk_fail when finishing this function.

My system is Mac OS.

It is because my stack overflowed itself by checking references.

Also based on my experiment, if set vocab_size = 30000 it shows __stack_chk_fail error but when vocab_size = 20000 it is fine.

So I believe

vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));

is the issue. But malloc allocate memory on the heap rather than stack, so I am wondering where I goes wrong?

void populate_vocab(){
    FILE *fin;
    fin = fopen(word_file, "rb");
    vocab = (struct vocab_word *)malloc ((size_t) ((vocab_size + 1) * sizeof(struct vocab_word)));
    char word[MAX_STRING];
    int word_idx = 0;
    int num = 0;
    boolean word_mode = 1;
    long long cur_vocab_size = 0;

    while (!feof(fin)) {
        ch = fgetc(fin);

        if(ch == ' '){
            word_mode = 0;
        }else if(ch == '\n'){
            word_mode = 1;
            word[word_idx] = 0;
            vocab[cur_vocab_size].word = (char *)calloc(word_idx, sizeof(char));
            strcpy(vocab[cur_vocab_size].word,word);
            vocab[cur_vocab_size].cn = num;
            cur_vocab_size++;
            if (cur_vocab_size >= vocab_size){
                break;
            }
            //fresh var
            word_idx = 0;
            num = 0;

        }else{
            if(word_mode){
                word[word_idx] = ch;
                word_idx ++;
            }else{
                num = num * 10;
                num += ch - '0';
            }
        }
    }
    fclose(fin);
}
like image 304
Sanqiang Zhao Avatar asked Jul 17 '16 05:07

Sanqiang Zhao


People also ask

What is __ Stack_chk_fail?

__stack_chk_fail , a callback function that is invoked when a stack buffer overflow is detected. This function shall abort the function that called it with a message that a stack buffer overflow has been detected, and then halt the program via exit , abort , or a custom panic handler.

How do I stop stack overflow?

One method to prevent stack overflow is to track the stack pointer with test and measurement methods. Use timer interrupts that periodically check the location of the stack pointer, record the largest value, and watch that it does not grow beyond that value.

What is a stack overflow in C++?

Stack overflow is a software bug which occurs when a program tries to access more memory than the available stack size, which results in the crashing of the program. Stack is Last in First Out data structure (LIFO).


1 Answers

Based on comments, I figured out the reason. One of words exceed MAX_STRING which cause stack overflowed.

like image 55
Sanqiang Zhao Avatar answered Nov 15 '22 19:11

Sanqiang Zhao