libnm: only call strlen() once in NMSetting8021x:path_to_scheme_value()
authorThomas Haller <thaller@redhat.com>
Thu, 26 Feb 2015 01:27:54 +0000 (02:27 +0100)
committerThomas Haller <thaller@redhat.com>
Thu, 12 Mar 2015 17:12:25 +0000 (18:12 +0100)
Also assert that path is not empty.

libnm-core/nm-setting-8021x.c
libnm-util/nm-setting-8021x.c

index 10d29d9..a02e711 100644 (file)
@@ -572,13 +572,16 @@ static GBytes *
 path_to_scheme_value (const char *path)
 {
        GByteArray *array;
+       gsize len;
 
-       g_return_val_if_fail (path != NULL, NULL);
+       g_return_val_if_fail (path != NULL && path[0], NULL);
 
-       /* Add the path scheme tag to the front, then the fielname */
-       array = g_byte_array_sized_new (strlen (path) + strlen (SCHEME_PATH) + 1);
+       len = strlen (path);
+
+       /* Add the path scheme tag to the front, then the filename */
+       array = g_byte_array_sized_new (len + strlen (SCHEME_PATH) + 1);
        g_byte_array_append (array, (const guint8 *) SCHEME_PATH, strlen (SCHEME_PATH));
-       g_byte_array_append (array, (const guint8 *) path, strlen (path));
+       g_byte_array_append (array, (const guint8 *) path, len);
        g_byte_array_append (array, (const guint8 *) "\0", 1);
 
        return g_byte_array_free_to_bytes (array);
index 456d5ee..1dc768c 100644 (file)
@@ -551,14 +551,16 @@ static GByteArray *
 path_to_scheme_value (const char *path)
 {
        GByteArray *array;
+       gsize len;
 
-       g_return_val_if_fail (path != NULL, NULL);
+       g_return_val_if_fail (path != NULL && path[0], NULL);
 
-       /* Add the path scheme tag to the front, then the fielname */
-       array = g_byte_array_sized_new (strlen (path) + strlen (SCHEME_PATH) + 1);
-       g_assert (array);
+       len = strlen (path);
+
+       /* Add the path scheme tag to the front, then the filename */
+       array = g_byte_array_sized_new (len + strlen (SCHEME_PATH) + 1);
        g_byte_array_append (array, (const guint8 *) SCHEME_PATH, strlen (SCHEME_PATH));
-       g_byte_array_append (array, (const guint8 *) path, strlen (path));
+       g_byte_array_append (array, (const guint8 *) path, len);
        g_byte_array_append (array, (const guint8 *) "\0", 1);
        return array;
 }