Up: Procedural Security Concerns   [Index]


6.13.1 Recursion

The procedural program interpreter does not make provisions for preventing stack overruns. There are available a number of options an application may take to preclude such an event, nonetheless.

One trivial option is to count the function call depth. Function call observers will be installed and the program interpretation will be terminated when the depth reaches a defined threshold.

See Call Observers.

To terminate interpretation it will be enough to have the function call enter observer return non zero.

aime function calls are supported by C function calls. The amount of space reserved on the stack by the supporting C functions does not reflect the definitions nor the declarations of the supported aime functions.

An alternate safe option to prevent the stack being overrun is to execute the program step by step.

See Step By Step Procedural Program Execution.

For such course, the depth of the C call stack will always stay small, likely counting in units, while at the same time allowing virtually unlimited deep aime recursion.

The aime program:

void
fx(integer p, integer c, real s)
{
    real w;

    s += c;
    c += 1;
    p -= 1;
    if (p) {
	fx(p, c, s);
    } else {
	o_real(8, s);
	o_byte(10);
    }
}

integer
main(void)
{
    fx(S, 1, 0);

    return 0;
}

will recurse as many times as indicated by the externally defined ‘S’. It may recurse 666666 times to print 222222111111, and it may recurse 6666666 times, take a lot of time and require a lot of memory (likely in excess of 1GB), but it will still print 22222221111111.