diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h --- a/sys/netlink/netlink_snl.h +++ b/sys/netlink/netlink_snl.h @@ -1039,23 +1039,23 @@ uint32_t size; struct nlmsghdr *hdr; struct snl_state *ss; - bool error; }; -static inline void +static inline bool snl_init_writer(struct snl_state *ss, struct snl_writer *nw) { - nw->size = SNL_WRITER_BUFFER_SIZE; - nw->base = (char *)snl_allocz(ss, nw->size); - if (nw->base == NULL) { - nw->error = true; - nw->size = 0; - } + char *base; - nw->offset = 0; - nw->hdr = NULL; - nw->error = false; - nw->ss = ss; + if (__predict_false((base = + snl_allocz(ss, SNL_WRITER_BUFFER_SIZE)) == NULL)) + return (NULL); + *nw = (struct snl_writer){ + .base = base, + .size = SNL_WRITER_BUFFER_SIZE, + .ss = ss, + }; + + return (true); } static inline bool @@ -1066,13 +1066,8 @@ while (new_size < nw->size + sz) new_size *= 2; - if (nw->error) + if (__predict_false(snl_allocz(nw->ss, new_size) == NULL)) return (false); - - if (snl_allocz(nw->ss, new_size) == NULL) { - nw->error = true; - return (false); - } nw->size = new_size; void *new_base = nw->ss->lb->base; @@ -1254,10 +1249,8 @@ static inline void snl_end_attr_nested(const struct snl_writer *nw, int off) { - if (!nw->error) { - struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr); - nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off); - } + struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr); + nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off); } static inline struct nlmsghdr * @@ -1277,33 +1270,27 @@ return (hdr); } -static void +static inline void snl_abort_msg(struct snl_writer *nw) { - if (nw->hdr != NULL) { - int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr); + assert(nw->hdr != NULL); - nw->offset -= offset; - nw->hdr = NULL; - } + nw->offset -= (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr); + nw->hdr = NULL; } static inline struct nlmsghdr * snl_finalize_msg(struct snl_writer *nw) { - if (nw->error) - snl_abort_msg(nw); - if (nw->hdr != NULL) { - struct nlmsghdr *hdr = nw->hdr; + struct nlmsghdr *hdr = nw->hdr; - int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr); - hdr->nlmsg_len = offset; - hdr->nlmsg_seq = snl_get_seq(nw->ss); - nw->hdr = NULL; + assert(hdr != NULL); - return (hdr); - } - return (NULL); + hdr->nlmsg_len = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr); + hdr->nlmsg_seq = snl_get_seq(nw->ss); + nw->hdr = NULL; + + return (hdr); } static inline bool diff --git a/tests/sys/netlink/test_snl.c b/tests/sys/netlink/test_snl.c --- a/tests/sys/netlink/test_snl.c +++ b/tests/sys/netlink/test_snl.c @@ -65,13 +65,13 @@ int optval = 1; ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_CAP_ACK, &optval, sizeof(optval)) == 0); - snl_init_writer(&ss, &nw); + ATF_CHECK(snl_init_writer(&ss, &nw)); struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255); ATF_CHECK(hdr != NULL); ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL); snl_add_msg_attr_string(&nw, 143, "some random string"); - ATF_CHECK(snl_finalize_msg(&nw) != NULL); + snl_finalize_msg(&nw); ATF_CHECK(snl_send_message(&ss, hdr)); @@ -107,13 +107,13 @@ optval = 1; ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_EXT_ACK, &optval, sizeof(optval)) == 0); - snl_init_writer(&ss, &nw); + ATF_CHECK(snl_init_writer(&ss, &nw)); struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255); ATF_CHECK(hdr != NULL); ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL); snl_add_msg_attr_string(&nw, 143, "some random string"); - ATF_CHECK(snl_finalize_msg(&nw) != NULL); + snl_finalize_msg(&nw); ATF_CHECK(snl_send_message(&ss, hdr)); @@ -147,13 +147,13 @@ int optval = 1; ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_EXT_ACK, &optval, sizeof(optval)) == 0); - snl_init_writer(&ss, &nw); + ATF_CHECK(snl_init_writer(&ss, &nw)); struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255); ATF_CHECK(hdr != NULL); ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL); snl_add_msg_attr_string(&nw, 143, "some random string"); - ATF_CHECK(snl_finalize_msg(&nw) != NULL); + snl_finalize_msg(&nw); ATF_CHECK(snl_send_message(&ss, hdr)); @@ -205,12 +205,12 @@ if (!snl_init(&ss, NETLINK_ROUTE)) atf_tc_fail("snl_init() failed"); - snl_init_writer(&ss, &nw); + ATF_CHECK(snl_init_writer(&ss, &nw)); struct nlmsghdr *hdr = snl_create_msg_request(&nw, RTM_GETLINK); ATF_CHECK(hdr != NULL); ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL); - ATF_CHECK(snl_finalize_msg(&nw) != NULL); + snl_finalize_msg(&nw); uint32_t seq_id = hdr->nlmsg_seq; ATF_CHECK(snl_send_message(&ss, hdr)); diff --git a/tests/sys/netlink/test_snl_generic.c b/tests/sys/netlink/test_snl_generic.c --- a/tests/sys/netlink/test_snl_generic.c +++ b/tests/sys/netlink/test_snl_generic.c @@ -83,7 +83,7 @@ if (!snl_init(&ss, NETLINK_GENERIC)) atf_tc_fail("snl_init() failed"); - snl_init_writer(&ss, &nw); + ATF_CHECK(snl_init_writer(&ss, &nw)); hdr = snl_create_genl_msg_request(&nw, GENL_ID_CTRL, CTRL_CMD_GETFAMILY); snl_add_msg_attr_string(&nw, CTRL_ATTR_FAMILY_NAME, "nlctrl"); hdr = snl_finalize_msg(&nw);