D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5698 - va_arg sets wrong length for (u)short[]
Summary: va_arg sets wrong length for (u)short[]
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-04 10:07 UTC by Mathias Baumann
Modified: 2015-06-09 01:31 UTC (History)
4 users (show)

See Also:


Attachments
test case demonstrating the problem (1.29 KB, application/octet-stream)
2011-03-04 10:07 UTC, Mathias Baumann
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Mathias Baumann 2011-03-04 10:07:48 UTC
Created attachment 929 [details]
test case demonstrating the problem

va_arg sets wrong length for ushort[]

the attached program outputs: 

vararg_:
ubyte[].length = 3
ushort[].length = 140737237065760
short[].length = 139885158469104
uint[].length = 3
vararg:
ubyte[].length = 3
ushort[].length = 0
short[].length = 0
uint[].length = 3


I tested with D1, but the bug is possibly also existant in D2.
Comment 1 Don 2011-03-15 02:04:42 UTC
When I compile the test code on D2 Windows (replacing tango.stdc.stdarg -> core.stdc.stdarg), I just get indication of memory corruption:

vararg_:
ubyte[].length = 18419671623532547
ushort[].length = 18419809062486019
short[].length = 18419860602093571
uint[].length = 18419912141701123


There's this code in the test case:

   T[] usa;
    va_arg(ap, _arguments[0],cast(void*) &usa); 

Are you sure this is correct? Looks wrong to me, remember that static arrays are passed by value.
Comment 2 Mathias Baumann 2011-03-15 03:41:30 UTC
I am not 100% sure, no. But I just replaced the array declerations with

    ubyte uba[] = [ 32,33,34];
    uint a[] = [1,2,3];
    ushort c[] = [ cast(ushort)51, 52, 53 ];
    short sc[] = [ cast(short)51, 52, 53 ];


and it resulted in the same problem.
I hope initializing them like this does qualify them as dynamic arrays. (?)
Comment 3 AndyC 2015-01-25 01:21:48 UTC
The example code (after changing tango.stdc.stdarg to core.stdc.stdarg) doesnt compile anymore  (DMD v2.066.1)

I'd guess there are newer/better ways to handle this, so I'd recommend closing it.


I get:
/usr/include/dmd/druntime/import/core/stdc/stdarg.d(67): Error: cannot implicitly convert expression (cast(void*)(cast(uint)p + (tsize + 4u - 1u & 4294967292u))) of type void* to char*
main.d(15): Error: template instance core.stdc.stdarg.va_arg!() error instantiating
main.d(57):        instantiated from here: varar_!ubyte
Comment 4 Steven Schveighoffer 2015-01-26 12:46:16 UTC
I downloaded attachment, changed tango to core in both cases, compiles and runs with 2.066.1, and outputs:

vararg_:
ubyte[].length = 3
ushort[].length = 3
short[].length = 3
uint[].length = 3
vararg:
ubyte[].length = 3
ushort[].length = 3
short[].length = 3
uint[].length = 3

On Macos X (64-bit).