June 20, 2022
https://issues.dlang.org/show_bug.cgi?id=23196

          Issue ID: 23196
           Summary: File constructor fails to preallocate oom error, uses
                    exception instead
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody@puremagic.com
          Reporter: su+dlangissues@angel-island.zone

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.

--