Catching stack overflow

后端 未结 3 1066
无人共我
无人共我 2020-12-09 17:39

What\'s the best way to catch stack overflow in C?

More specifically:

A C program contains an interpreter for a scripting language.

Scripts are not t

3条回答
  •  一向
    一向 (楼主)
    2020-12-09 18:16

    Off the top of my head, one way to catch excessive stack growth is to check the relative difference in addresses of stack frames:

    #define MAX_ROOM    (64*1024*1024UL)    // 64 MB
    
    static char *   first_stack = NULL;
    
    void foo(...args...)
    {
        char    stack;
    
        // Compare addresses of stack frames
        if (first_stack == NULL)
            first_stack = &stack;
        if (first_stack > &stack  &&  first_stack - &stack > MAX_ROOM  ||
            &stack > first_stack  &&  &stack - first_stack > MAX_ROOM)
            printf("Stack is larger than %lu\n", (unsigned long)MAX_ROOM);
    
        ...code that recursively calls foo()...
    }
    

    This compares the address of the first stack frame for foo() to the current stack frame address, and if the difference exceeds MAX_ROOM it writes a message.

    This assumes that you're on an architecture that uses a linear always-grows-down or always-grows-up stack, of course.

    You don't have to do this check in every function, but often enough that excessively large stack growth is caught before you hit the limit you've chosen.

提交回复
热议问题