From 2219302545021859ca01dfa882bb26a19ca9236a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 12 Dec 2022 15:51:34 +0100 Subject: [PATCH] libnm: use stack-buffer to construct string in nm_ip_routing_rule_to_string() There are two benefits: - the returned (allocated) string will have exactly the required length and no excess buffer that was used to build the string. - the string is (most likely) short enough to fit in 488 bytes on the stack. There is no re-allocation necessary to grow the buffer. --- src/libnm-core-impl/nm-setting-ip-config.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libnm-core-impl/nm-setting-ip-config.c b/src/libnm-core-impl/nm-setting-ip-config.c index cfdf2c7c24..b4fdd040d4 100644 --- a/src/libnm-core-impl/nm-setting-ip-config.c +++ b/src/libnm-core-impl/nm-setting-ip-config.c @@ -3791,8 +3791,8 @@ nm_ip_routing_rule_to_string(const NMIPRoutingRule *self, GHashTable *extra_args, GError **error) { - int addr_family; - NMStrBuf str; + int addr_family; + nm_auto_str_buf NMStrBuf str = NM_STR_BUF_INIT_A(NM_UTILS_GET_NEXT_REALLOC_SIZE_488, FALSE); g_return_val_if_fail(NM_IS_IP_ROUTING_RULE(self, TRUE), NULL); @@ -3830,8 +3830,6 @@ nm_ip_routing_rule_to_string(const NMIPRoutingRule *self, } } - str = NM_STR_BUF_INIT(NM_UTILS_GET_NEXT_REALLOC_SIZE_32, FALSE); - if (self->priority_has) { nm_str_buf_append_printf(nm_str_buf_append_required_delimiter(&str, ' '), "priority %u", @@ -3938,7 +3936,7 @@ nm_ip_routing_rule_to_string(const NMIPRoutingRule *self, nm_net_aux_rtnl_rtntype_n2a_maybe_buf(self->action, sbuf)); } - return nm_str_buf_finalize(&str, NULL); + return nm_str_buf_dup_str(&str); } /*****************************************************************************/