Is it possible to predict a stack overflow in C on Linux?

前端 未结 12 1525
遇见更好的自我
遇见更好的自我 2021-01-13 14:44

There are certain conditions that can cause stack overflows on an x86 Linux system:

  • struct my_big_object[HUGE_NUMBER] on the stack. Walking throu
12条回答
  •  礼貌的吻别
    2021-01-13 15:42

    alloca() is going to return NULL on failure, I believe the behavior of alloca(0) is undefined and platform variant. If you check for that prior to do_something(), you should never be hit with a SEGV.

    I have a couple of questions:

    1. Why, oh why, do you need something that big on the stack? The default size on most systems is 8M, that's still too small?
    2. If the function calling alloca() blocks, would protecting the same amount of heap via mlock() / mlockall() guarantee close to the same access performance (i.e. "Don't swap me, bro!") over time? If your using a more aggressive 'rt' scheduler, its recommended to call those anyway.

    The question is interesting but raises an eyebrow. It raises the needle on my square-peg-round-hole-o-meter.

提交回复
热议问题