summaryrefslogtreecommitdiff
path: root/src/nm-core-utils.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-10-23 13:55:54 +0200
committerThomas Haller <thaller@redhat.com>2017-10-24 16:04:46 +0200
commit6e01238a407ac546bf20fd23998b28cde672aeba (patch)
treef16f64f0640b1c912d2a1bea930543a16a85eb8a /src/nm-core-utils.c
parentb27a10bde8f0e28b16cc52e2c2f7de39d44cf0e6 (diff)
downloadNetworkManager-6e01238a407ac546bf20fd23998b28cde672aeba.tar.gz
core: don't use static buffer for nm_utils_ip4_property_path()
and nm_utils_ip6_property_path(). The API with static buffers looks a bit nicer. But I think they are dangerous, because we tend to pass the buffer down several layers of the stack, and it's not immediately clear, that we don't overwrite the static buffer again (which we probably did not, but it's hard to verify that there is no bug there).
Diffstat (limited to 'src/nm-core-utils.c')
-rw-r--r--src/nm-core-utils.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c
index 138d871717..2325283e6d 100644
--- a/src/nm-core-utils.c
+++ b/src/nm-core-utils.c
@@ -2525,55 +2525,63 @@ nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ns_
#define IPV6_PROPERTY_DIR "/proc/sys/net/ipv6/conf/"
#define IPV4_PROPERTY_DIR "/proc/sys/net/ipv4/conf/"
G_STATIC_ASSERT (sizeof (IPV4_PROPERTY_DIR) == sizeof (IPV6_PROPERTY_DIR));
+G_STATIC_ASSERT (NM_STRLEN (IPV6_PROPERTY_DIR) + IFNAMSIZ + 60 == NM_UTILS_IP_PROPERTY_PATH_BUFSIZE);
static const char *
-_get_property_path (const char *ifname,
+_get_property_path (char *buf,
+ const char *ifname,
const char *property,
gboolean ipv6)
{
- static char path[sizeof (IPV6_PROPERTY_DIR) + IFNAMSIZ + 32];
int len;
+ nm_assert (buf);
+
ifname = NM_ASSERT_VALID_PATH_COMPONENT (ifname);
property = NM_ASSERT_VALID_PATH_COMPONENT (property);
- len = g_snprintf (path,
- sizeof (path),
+ len = g_snprintf (buf,
+ NM_UTILS_IP_PROPERTY_PATH_BUFSIZE,
"%s%s/%s",
ipv6 ? IPV6_PROPERTY_DIR : IPV4_PROPERTY_DIR,
ifname,
property);
- g_assert (len < sizeof (path) - 1);
-
- return path;
+ g_assert (len < NM_UTILS_IP_PROPERTY_PATH_BUFSIZE - 1);
+ return buf;
}
/**
* nm_utils_ip6_property_path:
+ * @buf: the output buffer where to write the path. It
+ * must be at least NM_UTILS_IP_PROPERTY_PATH_BUFSIZE bytes
+ * long.
* @ifname: an interface name
* @property: a property name
*
- * Returns the path to IPv6 property @property on @ifname. Note that
- * this uses a static buffer.
+ * Returns: the path to IPv6 property @property on @ifname. Note that
+ * this returns the input argument @buf.
*/
const char *
-nm_utils_ip6_property_path (const char *ifname, const char *property)
+nm_utils_ip6_property_path (char *buf, const char *ifname, const char *property)
{
- return _get_property_path (ifname, property, TRUE);
+ return _get_property_path (buf, ifname, property, TRUE);
}
/**
* nm_utils_ip4_property_path:
+ * @buf: the output buffer where to write the path. It
+ * must be at least NM_UTILS_IP_PROPERTY_PATH_BUFSIZE bytes
+ * long.
* @ifname: an interface name
* @property: a property name
*
- * Returns the path to IPv4 property @property on @ifname. Note that
- * this uses a static buffer.
+ * Returns: the path to IPv6 property @property on @ifname. Note that
+ * this returns the input argument @buf.
*/
const char *
-nm_utils_ip4_property_path (const char *ifname, const char *property)
+nm_utils_ip4_property_path (char *buf, const char *ifname, const char *property)
{
- return _get_property_path (ifname, property, FALSE);
+ return _get_property_path (buf, ifname, property, FALSE);
}
gboolean