www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1679] New: D should cast char[] to char* when used in a variadic argument in extern(C) context

reply d-bugmail puremagic.com writes:
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 digitalmars.com
        ReportedBy: arthur.loiret 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.


-- 
Nov 19 2007
next sibling parent Brad Roberts <braddr puremagic.com> writes:
On Tue, 20 Nov 2007, d-bugmail puremagic.com 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.
Nov 19 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1679


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX





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.


-- 
Nov 27 2007