Skip to content

Commit 416e841

Browse files
committed
sd-netlink: introduce sd_netlink_message_get_max_attribute()
1 parent e0df8e9 commit 416e841

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/libsystemd/sd-netlink/netlink-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct netlink_container {
112112
const struct NLTypeSystem *type_system; /* the type system of the container */
113113
size_t offset; /* offset from hdr to the start of the container */
114114
struct netlink_attribute *attributes;
115-
unsigned short n_attributes; /* number of attributes in container */
115+
uint16_t max_attribute; /* the maximum attribute in container */
116116
};
117117

118118
struct sd_netlink_message {

src/libsystemd/sd-netlink/netlink-message.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ static int netlink_message_read_internal(
712712
if (!m->containers[m->n_containers].attributes)
713713
return -ENODATA;
714714

715-
if (type >= m->containers[m->n_containers].n_attributes)
715+
if (type > m->containers[m->n_containers].max_attribute)
716716
return -ENODATA;
717717

718718
attribute = &m->containers[m->n_containers].attributes[type];
@@ -1093,38 +1093,38 @@ int sd_netlink_message_read_strv(sd_netlink_message *m, unsigned short container
10931093
return 0;
10941094
}
10951095

1096-
static int netlink_container_parse(sd_netlink_message *m,
1097-
struct netlink_container *container,
1098-
struct rtattr *rta,
1099-
size_t rt_len) {
1096+
static int netlink_container_parse(
1097+
sd_netlink_message *m,
1098+
struct netlink_container *container,
1099+
struct rtattr *rta,
1100+
size_t rt_len) {
1101+
11001102
_cleanup_free_ struct netlink_attribute *attributes = NULL;
1101-
size_t n = 0;
1103+
uint16_t max_attr = 0;
11021104

11031105
/* RTA_OK() macro compares with rta->rt_len, which is unsigned short, and
11041106
* LGTM.com analysis does not like the type difference. Hence, here we
11051107
* introduce an unsigned short variable as a workaround. */
11061108
unsigned short len = rt_len;
11071109
for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
1108-
unsigned short type;
1110+
uint16_t attr;
11091111

1110-
type = RTA_TYPE(rta);
1112+
attr = RTA_TYPE(rta);
1113+
max_attr = MAX(max_attr, attr);
11111114

1112-
if (!GREEDY_REALLOC0(attributes, type + 1))
1115+
if (!GREEDY_REALLOC0(attributes, (size_t) max_attr + 1))
11131116
return -ENOMEM;
11141117

1115-
if (attributes[type].offset != 0)
1118+
if (attributes[attr].offset != 0)
11161119
log_debug("sd-netlink: message parse - overwriting repeated attribute");
11171120

1118-
attributes[type].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
1119-
attributes[type].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
1120-
attributes[type].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
1121-
1122-
if (type + 1U > n)
1123-
n = type + 1U;
1121+
attributes[attr].offset = (uint8_t *) rta - (uint8_t *) m->hdr;
1122+
attributes[attr].nested = RTA_FLAGS(rta) & NLA_F_NESTED;
1123+
attributes[attr].net_byteorder = RTA_FLAGS(rta) & NLA_F_NET_BYTEORDER;
11241124
}
11251125

11261126
container->attributes = TAKE_PTR(attributes);
1127-
container->n_attributes = n;
1127+
container->max_attribute = max_attr;
11281128

11291129
return 0;
11301130
}
@@ -1259,13 +1259,23 @@ int sd_netlink_message_exit_container(sd_netlink_message *m) {
12591259
assert_return(m->n_containers > 0, -EINVAL);
12601260

12611261
m->containers[m->n_containers].attributes = mfree(m->containers[m->n_containers].attributes);
1262+
m->containers[m->n_containers].max_attribute = 0;
12621263
m->containers[m->n_containers].type_system = NULL;
12631264

12641265
m->n_containers--;
12651266

12661267
return 0;
12671268
}
12681269

1270+
int sd_netlink_message_get_max_attribute(sd_netlink_message *m, uint16_t *ret) {
1271+
assert_return(m, -EINVAL);
1272+
assert_return(m->sealed, -EINVAL);
1273+
assert_return(ret, -EINVAL);
1274+
1275+
*ret = m->containers[m->n_containers].max_attribute;
1276+
return 0;
1277+
}
1278+
12691279
uint32_t message_get_serial(sd_netlink_message *m) {
12701280
assert(m);
12711281
assert(m->hdr);

src/systemd/sd-netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ int sd_netlink_message_get_errno(sd_netlink_message *m);
126126
int sd_netlink_message_get_type(sd_netlink_message *m, uint16_t *type);
127127
int sd_netlink_message_set_flags(sd_netlink_message *m, uint16_t flags);
128128
int sd_netlink_message_is_broadcast(sd_netlink_message *m);
129+
int sd_netlink_message_get_max_attribute(sd_netlink_message *m, uint16_t *ret);
129130

130131
/* rtnl */
131132
int sd_rtnl_message_get_family(sd_netlink_message *m, int *family);

0 commit comments

Comments
 (0)