[Issue 1679] New: D should cast char[] to char* when used in a variadic argument in extern(C) context
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Nov 19 20:11:02 PST 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1679
Summary: D should cast char[] to char* when used in a variadic
argument in extern(C) context
Product: D
Version: unspecified
Platform: PC
URL: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=451707
OS/Version: Linux
Status: NEW
Severity: critical
Priority: P5
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: arthur.loiret at gmail.com
The following code:
--------------------------------------------------
import std.c.process;
int main(char args[][]) {
execl("/bin/sh", "sh", "-c", "ls", null);
printf("%m\n");
return -1;
}
--------------------------------------------------
Doesn't work:
Bad address
zsh: exit 255 ./test
exelc prototype is:
extern(C) int exelc(char *, char *, ...) because if I do that:
And the following code works:
--------------------------------------------------
extern(C) int execl(char *path, char *arg, char*, char*, char*, ...);
int main(char args[][]) {
execl("/bin/sh", "sh", "-c", "ls", null);
printf("%m\n");
return -1;
}
--------------------------------------------------
So does this one:
--------------------------------------------------
import std.c.process;
int main(char args[][]) {
execl("/bin/sh", "sh", cast(char*)"-c", cast(char*)"ls", null);
printf("%m\n");
return -1;
}
--------------------------------------------------
Pierre Habouzit understood the issue, here is his explanation:
When the "-c" argument is sent as the first
argument of the variadic part, D sends a char[] and not a char*, meaning
that it puts 2, "-c" on the stack, which confuses the C.
I believe that D should do the implicit cast in that case, as it'll
break a _lot_ of programs using C APIs and variadic arguments in very
subtle ways.
--
More information about the Digitalmars-d-bugs
mailing list