当前位置:  开发笔记 > 运维 > 正文

是否有可能在Linux上预测C中的堆栈溢出?

如何解决《是否有可能在Linux上预测C中的堆栈溢出?》经验,为你挑选了1个好方法。



1> Paul..:

您可以通过查找进程堆栈空间的大小然后减去使用的数量来确定进程可用的堆栈空间.

ulimit -s

显示了linux系统上的堆栈大小.对于程序化方法,请查看getrlimit().然后,要确定当前堆栈深度,请从一个到底部减去指向堆栈顶部的指针.例如(代码未经测试):

unsigned char *bottom_of_stack_ptr;

void call_function(int argc, char *argv) {
    unsigned char top_of_stack;
    unsigned int depth = (&top_of_stack > bottom_of_stack_ptr) ? 
        &top_of_stack-bottom_of_stack_ptr : 
        bottom_of_stack_ptr-&top_of_stack;

    if( depth+100 < PROGRAMMATICALLY_DETERMINED_STACK_SIZE ) {
        ...
    }
}

int main(int argc, char *argv) {
    unsigned char bottom_of_stack;
    bottom_of_stack_ptr = &bottom_of_stack;
    my_function();
    return 0;
}

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有