#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.