diff --git a/libfwupdplugin/README.md b/libfwupdplugin/README.md index 0e54f0c98..77e8e3875 100644 --- a/libfwupdplugin/README.md +++ b/libfwupdplugin/README.md @@ -72,3 +72,4 @@ Remember: Plugins should be upstream! * `fu_common_bytes_compare_raw()`: Use `fu_memcmp_safe()` instead * `fu_common_spawn_sync()`: Use `g_spawn_sync()` instead, or ideally not at all! * `fu_common_extract_archive()`: Use `FuArchiveFirmware()` instead. +* `fu_common_instance_id_strsafe()`: Use `fu_device_add_instance_strsafe()` instead. diff --git a/libfwupdplugin/fu-common.c b/libfwupdplugin/fu-common.c index 0edf126ab..6771d7f54 100644 --- a/libfwupdplugin/fu-common.c +++ b/libfwupdplugin/fu-common.c @@ -1946,65 +1946,3 @@ fu_common_reset_firmware_search_path(GError **error) return fu_common_set_firmware_search_path(contents, error); } - -static gboolean -fu_strsafe_instance_id_is_valid_char(gchar c) -{ - if (c == ' ') - return FALSE; - if (c == '_') - return FALSE; - if (c == '&') - return FALSE; - if (c == '/') - return FALSE; - if (c == '\\') - return FALSE; - return g_ascii_isprint(c); -} - -/** - * fu_common_instance_id_strsafe: - * @str: (nullable): part of the string to sanitize - * - * Sanitize the string used as part of the InstanceID. - * - * Returns: a string, or %NULL if invalid - * - * Since: 1.7.6 - **/ -gchar * -fu_common_instance_id_strsafe(const gchar *str) -{ - g_autoptr(GString) tmp = g_string_new(NULL); - gboolean has_content = FALSE; - - /* sanity check */ - if (str == NULL) - return NULL; - - /* use - to replace problematic chars -- but only once per section */ - for (guint i = 0; str[i] != '\0'; i++) { - gchar c = str[i]; - if (!fu_strsafe_instance_id_is_valid_char(c)) { - if (has_content) { - g_string_append_c(tmp, '-'); - has_content = FALSE; - } - } else { - g_string_append_c(tmp, c); - has_content = TRUE; - } - } - - /* remove any trailing replacements */ - if (tmp->len > 0 && tmp->str[tmp->len - 1] == '-') - g_string_truncate(tmp, tmp->len - 1); - - /* nothing left! */ - if (tmp->len == 0) - return NULL; - - /* success */ - return g_string_free(g_steal_pointer(&tmp), FALSE); -} diff --git a/libfwupdplugin/fu-common.h b/libfwupdplugin/fu-common.h index c33b470ef..6acd49953 100644 --- a/libfwupdplugin/fu-common.h +++ b/libfwupdplugin/fu-common.h @@ -207,8 +207,6 @@ fu_common_dump_full(const gchar *log_domain, FuDumpFlags flags); void fu_common_dump_bytes(const gchar *log_domain, const gchar *title, GBytes *bytes); -gchar * -fu_common_instance_id_strsafe(const gchar *str); gboolean fu_common_kernel_locked_down(void); gboolean diff --git a/libfwupdplugin/fu-device.c b/libfwupdplugin/fu-device.c index 5bff35dbe..67cb07940 100644 --- a/libfwupdplugin/fu-device.c +++ b/libfwupdplugin/fu-device.c @@ -5005,6 +5005,59 @@ fu_device_add_instance_str(FuDevice *self, const gchar *key, const gchar *value) g_hash_table_insert(priv->instance_hash, g_strdup(key), g_strdup(value)); } +static gboolean +fu_strsafe_instance_id_is_valid_char(gchar c) +{ + if (c == ' ') + return FALSE; + if (c == '_') + return FALSE; + if (c == '&') + return FALSE; + if (c == '/') + return FALSE; + if (c == '\\') + return FALSE; + return g_ascii_isprint(c); +} + +/* NOTE: we can't use fu_strsafe as this behavior is now effectively ABI */ +static gchar * +fu_common_instance_id_strsafe(const gchar *str) +{ + g_autoptr(GString) tmp = g_string_new(NULL); + gboolean has_content = FALSE; + + /* sanity check */ + if (str == NULL) + return NULL; + + /* use - to replace problematic chars -- but only once per section */ + for (guint i = 0; str[i] != '\0'; i++) { + gchar c = str[i]; + if (!fu_strsafe_instance_id_is_valid_char(c)) { + if (has_content) { + g_string_append_c(tmp, '-'); + has_content = FALSE; + } + } else { + g_string_append_c(tmp, c); + has_content = TRUE; + } + } + + /* remove any trailing replacements */ + if (tmp->len > 0 && tmp->str[tmp->len - 1] == '-') + g_string_truncate(tmp, tmp->len - 1); + + /* nothing left! */ + if (tmp->len == 0) + return NULL; + + /* success */ + return g_string_free(g_steal_pointer(&tmp), FALSE); +} + /** * fu_device_add_instance_strsafe: * @self: a #FuDevice diff --git a/libfwupdplugin/fu-self-test.c b/libfwupdplugin/fu-self-test.c index 3540f79f0..27f4b51b2 100644 --- a/libfwupdplugin/fu-self-test.c +++ b/libfwupdplugin/fu-self-test.c @@ -579,8 +579,21 @@ fu_strsafe_func(void) {"dave\x03\x04XXX", "dave..X"}, {"\x03\x03", NULL}, {NULL, NULL}}; - g_autofree gchar *id_part = fu_common_instance_id_strsafe("_ _LEN&VO&\\&"); - g_assert_cmpstr(id_part, ==, "LEN-VO"); + GPtrArray *instance_ids; + gboolean ret; + g_autoptr(FuContext) ctx = fu_context_new(); + g_autoptr(FuDevice) dev = fu_device_new(ctx); + g_autoptr(GError) error = NULL; + + /* check bespoke legacy instance ID behavior */ + fu_device_add_instance_strsafe(dev, "KEY", "_ _LEN&VO&\\&"); + ret = fu_device_build_instance_id(dev, &error, "SUB", "KEY", NULL); + g_assert_no_error(error); + g_assert_true(ret); + instance_ids = fu_device_get_instance_ids(dev); + g_assert_cmpint(instance_ids->len, ==, 1); + g_assert_cmpstr(g_ptr_array_index(instance_ids, 0), ==, "SUB\\KEY_LEN-VO"); + for (guint i = 0; strs[i].in != NULL; i++) { g_autofree gchar *tmp = fu_strsafe(strs[i].in, 7); g_assert_cmpstr(tmp, ==, strs[i].op); diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index fcffeca68..3d3939200 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -913,7 +913,6 @@ LIBFWUPDPLUGIN_1.7.4 { LIBFWUPDPLUGIN_1.7.6 { global: - fu_common_instance_id_strsafe; fu_common_version_ensure_semver_full; fu_udev_device_get_parent_with_subsystem; local: *;