Bug 16555 – Stack corruption when calling multi-parameter outer function from nested function
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-09-27T20:56:24Z
Last change time
2018-03-03T08:55:50Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Lars T. Kyllingstad
Comments
Comment #0 by bugzilla — 2016-09-27T20:56:24Z
When a nested function calls an outer function with a large number of parameters, the first argument becomes corrupted.
Test case:
void outer(
double x,
double a, double b, double c, double d,
double e, double f, double g, double h)
{
import std.stdio;
writefln("x=%s a=%s b=%s c=%s d=%s e=%s f=%s g=%s h=%s",
x, a, b, c, d, e, f, g, h);
}
void main()
{
void inner(double x)
{
outer(x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
}
inner(999.0);
}
Here, the value of x in outer() is garbage. That is, the program SHOULD print:
x=999 a=1 b=2 c=3 d=4 e=5 f=6 g=7 h=8
But instead, it prints something like:
x=6.95253e-310 a=1 b=2 c=3 d=4 e=5 f=6 g=7 h=8
Reducing the number of parameters of outer() makes the problem go away. Also, interestingly, if the program is compiled with -O or -inline, the correct behaviour is restored.
Comment #1 by schveiguy — 2016-09-27T22:33:08Z
The stack corruption I think is on the expected location of x when calling the function.
Note that outer function is not needed, this also produces (same) corrupt output:
import std.stdio;
void main()
{
void inner(double x)
{
if(x == 999) // to show that x itself isn't corrupt
{
writefln("x=%s a=%s b=%s c=%s d=%s e=%s f=%s g=%s h=%s", x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
//outer(x, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
}
}
inner(999.0);
}
Not sure how to edit the description, but definitely the number of parameters plays a role, and probably the inner function.