Discussion Thread: DIP 1035-- at system Variables--Final Review
Paul Backus
snarwin at gmail.com
Tue Feb 22 13:13:43 UTC 2022
On Tuesday, 22 February 2022 at 08:47:55 UTC, Stanislav Blinov
wrote:
> A more pertinent example around file descriptors and memory
> safety is void-initialization:
>
> ```d
> struct File
> {
> void write(const(void)[] data) @safe;
> // ...
> private int fd;
> }
>
> void main() @safe
> {
> File f = void; // this compiles in current language,
> because `File` doesn't have pointers
> f.write("hello"); // may corrupt memory if
> (implementation-defined) value of `f.fd` happens to correspond
> to an existing mapping
> }
> ```
If you attempt to fill in the missing part of your example, I
think you will find that you cannot actually demonstrate memory
corruption resulting from `void`-initialization of a file
descriptor without the use of `@trusted` code (e.g., to cast the
`void*` returned from `mmap` to some other type of pointer whose
target type has unsafe values).
More information about the Digitalmars-d
mailing list