all: avoid calling g_free on a const pointer with g_clear_pointer()

With g_clear_pointer(pptr, g_free), pptr is cast to a non-const pointer,
and hence there is no compiler warning about calling g_free() in a const
pointer. However, it still feels ugly to pass a const pointer to
g_clear_pointer(). We should either add an explicity cast, or just
make the pointer non-const.

I guess part of the problem is that C's "const char *" means that the
string itself is immutable, but also that it cannot be freed. We most
often want a different semantic: the string itself is immutable after
being initialized once, but the memory itself can and will be freed.
Such a notion of immutable C strings cannot be expressed.

For that, just remove the "const" from the declarations. Although we
don't want to modify the (content of the) string, it's still more a
mutable string.

Only in _vt_cmd_obj_copy_lnk_vlan(), add an explicity cast but keep the
type as const. The reason is, that we really want that NMPObject
instances are immutable (in the sense that they don't modify while
existing), but that doesn't mean the memory cannot be freed.
This commit is contained in:
Thomas Haller 2018-03-19 13:19:53 +01:00
parent 9545a8bc34
commit f0442a47ed
3 changed files with 8 additions and 8 deletions

View file

@ -400,8 +400,8 @@ typedef struct _NMDevicePrivate {
guint timeout;
guint watch;
GPid pid;
const char *binary;
const char *address;
char *binary;
char *address;
guint deadline;
} gw_ping;

View file

@ -78,7 +78,7 @@ typedef struct {
GHashTable *interfaces; /* interface uuid => OpenvswitchInterface */
GHashTable *ports; /* port uuid => OpenvswitchPort */
GHashTable *bridges; /* bridge uuid => OpenvswitchBridge */
const char *db_uuid;
char *db_uuid;
} NMOvsdbPrivate;
struct _NMOvsdb {
@ -127,7 +127,7 @@ typedef struct {
OvsdbMethodCallback callback;
gpointer user_data;
union {
const char *ifname;
char *ifname;
struct {
NMConnection *bridge;
NMConnection *port;
@ -867,7 +867,7 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg)
if (ovs) {
iter = json_object_iter (ovs);
priv->db_uuid = g_strdup (iter ? json_object_iter_key (iter) : NULL);
priv->db_uuid = iter ? g_strdup (json_object_iter_key (iter)) : NULL;
}
/* Interfaces */

View file

@ -325,7 +325,7 @@ _vlan_xgress_qos_mappings_cmp (guint n_map,
static void
_vlan_xgress_qos_mappings_cpy (guint *dst_n_map,
const NMVlanQosMapping **dst_map,
NMVlanQosMapping **dst_map,
guint src_n_map,
const NMVlanQosMapping *src_map)
{
@ -916,11 +916,11 @@ _vt_cmd_obj_copy_lnk_vlan (NMPObject *dst, const NMPObject *src)
{
dst->lnk_vlan = src->lnk_vlan;
_vlan_xgress_qos_mappings_cpy (&dst->_lnk_vlan.n_ingress_qos_map,
&dst->_lnk_vlan.ingress_qos_map,
NM_UNCONST_PPTR (NMVlanQosMapping, &dst->_lnk_vlan.ingress_qos_map),
src->_lnk_vlan.n_ingress_qos_map,
src->_lnk_vlan.ingress_qos_map);
_vlan_xgress_qos_mappings_cpy (&dst->_lnk_vlan.n_egress_qos_map,
&dst->_lnk_vlan.egress_qos_map,
NM_UNCONST_PPTR (NMVlanQosMapping, &dst->_lnk_vlan.egress_qos_map),
src->_lnk_vlan.n_egress_qos_map,
src->_lnk_vlan.egress_qos_map);
}