www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22960] New: importC: K&R-style functions assume variadic


          Issue ID: 22960
           Summary: importC: K&R-style functions assume variadic calling
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: ImportC
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: duser neet.fi
                CC: duser neet.fi

on 64-bit linux/posix:

// file1.c
void other(int);
long return_arg1(long x) { return x; }
int main()
        return_arg1(-1); // put 0xff in AL
        return 0;

// file2.c
void other(x)
        int x;
        // never reached

compile using "dmd file1.c file2.c", run to observe segfault

it crashes because of the variadic function prologue in other():


 If the callee is a variadic function, then the number of floating point
arguments passed to the function in vector registers must be provided by the
caller in the AL register.
it's called through a non-variadic prototype and the body is in a different file so dmd doesn't know AL has to be cleared first the segfault is because the variadic code in other() does a jump depending on the value of AL to only save the used registers, which fails if AL contains a garbage value other than 0-8 the zlib library has function bodies in K&R style and assumes that this works if __STDC_VERSION__ is defined (function prototypes in headers will contain the parameters instead of () in that case). the zlib bindings in phobos also don't use variadic functions so they wouldn't work with a dmd-compiled zlib because of this --
Mar 29 2022