Skip to content
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

Issue 9195 - Should not be able to index a pointer in safed #1482

Merged
merged 1 commit into from
Jan 14, 2013

Conversation

yebblies
Copy link
Member

This prevents indexing a pointer in @safe code unless the index is known at compile time to be zero.

http://d.puremagic.com/issues/show_bug.cgi?id=9195

This prevents indexing a pointer in @safe code unless the index is known at compile time to be zero.
@andralex
Copy link
Member

nice!

WalterBright added a commit that referenced this pull request Jan 14, 2013
Issue 9195 - Should not be able to index a pointer in safed
@WalterBright WalterBright merged commit e97e886 into dlang:master Jan 14, 2013
@yebblies
Copy link
Member Author

Should we also allow ptr+0 in @safe code?

@braddr
Copy link
Member

braddr commented Jan 16, 2013

Ideally any expression involving ptr that after const folding is equal to ptr still. But I don't expect there's a lot of those other than ptr[0]. ptr+0 might be next most likely and ptr+var where var is known to be 0 a distant third. and ptr+expr where expr ctfe'ly evaluates to 0 and even more distant forth?

@yebblies
Copy link
Member Author

I don't think I've every seen *(ptr+0) in real code...

and ptr+expr where expr ctfe'ly evaluates to 0 and even more distant forth?

We can't evaluate expr using ctfe in most contexts.

We could allow *([1,2,3].ptr + 2) if we really wanted to, or even

uint x;
auto b = *((cast(ubyte*)&x)+3);

This could be done by calculating a 'safe offset' for each pointer expression and checking the range.

@andralex
Copy link
Member

Is this done after inlining? At that point there could be quite a few instances of p[0].

@yebblies
Copy link
Member Author

No, before inlining. If you do it after then some code will only compile with -inline.

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

Successfully merging this pull request may close these issues.

4 participants