Comment #0 by su+dlangissues — 2022-06-20T03:54:22Z
std.stdio.File uses enforce incorrectly in two ways. first, on encountering OOM, it throws an exception rather than an error; and second, it fails to preallocate that error.
offending code here:
https://phobos.dpldocs.info/source/std.stdio.d.html#L507
package this(FILE* handle, string name, uint refs = 1, bool isPopened = false) @trusted
{
import core.stdc.stdlib : malloc;
import std.exception : enforce;
assert(!_p);
_p = cast(Impl*) enforce(malloc(Impl.sizeof), "Out of memory");
initImpl(handle, name, refs, isPopened);
}
as an aside, i believe fixing this may allow the function to be labelled with @nogc nothrow.
Comment #1 by dlang-bot — 2022-06-20T06:22:41Z
@Moth-Tolias created dlang/phobos pull request #8480 "preallocate oom error" mentioning this issue:
- preallocate oom error
fixes [issue 23196](https://issues.dlang.org/show_bug.cgi?id=23196).
unfortunately, while i believe it should be possible to make this function `@nogc nothrow` [since we are throwing an error, which cannot be recovered from], i wasn't able to figure out how to get enforce to realise it. advice would be greatly appreciated on this front.
https://github.com/dlang/phobos/pull/8480
Comment #2 by dlang-bot — 2022-06-22T05:33:18Z
dlang/phobos pull request #8480 "preallocate oom error" was merged into master:
- 37e22eef1cd1e409aee2e2bcc99f63e7e0ce68db by Su:
preallocate oom error
fixes issue 23196.
unfortunately, while i believe it should be possible to make this function `@nogc nothrow` [since we are throwing an error, which cannot be recovered from], i wasn't able to figure out how to get enforce to realise it. advice would be greatly appreciated on this front.
https://github.com/dlang/phobos/pull/8480