From 62a02a1a8a8ebe8d73d86e11064b01745cc38133 Mon Sep 17 00:00:00 2001 From: Daniel Murphy Date: Tue, 3 Dec 2013 19:23:04 +1100 Subject: [PATCH] Fix cases where goto skips variable initialization found by fixing issue 602 --- src/expression.c | 45 ++++++++++++++++++++++----------------------- src/inifile.c | 17 ++++++----------- src/interpret.c | 4 ++-- src/mtype.c | 10 ++++------ src/optimize.c | 9 ++++----- src/template.c | 12 +++++------- 6 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/expression.c b/src/expression.c index b3ff026a6072..7e8b6c67548e 100644 --- a/src/expression.c +++ b/src/expression.c @@ -1420,16 +1420,18 @@ Type *functionParameters(Loc loc, Scope *sc, TypeFunction *tf, if (tf->varargs == 2 && i + 1 == nparams) { //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); - MATCH m; - if ((m = arg->implicitConvTo(p->type)) != MATCHnomatch) { - if (p->type->nextOf() && arg->implicitConvTo(p->type->nextOf()) >= m) - goto L2; - else if (nargs != nparams) - { error(loc, "expected %llu function arguments, not %llu", (ulonglong)nparams, (ulonglong)nargs); - return Type::terror; + MATCH m; + if ((m = arg->implicitConvTo(p->type)) != MATCHnomatch) + { + if (p->type->nextOf() && arg->implicitConvTo(p->type->nextOf()) >= m) + goto L2; + else if (nargs != nparams) + { error(loc, "expected %llu function arguments, not %llu", (ulonglong)nparams, (ulonglong)nargs); + return Type::terror; + } + goto L1; } - goto L1; } L2: Type *tb = p->type->toBasetype(); @@ -10235,6 +10237,7 @@ Expression *SliceExp::semantic(Scope *sc) e = this; Type *t = e1->type->toBasetype(); + AggregateDeclaration *ad = isAggregate(t); if (t->ty == Tpointer) { if (!lwr || !upr) @@ -10250,7 +10253,7 @@ Expression *SliceExp::semantic(Scope *sc) else if (t->ty == Tsarray) { } - else if (AggregateDeclaration *ad = isAggregate(t)) + else if (ad) { if (search_function(ad, Id::slice)) { @@ -10290,7 +10293,16 @@ Expression *SliceExp::semantic(Scope *sc) else if (t == Type::terror) goto Lerr; else - goto Lerror; + { + Lerror: + if (e1->op == TOKerror) + return e1; + error("%s cannot be sliced with []", + t->ty == Tvoid ? e1->toChars() : t->toChars()); + Lerr: + e = new ErrorExp(); + return e; + } { Scope *sc2 = sc; @@ -10387,19 +10399,6 @@ Expression *SliceExp::semantic(Scope *sc) type = e1->type; return e; - -Lerror: - if (e1->op == TOKerror) - return e1; - char *s; - if (t->ty == Tvoid) - s = e1->toChars(); - else - s = t->toChars(); - error("%s cannot be sliced with []", s); -Lerr: - e = new ErrorExp(); - return e; } void SliceExp::checkEscape() diff --git a/src/inifile.c b/src/inifile.c index 806d3d6ae039..c5eeef7a608e 100644 --- a/src/inifile.c +++ b/src/inifile.c @@ -184,24 +184,18 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec break; } - // The line is file.buffer[linestart..i] - char *line; - size_t len; - char *pn; - - line = (char *)&file.buffer[linestart]; - len = i - linestart; - buf.reset(); // First, expand the macros. // Macros are bracketed by % characters. - for (size_t k = 0; k < len; k++) + for (size_t k = 0; k < i - linestart; k++) { + // The line is file.buffer[linestart..i] + char *line = (char *)&file.buffer[linestart]; if (line[k] == '%') { - for (size_t j = k + 1; j < len; j++) + for (size_t j = k + 1; j < i - linestart; j++) { if (line[j] == '%') { @@ -264,6 +258,7 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec case '[': // look for [Environment] p = skipspace(p + 1); + char *pn; for (pn = p; isalnum((utf8_t)*pn); pn++) ; if (pn - p == envsectionnamelen && @@ -278,7 +273,7 @@ const char *inifile(const char *argv0x, const char *inifilex, const char *envsec default: if (envsection) { - pn = p; + char *pn = p; // Convert name to upper case; // remove spaces bracketing = diff --git a/src/interpret.c b/src/interpret.c index 823fdad0ea12..314ac46f89d9 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -1796,11 +1796,11 @@ Expression *TryCatchStatement::interpret(InterState *istate) InterState istatex = *istate; istatex.start = istate->gotoTarget; // set starting statement istatex.gotoTarget = NULL; - Expression *ex = ca->handler->interpret(&istatex); + Expression *eh = ca->handler->interpret(&istatex); if (!istatex.start) { istate->gotoTarget = NULL; - e = ex; + e = eh; } } } diff --git a/src/mtype.c b/src/mtype.c index 6d70a49346a5..6eef7ddc6a57 100644 --- a/src/mtype.c +++ b/src/mtype.c @@ -7186,7 +7186,10 @@ void TypeTypeof::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol { inuse = 2; error(loc, "circular typeof definition"); - goto Lerr; + Lerr: + *pt = Type::terror; + inuse--; + return; } inuse++; @@ -7270,11 +7273,6 @@ void TypeTypeof::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol (*pt) = (*pt)->addMod(mod); inuse--; return; - -Lerr: - *pt = Type::terror; - inuse--; - return; } Type *TypeTypeof::semantic(Loc loc, Scope *sc) diff --git a/src/optimize.c b/src/optimize.c index ecc73b4e3c04..f88ac2f5a223 100644 --- a/src/optimize.c +++ b/src/optimize.c @@ -569,7 +569,10 @@ Expression *CastExp::optimize(int result, bool keepLvalue) e1->type->implicitConvTo(type) >= MATCHconst) { if (X) printf(" returning2 %s\n", e1->toChars()); - goto L1; + L1: // Returning e1 with changing its type + Expression *e = (e1old == e1 ? e1->copy() : e1); + e->type = type; + return e; } /* The first test here is to prevent infinite loops @@ -628,10 +631,6 @@ Expression *CastExp::optimize(int result, bool keepLvalue) e = this; if (X) printf(" returning6 %s\n", e->toChars()); return e; -L1: // Returning e1 with changing its type - e = (e1old == e1 ? e1->copy() : e1); - e->type = type; - return e; #undef X } diff --git a/src/template.c b/src/template.c index cb6cf1d772f9..0cf7f7000ef4 100644 --- a/src/template.c +++ b/src/template.c @@ -2349,7 +2349,11 @@ void functionResolve(Match *m, Dsymbol *dstart, Loc loc, Scope *sc, if (td->semanticRun == PASSinit) { ::error(loc, "forward reference to template %s", td->toChars()); - goto Lerror; + Lerror: + m->lastf = NULL; + m->count = 0; + m->last = MATCHnomatch; + return 1; } FuncDeclaration *f; f = td->onemember ? td->onemember/*->toAlias()*/->isFuncDeclaration() : NULL; @@ -2515,12 +2519,6 @@ void functionResolve(Match *m, Dsymbol *dstart, Loc loc, Scope *sc, continue; } return 0; - - Lerror: - m->lastf = NULL; - m->count = 0; - m->last = MATCHnomatch; - return 1; } }; ParamDeduce p;