Thread overview
[Issue 22673] .array of a range with length preallocates without checking if the length was lying or not.
Jan 13, 2022
Adam D. Ruppe
Jan 14, 2022
mhh
Jan 14, 2022
Adam D. Ruppe
Jan 14, 2022
mhh
Jan 14, 2022
Dlang Bot
Jan 26, 2022
Dlang Bot
January 13, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

Adam D. Ruppe <destructionator@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator@gmail.com

--- Comment #1 from Adam D. Ruppe <destructionator@gmail.com> ---
The others will be in the .init state, which should mean the destructor is a no-op on them.

--
January 14, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

--- Comment #2 from mhh <maxhaton@gmail.com> ---
The array allocated is uninitialized.
```
auto result = (() @trusted => uninitializedArray!(Unqual!E[])(length))();
```

--
January 14, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

--- Comment #3 from Adam D. Ruppe <destructionator@gmail.com> ---
ah i see yeah that's good good.

the fix would prolly be to just bit some init over and slice it off then at the end

--
January 14, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

--- Comment #4 from mhh <maxhaton@gmail.com> ---
(In reply to Adam D. Ruppe from comment #3)
> ah i see yeah that's good good.
> 
> the fix would prolly be to just bit some init over and slice it off then at the end

I kind of think this should actually error or throw, because I think it's a bit iffy letting these kinds of things fester. There's a wider problem though, if something throws when filling the array up then it will be finalized with (what must be assumed to be, even if it is zero) junk. For that the fix I have is to just write T.init into the memory before passing the exception (this might be a case to catch Throwable) up.

Slightly painful to test, I can't quite get the GC to collect before I try and check all the destructions were valid. I might have to put an assert in the destructor (bitch to debug if it ever breaks)

--
January 14, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@maxhaton created dlang/phobos pull request #8359 "Fixes issues 22185, 22673" fixing this issue:

- Fixes issues 22185, 22673

https://github.com/dlang/phobos/pull/8359

--
January 26, 2022
https://issues.dlang.org/show_bug.cgi?id=22673

Dlang Bot <dlang-bot@dlang.rocks> changed:

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

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #8359 "Fixes issues 22185, 22673" was merged into master:

- 4eff7e2a312f70cd5a576bcfdd63d8b0022c68b4 by Max Haughton:
  Fixes issues 22185, 22673

https://github.com/dlang/phobos/pull/8359

--