core: transfer ownership of strbuf data in _fw_nft_set()

In practice there is little difference.

Previously, "strbuf" would own the string until the end of the function,
when the "nm_auto_str_buf" cleanup attribute destroys it. In the
meantime, we would pass it on to _fw_nft_call_sync(), which in fact
won't access the string after returning.

Instead, we can just transfer ownership to the GBytes instance. That seems
more logical and safer than aliasing the buffer owned by NMStrBuf with
a g_bytes_new_static(). That way, we don't add a non-obvious restriction
on the lifetime of the string. The lifetime is now guarded by the GBytes
instance, which, could be referenced and kept alive longer.

There is also no runtime/memory overhead in doing this.
This commit is contained in:
Thomas Haller
2022-05-02 16:34:06 +02:00
parent 04ce34d8dc
commit 6a04bcc59d

View File

@@ -679,8 +679,7 @@ _fw_nft_set(gboolean add, const char *ip_iface, in_addr_t addr, guint8 plen)
NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL, NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL,
&ss1)); &ss1));
stdin_buf = g_bytes_new_static(nm_str_buf_get_str(&strbuf), strbuf.len); stdin_buf = nm_str_buf_finalize_to_gbytes(&strbuf);
_fw_nft_call_sync(stdin_buf, NULL); _fw_nft_call_sync(stdin_buf, NULL);
} }