scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit b2d5d321150534a6be5ee576a5bd9957ffe9e915
parent fc7ec1b82a89eb33fa5b11077c6491fa22876dd0
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 12 Dec 2017 12:32:53 +0000

[as] Add validator of labels

Field spliting is done using file separators characters, and it means
that we can include in the label everything until the file separator,
including spaces and undesired characters.

Diffstat:
Mas/expr.c | 16++++++++++++++--
Mas/parser.c | 23+++++++++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/as/expr.c b/as/expr.c @@ -197,8 +197,20 @@ iden(void) int c; char *p; - while (isalnum(c = *endp) || c == '_' || c == '.') - ++endp; + for ( ; c = *endp; ++endp) { + if (isalnum(c)) + continue; + switch (c) { + case '_': + case '-': + case '.': + case '$': + continue; + default: + break; + } + } + tok2str(); yylval.sym = lookup(yytext, FUNDEF); diff --git a/as/parser.c b/as/parser.c @@ -88,6 +88,27 @@ out_loop: } static int +validlabel(char *name) +{ + int c; + + while ((c = *name++) != '\0') { + if (isalnum(c)) + continue; + switch (c) { + case '_': + case '-': + case '.': + case '$': + continue; + default: + return 0; + } + } + return 1; +} + +static int extract(char *s, struct line *lp) { int r = 0; @@ -97,6 +118,8 @@ extract(char *s, struct line *lp) len = strlen(lp->label); if (lp->label[len-1] == ':') lp->label[len-1] = '\0'; + if (!validlabel(lp->label)) + error("incorrect label name '%s'", lp->label); r++; } if (lp->op = field(&s))