empserver/src/lib
Markus Armbruster 030b374db7 accept: Resize thread stack to avoid stack smash for small worlds
I observed a stack overflow in news command on my x86_64 system
running Fedora 18.

Empire 2 settled on this formula for the stack size:

	    stacksize = 100000
    /* budget */  + MAX(WORLD_SZ() * sizeof(int) * 7,
    /* power */ MAXNOC * sizeof(struct powstr));

Obviously attempts to provide space for known configuration-dependent
stack hogs.

The first hog is allegedly budget.  Bogus since day one: its large
arrays were static in Empire 2, and became dynamically allocated in
Empire 3.

The second one makes some sense: powe() has a struct powstr[MAXNOC].
It also has an int[MAXNOC], which isn't accounted for.

Except for ridiculously small worlds, the second term is smaller, and
only the (bogus) first term matters.

Two hogs are missing: head() has a struct histstr[MAXNOC][MAXNOC], and
news() has a short[MAXNOC][MAXNOC].  It also calls head().

I looked for more hogs with "gcc -fstack-usage", and found none.

On my x86_64 system, a news command needs almost 107KiB of stack.
Only slightly less when compiled for 32 bit.  Stack overrun for worlds
with fewer than some 320 sectors, thus unlikely to bite in real games.

Increase player stack size to 1 MiB.  Using MAXNOC to size the stack
isn't worth the trouble.

Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
2014-01-06 20:50:06 +01:00
..
commands Trim trailing white space from source code 2014-01-06 20:49:59 +01:00
common Update copyright notice 2014-01-02 14:33:48 +01:00
empthread Update copyright notice 2014-01-02 14:33:48 +01:00
gen Update copyright notice 2014-01-02 14:33:48 +01:00
global Update copyright notice 2014-01-02 14:33:48 +01:00
lwp Update copyright notice 2014-01-02 14:33:48 +01:00
player accept: Resize thread stack to avoid stack smash for small worlds 2014-01-06 20:50:06 +01:00
subs maps: Trim unwanted trailing space from map decoration 2014-01-06 20:49:59 +01:00
update Update copyright notice 2014-01-02 14:33:48 +01:00
w32 Update copyright notice 2014-01-02 14:33:48 +01:00