diff options
author | Alex Elder <elder@linaro.org> | 2021-10-01 18:23:32 -0500 |
---|---|---|
committer | Bjorn Andersson <bjorn.andersson@linaro.org> | 2021-10-04 14:15:18 -0500 |
commit | b6b7384256530f8d393383c41b144687baed2875 (patch) | |
tree | 6fb653335e0ce513156a251ec7f4d690ad8019dd | |
parent | 789c4e9d23e3a8e9f30573a12b2e38a9484ecf77 (diff) |
parser: assert symbol names are valid
Introduce symbol_valid(), which returns true only if a given symbol
name is valid. A valid symbol begins with an alphabetic character
and consists otherwise of alphanumerics or '_'. It also must be
representable in the token buffer.
Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-29-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
-rw-r--r-- | parser.c | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -119,11 +119,40 @@ struct symbol { static struct list_head symbols = LIST_INIT(symbols); +static bool symbol_valid(const char *name) +{ + const char *p = name; + struct symbol *sym; + char ch; + + /* Symbol name must start with an alphabetic character */ + if (!p || !isalpha(*p++)) + return false; + + /* Remainder of the name is alphanumeric or underscore */ + while ((ch = *p++)) + if (!(isalnum(ch) || ch == '_')) + return false; + + /* Symbol name must fit in the token buffer */ + if (p - name > TOKEN_BUF_SIZE) + return 0; + + /* Finally, symbol names must be unique */ + list_for_each_entry(sym, &symbols, node) + if (!strcmp(name, sym->name)) + return false; + + return true; +} + static void symbol_add(const char *name, enum token_id token_id, ...) { struct symbol *sym; va_list ap; + assert(symbol_valid(name)); + va_start(ap, token_id); sym = memalloc(sizeof(struct symbol)); |