fix signed vs unsigned char validation bug
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index 7f37e88..cb88968 100644 (file)
--- a/util.c
+++ b/util.c
@@ -86,7 +86,7 @@ __plugin bool uci_validate_str(const char *str, bool name)
                return false;
 
        while (*str) {
-               char c = *str;
+               unsigned char c = *str;
                if (!isalnum(c) && c != '_') {
                        if (name || (c < 33) || (c > 126))
                                return false;
@@ -96,6 +96,16 @@ __plugin bool uci_validate_str(const char *str, bool name)
        return true;
 }
 
+static inline bool uci_validate_package(const char *str)
+{
+       return uci_validate_str(str, false);
+}
+
+static inline bool uci_validate_type(const char *str)
+{
+       return uci_validate_str(str, false);
+}
+
 static inline bool uci_validate_name(const char *str)
 {
        return uci_validate_str(str, true);
@@ -104,8 +114,9 @@ static inline bool uci_validate_name(const char *str)
 bool uci_validate_text(const char *str)
 {
        while (*str) {
-               if ((*str == '\r') || (*str == '\n') ||
-                       ((*str < 32) && (*str != '\t')))
+               unsigned char c = *str;
+               if ((c == '\r') || (c == '\n') ||
+                       ((c < 32) && (c != '\t')))
                        return false;
                str++;
        }
@@ -159,7 +170,7 @@ int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
                goto error;
 
 lastval:
-       if (ptr->package && !uci_validate_str(ptr->package, false))
+       if (ptr->package && !uci_validate_package(ptr->package))
                goto error;
        if (ptr->section && !uci_validate_name(ptr->section))
                ptr->flags |= UCI_LOOKUP_EXTENDED;
@@ -398,14 +409,14 @@ int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char *
        UCI_ASSERT(ctx, str != NULL);
        UCI_ASSERT(ctx, result != NULL);
 
-       if (ctx->pctx) {
-               if (ctx->pctx->file != stream) {
-                       uci_cleanup(ctx);
-               }
-       } else {
+       if (ctx->pctx && (ctx->pctx->file != stream))
+               uci_cleanup(ctx);
+
+       if (!ctx->pctx)
                uci_alloc_parse_context(ctx);
-               ctx->pctx->file = stream;
-       }
+
+       ctx->pctx->file = stream;
+
        if (!*str) {
                uci_getln(ctx, 0);
                *str = ctx->pctx->buf;