Bug 23409 – ImportC: multiple usages of va_list produces garbage

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2022-10-12T14:53:18Z
Last change time
2023-05-24T01:56:08Z
Keywords
ImportC
Assigned to
No Owner
Creator
dave287091
See also
https://issues.dlang.org/show_bug.cgi?id=21425

Comments

Comment #0 by dave287091 — 2022-10-12T14:53:18Z
#include <stdio.h> #include <stdarg.h> void printf10(const char* fmt, ...){ for(int i = 0; i < 10; i++){ va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); } } int main(){ printf10("Hello %s\n", "world"); return 0; } Example output of the above: $ ./vap Hello world Hello �Ƚ Hello �Ƚ Segmentation fault: 11 Segfault occurs within a call to strlen (as it is strlen-ing garbage).
Comment #1 by dave287091 — 2022-10-12T22:07:31Z
The D version also seg faults, so maybe D’s implementation of stdarg is buggy? import core.stdc.stdarg; import core.stdc.stdio; void printf10(const(char)* fmt, ...){ for(int i = 0; i < 10; i++){ va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); } } int main(){ printf10("Hello %s\n".ptr, "world".ptr); return 0; }
Comment #2 by dave287091 — 2022-10-12T22:12:11Z
(In reply to dave287091 from comment #1) > The D version also seg faults, so maybe D’s implementation of stdarg is > buggy? > > import core.stdc.stdarg; > import core.stdc.stdio; > > void printf10(const(char)* fmt, ...){ > for(int i = 0; i < 10; i++){ > va_list args; > va_start(args, fmt); > vprintf(fmt, args); > va_end(args); > } > } > > int main(){ > printf10("Hello %s\n".ptr, "world".ptr); > return 0; > } Forgot the extern(C) on printf10, but it still is buggy: $ ./vap Hello world Hello Segmentation fault: 11
Comment #3 by dave287091 — 2022-10-12T22:19:41Z
It works with ldc (which probably just defers to the llvm intrinsic) so the dmd implementation of stdarg is wrong. This is not actually ImportC specific.
Comment #4 by bugzilla — 2023-05-14T01:03:37Z
The problem probably has to do with putting va_start and va_end in a loop. I think it was written expecting it to be done only once.
Comment #5 by dave287091 — 2023-05-14T04:35:26Z
(In reply to Walter Bright from comment #4) > The problem probably has to do with putting va_start and va_end in a loop. I > think it was written expecting it to be done only once. The same problem occurs with manually unrolling the loop.
Comment #6 by bugzilla — 2023-05-23T05:27:13Z
ImportC uses the D implementation of va_list, so your deduction that the fault lies with the D implementation is most likely correct.
Comment #7 by tim.dlang — 2023-05-23T15:22:21Z
Here is a related issue for normal D: https://issues.dlang.org/show_bug.cgi?id=21425
Comment #8 by bugzilla — 2023-05-24T01:56:08Z
*** This issue has been marked as a duplicate of issue 21425 ***