1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

sd-netlink: don't access type->type_system[_union] directly

Make sure we never access type->type_system or type->type_system_union
directly. This is an implementation detail of the type-system and we
should always use the accessors. Right now, they only exist for 2-level
accesses (type-system to type-system). This patch introduces the 1-level
accessors (type to type-system) and makes use of it.

This patch makes sure the proper assertions are in place, so we never
accidentally access sub-type-systems for non-nested/union types.

Note that this places hard-asserts on the accessors. This should be fine,
as we expect callers to only access sub type-systems if they *know*
they're dealing with nested types.
This commit is contained in:
David Herrmann 2015-06-23 10:51:25 +02:00
parent cafbc790d1
commit c658008f50
3 changed files with 26 additions and 13 deletions

View File

@ -85,7 +85,8 @@ int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type) {
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
m->container_type_system[0] = nl_type->type_system;
if (nl_type->type == NETLINK_TYPE_NESTED)
type_get_type_system(nl_type, &m->container_type_system[0]);
m->hdr->nlmsg_len = size;
m->hdr->nlmsg_type = type;
@ -872,9 +873,9 @@ int sd_netlink_message_rewind(sd_netlink_message *m) {
return r;
if (type->type == NETLINK_TYPE_NESTED) {
const NLTypeSystem *type_system = type->type_system;
const NLTypeSystem *type_system;
assert(type_system);
type_get_type_system(type, &type_system);
m->container_type_system[0] = type_system;

View File

@ -460,6 +460,24 @@ const NLTypeSystem rtnl_type_system = {
.types = rtnl_types,
};
void type_get_type_system(const NLType *nl_type, const NLTypeSystem **ret) {
assert(nl_type);
assert(ret);
assert(nl_type->type == NETLINK_TYPE_NESTED);
assert(nl_type->type_system);
*ret = nl_type->type_system;
}
void type_get_type_system_union(const NLType *nl_type, const NLTypeSystemUnion **ret) {
assert(nl_type);
assert(ret);
assert(nl_type->type == NETLINK_TYPE_UNION);
assert(nl_type->type_system_union);
*ret = nl_type->type_system_union;
}
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type) {
const NLType *nl_type;
@ -493,11 +511,7 @@ int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSys
if (r < 0)
return r;
assert(nl_type->type == NETLINK_TYPE_NESTED);
assert(nl_type->type_system);
*ret = nl_type->type_system;
type_get_type_system(nl_type, ret);
return 0;
}
@ -511,11 +525,7 @@ int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLT
if (r < 0)
return r;
assert(nl_type->type == NETLINK_TYPE_UNION);
assert(nl_type->type_system_union);
*ret = nl_type->type_system_union;
type_get_type_system_union(nl_type, ret);
return 0;
}

View File

@ -65,6 +65,8 @@ struct NLType {
const NLTypeSystemUnion *type_system_union;
};
void type_get_type_system(const NLType *type, const NLTypeSystem **ret);
void type_get_type_system_union(const NLType *type, const NLTypeSystemUnion **ret);
int type_system_get_type(const NLTypeSystem *type_system, const NLType **ret, uint16_t type);
int type_system_get_type_system(const NLTypeSystem *type_system, const NLTypeSystem **ret, uint16_t type);
int type_system_get_type_system_union(const NLTypeSystem *type_system, const NLTypeSystemUnion **ret, uint16_t type);