From b0d497eee40ebb9b2c54d1c98dd23f6b13a50c00 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 12 Feb 2019 17:54:08 +0100 Subject: [PATCH] node: check for validity of node name --- include/villas/node.h | 2 ++ lib/node.c | 13 +++++++++++++ lib/super_node.cpp | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/include/villas/node.h b/include/villas/node.h index 38557ebb8..53d77c106 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -201,6 +201,8 @@ struct node_type * node_type(struct node *n); struct memory_type * node_memory_type(struct node *n, struct memory_type *parent); +int node_is_valid_name(const char *name); + #ifdef __cplusplus } #endif diff --git a/lib/node.c b/lib/node.c index a4753db79..d89671655 100644 --- a/lib/node.c +++ b/lib/node.c @@ -21,6 +21,7 @@ *********************************************************************************/ #include +#include #include #include @@ -766,3 +767,15 @@ invalid2: return 0; } + +int node_is_valid_name(const char *name) +{ + for (const char *p = name; *p; p++) { + if (isalnum(*p) || (*p == '_') || (*p == '-')) + continue; + + return -1; + } + + return 0; +} diff --git a/lib/super_node.cpp b/lib/super_node.cpp index 0993c5881..313d131d9 100644 --- a/lib/super_node.cpp +++ b/lib/super_node.cpp @@ -217,6 +217,10 @@ int SuperNode::parseJson(json_t *j) struct node_type *nt; const char *type; + ret = node_is_valid_name(name); + if (ret) + throw RuntimeError("Invalid name for node: {}", name); + ret = json_unpack_ex(json_node, &err, 0, "{ s: s }", "type", &type); if (ret) throw JsonError(err, "Failed to parse node");