SegFault trying to chunk lazily joined ranges

Steven Schveighoffer schveiguy at gmail.com
Thu Jul 1 20:21:39 UTC 2021


On 7/1/21 10:56 AM, Keivan Shah wrote:

> Using the handler I was able to get the stack trace and it seems that 
> the segFault is caused by `joiner` trying to call `.save` on a null 
> object leading to a `NullPointerError`. But I have not been able to 
> debug it further. Mostly it seems that there is something wrong with my 
> understanding of ranges or it could be a genuine bug in std.range.
> Can anyone help me debug this piece of code?

Thanks for the detailed instructions, very easy to reproduce.

However, chunkBy is a hot mess, which probably has a bug in it 
somewhere. I don't exactly know where this is, but know that using 
objects for forward ranges is bound to result in some failures, simply 
because they are one of the only forward ranges that *requires* calling 
`.save`, and much code exists that forgets to do that.

I spent about 20 minutes trying to find this and can't see how `chunkBy` 
actually works in this case. I don't have any more time to spend on it, 
sorry.

I narrowed your code down to the minimal case that I could find that 
segfaults:


```d
     ForwardRange!int[] listOfRanges = [iota(1).inputRangeObject];
     auto lazyFlattenedRange = joiner(listOfRanges);
     auto d2 = lazyFlattenedRange.chunkBy!(a => a);
     while(!d2.empty)
         d2.popFront;
```

I hope this helps someone narrow it down.

-Steve


More information about the Digitalmars-d-learn mailing list