summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Elder <elder@linaro.org>2021-10-01 18:23:32 -0500
committerBjorn Andersson <bjorn.andersson@linaro.org>2021-10-04 14:15:18 -0500
commitb6b7384256530f8d393383c41b144687baed2875 (patch)
tree6fb653335e0ce513156a251ec7f4d690ad8019dd
parent789c4e9d23e3a8e9f30573a12b2e38a9484ecf77 (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.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/parser.c b/parser.c
index 2498b79..a684a29 100644
--- a/parser.c
+++ b/parser.c
@@ -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));