[network-manager-applet] all: re-import shared files



commit 554fafd9faf185342b77566de58d5c05d9be1c69
Author: Thomas Haller <thaller redhat com>
Date:   Thu Dec 21 10:09:22 2017 +0100

    all: re-import shared files
    
    - enable -Wvla and -Wunused-but-set-variable warnings
    - minor bugfixes in shared files and new API.

 m4/compiler_options.m4               |    2 +-
 shared/nm-utils/nm-macros-internal.h |    9 +++-
 shared/nm-utils/nm-shared-utils.c    |  105 +++++++++++++++++++++++++++++++++-
 shared/nm-utils/nm-shared-utils.h    |   34 ++++++++++-
 shared/nm-utils/nm-test-utils.h      |   10 +++-
 5 files changed, 153 insertions(+), 7 deletions(-)
---
diff --git a/m4/compiler_options.m4 b/m4/compiler_options.m4
index ccb51f5..b96e350 100644
--- a/m4/compiler_options.m4
+++ b/m4/compiler_options.m4
@@ -79,13 +79,13 @@ if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
                      -Wshift-negative-value \
                      -Wstrict-prototypes \
                      -Wundef \
+                     -Wvla \
                      -Wno-duplicate-decl-specifier \
                      -Wno-format-truncation \
                      -Wno-format-y2k \
                      -Wno-missing-field-initializers \
                      -Wno-pragmas \
                      -Wno-sign-compare \
-                     -Wno-unused-but-set-variable \
                      -Wno-unused-parameter \
                      ; do
                dnl GCC 4.4 does not warn when checking for -Wno-* flags 
(https://gcc.gnu.org/wiki/FAQ#wnowarning)
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index cf9142f..4ee59f1 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -35,6 +35,12 @@
 #define _nm_alignof(type)    __alignof (type)
 #define _nm_alignas(type)    _nm_align (_nm_alignof (type))
 
+#if __GNUC__ >= 7
+#define _nm_fallthrough      __attribute__ ((fallthrough))
+#else
+#define _nm_fallthrough
+#endif
+
 /*****************************************************************************/
 
 #ifdef thread_local
@@ -577,7 +583,7 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
 
 /* NM_CACHED_QUARK_FCN() is essentially the same as G_DEFINE_QUARK
  * with two differences:
- * - @string must be a quited string-literal
+ * - @string must be a quoted string-literal
  * - @fcn must be the full function name, while G_DEFINE_QUARK() appends
  *   "_quark" to the function name.
  * Both properties of G_DEFINE_QUARK() are non favorable, because you can no
@@ -721,6 +727,7 @@ nm_g_object_ref (gpointer obj)
                g_object_ref (obj);
        return obj;
 }
+#define nm_g_object_ref(obj) ((typeof (obj)) nm_g_object_ref (obj))
 
 static inline void
 nm_g_object_unref (gpointer obj)
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index 79d1e6a..25b8675 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -569,7 +569,7 @@ nm_utils_strsplit_set (const char *str, const char *delimiters)
 
                        /* reallocate the buffer. Note that for now the string
                         * continues to be in ptr0/s0. We fix that at the end. */
-                       alloc_size += 2;
+                       alloc_size *= 2;
                        ptr = g_malloc ((sizeof (const char *) * (alloc_size + 1)) + str_len);
                        memcpy (ptr, ptr_old, sizeof (const char *) * plen);
                        if (ptr_old != ptr0)
@@ -845,6 +845,32 @@ nm_g_object_set_property (GObject *object,
        return TRUE;
 }
 
+gboolean
+nm_g_object_set_property_boolean (GObject *object,
+                                  const gchar  *property_name,
+                                  gboolean value,
+                                  GError **error)
+{
+       nm_auto_unset_gvalue GValue gvalue = { 0 };
+
+       g_value_init (&gvalue, G_TYPE_BOOLEAN);
+       g_value_set_boolean (&gvalue, !!value);
+       return nm_g_object_set_property (object, property_name, &gvalue, error);
+}
+
+gboolean
+nm_g_object_set_property_uint (GObject *object,
+                               const gchar  *property_name,
+                               guint value,
+                               GError **error)
+{
+       nm_auto_unset_gvalue GValue gvalue = { 0 };
+
+       g_value_init (&gvalue, G_TYPE_UINT);
+       g_value_set_uint (&gvalue, value);
+       return nm_g_object_set_property (object, property_name, &gvalue, error);
+}
+
 GParamSpec *
 nm_g_object_class_find_property_from_gtype (GType gtype,
                                             const char *property_name)
@@ -1089,3 +1115,80 @@ nm_utils_fd_read_loop_exact (int fd, void *buf, size_t nbytes, bool do_poll)
 
        return 0;
 }
+
+NMUtilsNamedValue *
+nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len)
+{
+       GHashTableIter iter;
+       NMUtilsNamedValue *values;
+       guint i, len;
+
+       if (   !hash
+           || !(len = g_hash_table_size (hash))) {
+               NM_SET_OUT (out_len, 0);
+               return NULL;
+       }
+
+       i = 0;
+       values = g_new (NMUtilsNamedValue, len + 1);
+       g_hash_table_iter_init (&iter, hash);
+       while (g_hash_table_iter_next (&iter,
+                                      (gpointer *) &values[i].name,
+                                      (gpointer *) &values[i].value_ptr))
+               i++;
+       nm_assert (i == len);
+       values[i].name = NULL;
+       values[i].value_ptr = NULL;
+
+       if (len > 1) {
+               g_qsort_with_data (values, len, sizeof (values[0]),
+                                  nm_utils_named_entry_cmp_with_data, NULL);
+       }
+
+       NM_SET_OUT (out_len, len);
+       return values;
+}
+
+const char **
+nm_utils_strdict_get_keys (const GHashTable *hash,
+                           gboolean sorted,
+                           guint *out_length)
+{
+       const char **names;
+       guint length;
+
+       if (   !hash
+           || !g_hash_table_size ((GHashTable *) hash)) {
+               NM_SET_OUT (out_length, 0);
+               return NULL;
+       }
+
+       names = (const char **) g_hash_table_get_keys_as_array ((GHashTable *) hash, &length);
+       if (   sorted
+           && length > 1) {
+               g_qsort_with_data (names,
+                                  length,
+                                  sizeof (char *),
+                                  nm_strcmp_p_with_data,
+                                  NULL);
+       }
+       NM_SET_OUT (out_length, length);
+       return names;
+}
+
+char **
+nm_utils_strv_make_deep_copied (const char **strv)
+{
+       gsize i;
+
+       /* it takes a strv dictionary, and copies each
+        * strings. Note that this updates @strv *in-place*
+        * and returns it. */
+
+       if (!strv)
+               return NULL;
+       for (i = 0; strv[i]; i++)
+               strv[i] = g_strdup (strv[i]);
+
+       return (char **) strv;
+}
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index 1e80b35..e52b419 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -326,12 +326,18 @@ _nm_g_slice_free_fcn_define (16)
                /* If mem_size is a compile time constant, the compiler
                 * will be able to optimize this. Hence, you don't want
                 * to call this with a non-constant size argument. */ \
-               switch (mem_size) { \
+               G_STATIC_ASSERT_EXPR (   ((mem_size) ==  1) \
+                                     || ((mem_size) ==  2) \
+                                     || ((mem_size) ==  4) \
+                                     || ((mem_size) ==  8) \
+                                     || ((mem_size) == 12) \
+                                     || ((mem_size) == 16)); \
+               switch ((mem_size)) { \
                case  1: _fcn = _nm_g_slice_free_fcn_1;  break; \
                case  2: _fcn = _nm_g_slice_free_fcn_2;  break; \
                case  4: _fcn = _nm_g_slice_free_fcn_4;  break; \
                case  8: _fcn = _nm_g_slice_free_fcn_8;  break; \
-               case 12: _fcn = _nm_g_slice_free_fcn_12;  break; \
+               case 12: _fcn = _nm_g_slice_free_fcn_12; break; \
                case 16: _fcn = _nm_g_slice_free_fcn_16; break; \
                default: g_assert_not_reached (); _fcn = NULL; break; \
                } \
@@ -386,6 +392,16 @@ gboolean nm_g_object_set_property (GObject *object,
                                    const GValue *value,
                                    GError **error);
 
+gboolean nm_g_object_set_property_boolean (GObject *object,
+                                           const gchar  *property_name,
+                                           gboolean value,
+                                           GError **error);
+
+gboolean nm_g_object_set_property_uint (GObject *object,
+                                        const gchar  *property_name,
+                                        guint value,
+                                        GError **error);
+
 GParamSpec *nm_g_object_class_find_property_from_gtype (GType gtype,
                                                         const char *property_name);
 
@@ -425,6 +441,20 @@ typedef struct {
 #define nm_utils_named_entry_cmp           nm_strcmp_p
 #define nm_utils_named_entry_cmp_with_data nm_strcmp_p_with_data
 
+NMUtilsNamedValue *nm_utils_named_values_from_str_dict (GHashTable *hash, guint *out_len);
+
+const char **nm_utils_strdict_get_keys (const GHashTable *hash,
+                                        gboolean sorted,
+                                        guint *out_length);
+
+char **nm_utils_strv_make_deep_copied (const char **strv);
+
+static inline char **
+nm_utils_strv_make_deep_copied_nonnull (const char **strv)
+{
+       return nm_utils_strv_make_deep_copied (strv) ?: g_new0 (char *, 1);
+}
+
 /*****************************************************************************/
 
 #define NM_UTILS_NS_PER_SECOND  ((gint64) 1000000000)
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h
index 8d3ff10..0646df8 100644
--- a/shared/nm-utils/nm-test-utils.h
+++ b/shared/nm-utils/nm-test-utils.h
@@ -424,6 +424,11 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_
                        g_array_append_val (debug_messages, msg);
                }
        } else {
+               /* We're intentionally assigning a value to static variables
+                * s_tests_x and p_tests_x without using it afterwards, just
+                * so that valgrind doesn't complain about the leak. */
+               NM_PRAGMA_WARNING_DISABLE("-Wunused-but-set-variable")
+
                /* g_test_init() is a variadic function, so we cannot pass it
                 * (variadic) arguments. If you need to pass additional parameters,
                 * call nmtst_init() with argc==NULL and call g_test_init() yourself. */
@@ -497,6 +502,8 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_
                                s_tests = NULL;
                        }
                }
+
+               NM_PRAGMA_WARNING_REENABLE
        }
 
        if (test_quick_set)
@@ -1499,13 +1506,12 @@ _nmtst_connection_normalize (NMConnection *connection, ...)
 static inline NMConnection *
 _nmtst_connection_duplicate_and_normalize (NMConnection *connection, ...)
 {
-       gboolean was_modified;
        va_list args;
 
        connection = nmtst_clone_connection (connection);
 
        va_start (args, connection);
-       was_modified = _nmtst_connection_normalize_v (connection, args);
+       _nmtst_connection_normalize_v (connection, args);
        va_end (args);
 
        return connection;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]