-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue 14137: std.socket.getAddressInfo breaks @safe #4009
Conversation
Remove abuse of @trusted in template function getAddressInfo that cannot guarantee that the incoming type argument is @safe. Localize @trusted block of the function to the single call to getAddressInfoImpl(), so that any @System code in T will be caught by the type system. Add unittest to ensure such examples of T will be rejected at compile-time. Mark normal unittest for getAddressInfo as @safe to ensure that the function body itself does not introduce any non-@safe code.
|
An alternative would be to check I've been thinking about what to do with Socket.receive: auto buffer = new Object[](1);
socket.receive(buffer);It clearly doesn't have a memory safe interface. We could of course start thinking about type-aware wrappers, but considering the ubiquity of Anyway, that's for another time. LGTM. |
|
Well, technically the problem isn't with |
|
Raised a new issue here: https://issues.dlang.org/show_bug.cgi?id=15702 |
|
My point is that if conversion from Note that void foo(Object o) @safe {}
void main() @system {
size_t i = void;
foo(cast(Object)cast(void*)i);
} |
|
Conversion of int*[] x = [new int(0), new int(1), new int(2)];
size_t[] y = [0x12345678, 0x12345678, 0x12345678];
void[] v = x[];
v[] = y[];And the same thing happens if you replace So it looks like we need to make |
|
What about copying Having said that, though, if |
|
It's true, we could and maybe should tighten up |
But the problem there is that you're mutating int*[] x = [new int(0), new int(1), new int(2)];
void[] v = x[];be perfectly |
|
Yeah, writing to |
|
Ah. I responded too quickly and missed this comment:
Yes. Conversion to |
The question is whether it's |
|
I'll say it a third time. Receiving data on a socket and writing it to As a completely separate issue, |
Well, since the conversion to But the simple fact that |
It's always safe to mutate with the amended rule.
I suggest we do both, although with the latter instated, the former becomes just a safeguard (casting before mutating is more visible than no casting required), not a mechanic of supporting
Whether a function is a C function or D function is entirely orthogonal to |
Well, in theory, but to mark a C function with |
| @@ -971,7 +971,23 @@ AddressInfo[] getAddressInfo(T...)(in char[] node, T options) @trusted | |||
| static assert(0, "Unknown getAddressInfo option type: " ~ typeof(option).stringof); | |||
| } | |||
|
|
|||
| return getAddressInfoImpl(node, service, &hints); | |||
| return () @trusted { return getAddressInfoImpl(node, service, &hints); }(); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAddressInfoImpl looks like it has a memory safe interface. Memory safety attributes should be handled there instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So you're saying it should be getAddressInfoImpl that gets marked @trusted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, the hints parameter has possible unsafe configurations, such as invalid ai_addrlen.
C functions can have available body when implemented in D, and D functions can also have unavailable body. Whether a function is a C function or not is a red herring.
D functions are also
The slice The reason that |
|
Hmmm. I thought that |
Are you sure about that? int main() @safe {
auto a = [0, 1, 2];
auto b = a[$..$];
return *b.ptr;
} |
|
@klickverbot, no, I'm not. I guess your example would deref |
|
@JakobOvrum: Yep, it accesses the beyond-the-last pointer. I knew reported this somewhere already: https://issues.dlang.org/show_bug.cgi?id=11176 |
|
@klickverbot, definitely something we need to fix. It doesn't have any bearing on the use of |
|
@JakobOvrum: Agreed, it is unrelated to |
|
Tons of unrelated discussion. The pull looks good to me |
|
Auto-merge toggled on |
Remove abuse of
@trustedin template functiongetAddressInfothat cannot guarantee that the incoming type argumentTis actually@safe. Localize@trustedto the single call togetAddressInfoImpl, so that any@systemcode inTwill be caught by the type system.Add unittest to ensure such examples of
Twill be rejected at compile-time.Mark normal unittest for
getAddressInfoas@safeto ensure that the function body itself does not introduce any non-@safecode.The rest of this module has frightening amounts of
@trustedfunctions, which need to be properly vetted (and probably the scope of@trustedreduced), but let's leave that for another battle.