remove internal usage of redundant uci_ptr.last
[project/uci.git] / util.c
diff --git a/util.c b/util.c
index 09f18171cede5399a020610050b9f202dd476ee7..61e42cd3b061c5e17f9950339e815ebbac96b68d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -36,10 +36,9 @@ __private void *uci_malloc(struct uci_context *ctx, size_t size)
 {
        void *ptr;
 
-       ptr = malloc(size);
+       ptr = calloc(1, size);
        if (!ptr)
                UCI_THROW(ctx, UCI_ERR_MEM);
-       memset(ptr, 0, size);
 
        return ptr;
 }
@@ -69,18 +68,22 @@ __private char *uci_strdup(struct uci_context *ctx, const char *str)
  * for names, only alphanum and _ is allowed (shell compatibility)
  * for types, we allow more characters
  */
-__private bool uci_validate_str(const char *str, bool name)
+__private bool uci_validate_str(const char *str, bool name, bool package)
 {
        if (!*str)
                return false;
 
-       while (*str) {
+       for (; *str; str++) {
                unsigned char c = *str;
-               if (!isalnum(c) && c != '_') {
-                       if (name || (c < 33) || (c > 126))
-                               return false;
-               }
-               str++;
+
+               if (isalnum(c) || c == '_')
+                       continue;
+
+               if (c == '-' && package)
+                       continue;
+
+               if (name || (c < 33) || (c > 126))
+                       return false;
        }
        return true;
 }
@@ -89,12 +92,10 @@ bool uci_validate_text(const char *str)
 {
        while (*str) {
                unsigned char c = *str;
-               if (((c < 32) &&
-                    (c != '\t') &&
-                    (c != '\n') &&
-                    (c != '\r'))) {
+
+               if (c < 32 && c != '\t' && c != '\n' && c != '\r')
                        return false;
-               }
+
                str++;
        }
        return true;
@@ -219,17 +220,21 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, c
 
        ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
        if ((ret < 0) && (errno != ENOSYS))
-               goto error;
+               goto error_close;
 
        ret = lseek(fd, 0, pos);
 
        if (ret < 0)
-               goto error;
+               goto error_unlock;
 
        file = fdopen(fd, (write ? "w+" : "r"));
        if (file)
                goto done;
 
+error_unlock:
+       flock(fd, LOCK_UN);
+error_close:
+       close(fd);
 error:
        UCI_THROW(ctx, UCI_ERR_IO);
 done: