Bug 1679 – D should cast char[] to char* when used in a variadic argument in extern(C) context

Status
RESOLVED
Resolution
WONTFIX
Severity
critical
Priority
P5
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2007-11-19T22:10:00Z
Last change time
2015-06-09T05:14:55Z
Assigned to
bugzilla
Creator
arthur.loiret

Comments

Comment #0 by arthur.loiret — 2007-11-19T22:10:58Z
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.
Comment #1 by braddr — 2007-11-19T22:56:17Z
On Tue, 20 Nov 2007, [email protected] wrote: > 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. It would only potentially be safe to implicitly do this for literals. In D, char[]'s don't necessairily have a null terminating byte. Literals do. This implicit cast used to exist and was removed due to it being fairly unsafe to do in the general case.
Comment #2 by bugzilla — 2007-11-27T13:21:13Z
I don't think there's any safe way of doing this. If this behavior is implemented, other things break. You can use the .ptr suffix to make it work.