projects
/
project
/
libubox.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
cad5bea
)
blobmsg: constify and add more validation
author
Felix Fietkau
<nbd@openwrt.org>
Mon, 31 Jan 2011 02:51:06 +0000
(
03:51
+0100)
committer
Felix Fietkau
<nbd@openwrt.org>
Mon, 31 Jan 2011 02:51:06 +0000
(
03:51
+0100)
blobmsg.c
patch
|
blob
|
history
blobmsg.h
patch
|
blob
|
history
diff --git
a/blobmsg.c
b/blobmsg.c
index
742a93b
..
3e9c774
100644
(file)
--- a/
blobmsg.c
+++ b/
blobmsg.c
@@
-21,7
+21,7
@@
struct strbuf {
char *buf;
};
char *buf;
};
-static bool blobmsg_puts(struct strbuf *s, char *c, int len)
+static bool blobmsg_puts(struct strbuf *s, c
onst c
har *c, int len)
{
if (len <= 0)
return true;
{
if (len <= 0)
return true;
@@
-37,9
+37,9
@@
static bool blobmsg_puts(struct strbuf *s, char *c, int len)
return true;
}
return true;
}
-static void blobmsg_format_string(struct strbuf *s, char *str)
+static void blobmsg_format_string(struct strbuf *s, c
onst c
har *str)
{
{
- char *p, *last = str, *end = str + strlen(str);
+ c
onst c
har *p, *last = str, *end = str + strlen(str);
char buf[8] = "\\u00";
blobmsg_puts(s, "\"", 1);
char buf[8] = "\\u00";
blobmsg_puts(s, "\"", 1);
@@
-175,9
+175,19
@@
char *blobmsg_format_json(struct blob_attr *attr, bool list)
return s.buf;
}
return s.buf;
}
+static const int blob_type[__BLOBMSG_TYPE_LAST] = {
+ [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
+ [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
+ [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
+ [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
+ [BLOBMSG_TYPE_STRING] = BLOB_ATTR_STRING,
+};
+
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
{
const struct blobmsg_hdr *hdr;
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
{
const struct blobmsg_hdr *hdr;
+ const char *data;
+ int id, len;
if (blob_len(attr) < sizeof(struct blobmsg_hdr))
return false;
if (blob_len(attr) < sizeof(struct blobmsg_hdr))
return false;
@@
-192,7
+202,17
@@
bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
if (hdr->name[hdr->namelen] != 0)
return false;
if (hdr->name[hdr->namelen] != 0)
return false;
- return true;
+ id = blob_id(attr);
+ len = blobmsg_data_len(attr);
+ data = blobmsg_data(attr);
+
+ if (!id || id > BLOBMSG_TYPE_LAST)
+ return false;
+
+ if (!blob_type[id])
+ return true;
+
+ return blob_check_type(data, len, blob_type[id]);
}
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
}
int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
diff --git
a/blobmsg.h
b/blobmsg.h
index
7c63dbf
..
e978235
100644
(file)
--- a/
blobmsg.h
+++ b/
blobmsg.h
@@
-49,19
+49,19
@@
static inline int blobmsg_hdrlen(int namelen)
return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1);
}
return BLOBMSG_PADDING(sizeof(struct blobmsg_hdr) + namelen + 1);
}
-static inline c
har *blobmsg_name(
struct blob_attr *attr)
+static inline c
onst char *blobmsg_name(const
struct blob_attr *attr)
{
struct blobmsg_hdr *hdr = blob_data(attr);
{
struct blobmsg_hdr *hdr = blob_data(attr);
- return (char *) hdr->name;
+ return (c
onst c
har *) hdr->name;
}
}
-static inline void *blobmsg_data(struct blob_attr *attr)
+static inline void *blobmsg_data(
const
struct blob_attr *attr)
{
struct blobmsg_hdr *hdr = blob_data(attr);
return (char *) hdr + blobmsg_hdrlen(hdr->namelen);
}
{
struct blobmsg_hdr *hdr = blob_data(attr);
return (char *) hdr + blobmsg_hdrlen(hdr->namelen);
}
-static inline int blobmsg_data_len(struct blob_attr *attr)
+static inline int blobmsg_data_len(
const
struct blob_attr *attr)
{
uint8_t *start, *end;
{
uint8_t *start, *end;