Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1482 from yebblies/issue9195

Issue 9195 - Should not be able to index a pointer in safed
  • Loading branch information...
commit e97e886c7a092a279bf72b1ad5e6fb63dc81b82e 2 parents e540881 + 580eb16
@WalterBright WalterBright authored
Showing with 21 additions and 1 deletion.
  1. +13 −0 src/expression.c
  2. +8 −1 test/runnable/testsafe.d
View
13 src/expression.c
@@ -10070,6 +10070,19 @@ Expression *IndexExp::semantic(Scope *sc)
switch (t1->ty)
{
case Tpointer:
+ e2 = e2->implicitCastTo(sc, Type::tsize_t);
+ e2 = e2->optimize(WANTvalue);
+ if (e2->op == TOKint64 && e2->toInteger() == 0)
+ ;
+ else if (sc->func->setUnsafe())
+ {
+ error("safe function '%s' cannot index pointer '%s'",
+ sc->func->toPrettyChars(), e1->toChars());
+ return new ErrorExp();
+ }
+ e->type = ((TypeNext *)t1)->next;
+ break;
+
case Tarray:
e2 = e2->implicitCastTo(sc, Type::tsize_t);
e->type = ((TypeNext *)t1)->next;
View
9 test/runnable/testsafe.d
@@ -279,7 +279,14 @@ void voidinitializers()
static assert(!__traits(compiles, { int** a = void; } ));
static assert(!__traits(compiles, { int[int] a = void; } ));
}
-
+
+@safe
+void pointerindex()
+{//http://d.puremagic.com/issues/show_bug.cgi?id=9195
+ static assert(!__traits(compiles, { int* p; auto a = p[30]; }));
+ static assert( __traits(compiles, { int* p; auto a = p[0]; }));
+}
+
@safe
void basiccast()
{//http://d.puremagic.com/issues/show_bug.cgi?id=5088
Please sign in to comment.
Something went wrong with that request. Please try again.