@@ -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+
12691279uint32_t message_get_serial (sd_netlink_message * m ) {
12701280 assert (m );
12711281 assert (m -> hdr );
0 commit comments