improve validation, accept more characters in the section type
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index bcf65aa..78b22cf 100644 (file)
--- a/util.c
+++ b/util.c
@@ -59,19 +59,32 @@ static char *uci_strdup(struct uci_context *ctx, const char *str)
        return ptr;
 }
 
-static bool uci_validate_name(const char *str)
+/*
+ * validate strings for names and types, reject special characters
+ * for names, only alphanum and _ is allowed (shell compatibility)
+ * for types, we allow more characters
+ */
+static bool uci_validate_str(const char *str, bool name)
 {
        if (!*str)
                return false;
 
        while (*str) {
-               if (!isalnum(*str) && (*str != '_'))
-                       return false;
+               char c = *str;
+               if (!isalnum(c) && c != '_') {
+                       if (name || (c < 33) || (c > 126))
+                               return false;
+               }
                str++;
        }
        return true;
 }
 
+static inline bool uci_validate_name(const char *str)
+{
+       return uci_validate_str(str, true);
+}
+
 static void uci_alloc_parse_context(struct uci_context *ctx)
 {
        ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));