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.
This commit is contained in:
Thomas Haller 2022-12-12 15:51:34 +01:00
parent 26e7f707c5
commit 2219302545
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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);
}
/*****************************************************************************/