www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23654] New: execv_: toAStringz: memory corruption

https://issues.dlang.org/show_bug.cgi?id=23654

          Issue ID: 23654
           Summary: execv_: toAStringz: memory corruption
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kdevel vogtner.de

//
// tassnd.d
// 2023-01-24 stvo
// function toAStringz taken from dmd2/src/phobos/std/process.d
// of DMD64 D Compiler v2.102.0-rc.1
// compile: dmd -checkaction=context -unittest -main -run tassnd.d
//
module tassnd;
import std.conv;
import std.stdio;
import core.stdc.stdlib;
import std.exception;
import core.exception : OutOfMemoryError;
private void toAStringz(in string[] a, const(char)**az)
{
    import std.string : toStringz;
    foreach (string s; a)
    {
        *az++ = toStringz(s);
    }
    *az = null; 
}

bool data_are_the_same (const string [] a, const char **b)
{
   foreach (i, s; a) {
      auto bs = b [i].to!string;
//      if (i < 3)
//         writefln!"%s %s %s" (i, s, bs);
      if (s != bs) {
         writefln!"i = <%d> a <%s> b <%s>" (i, s, bs);
         return false;
      }
   }
   return true;
}

unittest {
   immutable sizlist = [8, 16, 32, 64, 128];
   foreach (p; sizlist) {
      size_t s = 1024 * p;
      writeln (s);
      auto argv = new string [s];
      argv [0] = "ppp";
      argv [1] = s.to!string;
      argv [2 .. $] = "X";

//
// fragment taken from private int execv_ (...)
//
    auto argv_ = cast(const(char)**)core.stdc.stdlib.malloc((char*).sizeof * (1
+ argv.length));
    enforce!OutOfMemoryError(argv_ !is null, "Out of memory in std.process.");
    scope(exit) core.stdc.stdlib.free(argv_);
    toAStringz(argv, argv_); 
//
// end fragment
//
      assert (data_are_the_same (argv, argv_)); 
   }
}

$ dmd  -checkaction=context -unittest -main -run tassnd.d 
8192
16384
32768
i = <12156> a <X> b < >
tassnd.d(58): [unittest] false != true
1/1 modules FAILED unittests

--
Jan 24 2023