tests: blobmsg/json: add more test cases
[project/libubox.git] / blobmsg.c
index fbc6d2de9135509a955fd55d43a2e0b6be01fe4a..1dd57e156122ec06d1797f9a17fb10938c2fc356 100644 (file)
--- a/blobmsg.c
+++ b/blobmsg.c
@@ -25,12 +25,6 @@ static const int blob_type[__BLOBMSG_TYPE_LAST] = {
        [BLOBMSG_TYPE_UNSPEC] = BLOB_ATTR_BINARY,
 };
 
-static uint16_t
-blobmsg_namelen(const struct blobmsg_hdr *hdr)
-{
-       return be16_to_cpu(hdr->namelen);
-}
-
 bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
 {
        return blobmsg_check_attr_len(attr, name, blob_raw_len(attr));
@@ -100,12 +94,22 @@ bool blobmsg_check_attr_len(const struct blob_attr *attr, bool name, size_t len)
 }
 
 int blobmsg_check_array(const struct blob_attr *attr, int type)
+{
+       return blobmsg_check_array_len(attr, type, blobmsg_len(attr));
+}
+
+int blobmsg_check_array_len(const struct blob_attr *attr, int type, size_t len)
 {
        struct blob_attr *cur;
        bool name;
-       size_t rem;
        int size = 0;
 
+       if (type > BLOBMSG_TYPE_LAST)
+               return -1;
+
+       if (!blobmsg_check_attr_len(attr, false, len))
+               return -1;
+
        switch (blobmsg_type(attr)) {
        case BLOBMSG_TYPE_TABLE:
                name = true;
@@ -117,11 +121,11 @@ int blobmsg_check_array(const struct blob_attr *attr, int type)
                return -1;
        }
 
-       blobmsg_for_each_attr(cur, attr, rem) {
+       __blobmsg_for_each_attr(cur, attr, len) {
                if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type)
                        return -1;
 
-               if (!blobmsg_check_attr(cur, name))
+               if (!blobmsg_check_attr_len(cur, name, len))
                        return -1;
 
                size++;
@@ -135,6 +139,11 @@ bool blobmsg_check_attr_list(const struct blob_attr *attr, int type)
        return blobmsg_check_array(attr, type) >= 0;
 }
 
+bool blobmsg_check_attr_list_len(const struct blob_attr *attr, int type, size_t len)
+{
+       return blobmsg_check_array_len(attr, type, len) >= 0;
+}
+
 int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len,
                        struct blob_attr **tb, void *data, unsigned int len)
 {
@@ -231,7 +240,10 @@ blobmsg_new(struct blob_buf *buf, int type, const char *name, int payload_len, v
        attr->id_len |= be32_to_cpu(BLOB_ATTR_EXTENDED);
        hdr = blob_data(attr);
        hdr->namelen = cpu_to_be16(namelen);
-       strcpy((char *) hdr->name, (const char *)name);
+
+       memcpy(hdr->name, name, namelen);
+       hdr->name[namelen] = '\0';
+
        pad_end = *data = blobmsg_data(attr);
        pad_start = (char *) &hdr->name[namelen];
        if (pad_start < pad_end)