iteration over directories is unsafe

bauss jj_1337 at live.dk
Mon Feb 7 09:50:19 UTC 2022


On Saturday, 5 February 2022 at 23:26:21 UTC, forkit wrote:
> It is not possible to do a simple iteration over directories in 
> @safe mode.
>
> Really? I have to resort to unsafe??
>
>
> //----
>
> module test;
> @safe: // nope. no can do.
>
> import std;
>
> void main()
> {
>     auto dFiles = dirEntries("", "*.{d,di}", SpanMode.depth);
>     foreach(d; dFiles)
>         writeln(d.name);
> }
>
> //----

Well it can't be "truly" safe since it requires a call to the 
system to retrieve the directory entries.

While yeah, from common sense it's most likely safe from the 
perspective of a user, then it's not true safety as there are no 
safety guarantees as it's an external call.

I would argue that D should move towards marking certain system 
functions as trusted.


More information about the Digitalmars-d-learn mailing list