rawRead from Pipe segfaults

kdevel kdevel at vogtner.de
Wed Mar 17 23:08:07 UTC 2021


In order to watch out for lost bytes in a pipe I encountered this 
segfault.
It seems that the readEnd is already closed when rawRead = fread 
is
called (uncomment the eof line).

How do I keep the pipe open?

```piperawreadsegfault.d (linux)
import std.stdio;
import std.process;

void main ()
{
    auto dev_zero = File ("/dev/zero", "r");
    auto dev_null = File ("/dev/null", "w");
    auto p = pipe ();
    auto proc1 = spawnProcess (["dd", "bs=4096", "count=2"],
       dev_zero, p.writeEnd);
    auto proc2 = spawnProcess (["dd", "bs=4096", "count=1"],
       p.readEnd, dev_null);
    auto res2 = wait (proc2);
    auto res1 = wait (proc1);
    stderr.writeln ("res1 = ", res1, ", res2 = ", res2);
//  stderr.writeln (p.readEnd.eof);
    ubyte [1] u;
    auto n = p.readEnd.rawRead (u);
}
```

$ dmd -g piperawreadsegfault
$ gdb ./piperawreadsegfault
(gdb) r
Starting program: /tmp/k/piperawreadsegfault
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
2+0 records in
2+0 records out
8192 bytes (8.2 kB) copied, 2.487e-05 s, 329 MB/s
1+0 records in
1+0 records out
4096 bytes (4.1 kB) copied, 2.0234e-05 s, 202 MB/s
res1 = 0, res2 = 0

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7054f99 in fread () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff7054f99 in fread () from /lib64/libc.so.6
#1  0x000000000048fd96 in std.stdio() (obj=..., f=0x0) at 
[...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d:4383
#2  0x000000000048fcb7 in std.stdio.File() (this=..., buffer=...)
     at [...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d:1036
#3  0x000000000048f10a in D main () at piperawreadsegfault.d:18
[...]


More information about the Digitalmars-d-learn mailing list