Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Conversation

alexrp
Copy link
Contributor

@alexrp alexrp commented Jul 8, 2012

Minor change to core.thread since some internal functions were made private.

This marks almost everything in core.stdc as @trusted. I also added some nothrow annotations while I was there.

@alexrp
Copy link
Contributor Author

alexrp commented Jul 10, 2012

Now green everywhere (previous failures were unrelated).

@MartinNowak
Copy link
Member

Nice.

You made a lot of decision why certain functions are not marked @trusted.
Could you please add some short comments (especially for stdio) so this won't get lost.

@alexrp
Copy link
Contributor Author

alexrp commented Jul 10, 2012

OK, I'll go over and add more comments about safety decisions to the code later today.

@alexrp
Copy link
Contributor Author

alexrp commented Jul 15, 2012

I've rebased with some comments on the annotations. Generally, I consider anything that mutates processor state or operates on unsafe stuff like C strings @system.

Minor change to core.thread since some internal functions were made private.
int fclose(FILE* stream);
int fflush(FILE* stream);
// No unsafe pointer manipulation.
@trusted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonono, fclose isn't trusted. It invalidates memory pointed to by stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Will mark it @system.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// We don't mark these @trusted. Given that they return a void*, one has
// to do a pointer cast to do anything sensible with the result. Thus,
// functions using these already have to be @trusted, allowing them to
// call @system stuff anyway.
void* malloc(size_t size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can provide a simple @trusted interface for malloc and calloc that work with a category of types (e.g. numeric types) and return a T[]. Of course this is not the place...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured something like that would probably fit better in the allocators design.

@andralex
Copy link
Member

Looks ready except as noted.

andralex added a commit that referenced this pull request Jul 16, 2012
Safety annotations in core.stdc.
@andralex andralex merged commit b8ad17f into dlang:master Jul 16, 2012
@d-random-contributor
Copy link

d-random-contributor commented Apr 4, 2017

FILE's fields are available for modification, so functions with FILE* arguments can't be trusted. This is the reason MS provides FILE as opaque struct: it happened in the wild.

@andralex
Copy link
Member

andralex commented Apr 5, 2017

@d-random-contributor thanks for the note. Here "trusted" has a restricted meaning - it's only about memory safety, not general security.

@d-random-contributor
Copy link

Fields of FILE struct can be assigned with values that are not what clib expects there, breaking its invariant, e.g. a file buffer should be of a certain size - clib will blindly use it for reading and writing, if the buffer is of the wrong size, it will write past the end of the buffer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants