diff --git a/ratbagd/ratbagd-profile.c b/ratbagd/ratbagd-profile.c index 454d2aa9..3aa92c81 100644 --- a/ratbagd/ratbagd-profile.c +++ b/ratbagd/ratbagd-profile.c @@ -382,8 +382,15 @@ ratbagd_profile_get_name(sd_bus *bus, { struct ratbagd_profile *profile = userdata; const char *name = ratbag_profile_get_name(profile->lib_profile); + _cleanup_free_ char *asciiname; - CHECK_CALL(sd_bus_message_append(reply, "s", name)); + /* This should really be a UTF8 check and it should really be in + * libratbag, but for now this will do to at least not kick us off + * the bus. https://github.com/libratbag/libratbag/issues/617 + */ + asciiname = strdup_ascii_only(name); + + CHECK_CALL(sd_bus_message_append(reply, "s", asciiname)); return 0; } diff --git a/src/libratbag-util.h b/src/libratbag-util.h index 38d51990..8f6a8552 100644 --- a/src/libratbag-util.h +++ b/src/libratbag-util.h @@ -116,6 +116,27 @@ snprintf_safe(char *buf, size_t n, const char *fmt, ...) return rc; } +/** + * Returns a strdup'd string with all non-ascii characters replaced with a + * space. + */ +static inline char * +strdup_ascii_only(const char *str_in) +{ + char *str, *ascii_only; + + ascii_only = strdup_safe(str_in); + str = ascii_only; + while (str && *str) { + unsigned char c = *str; + if (c > 127) + *str = ' '; + + str++; + } + return ascii_only; +} + #define sprintf_safe(buf, fmt, ...) \ snprintf_safe(buf, ARRAY_LENGTH(buf), fmt, __VA_ARGS__) diff --git a/tools/ratbagctl.in b/tools/ratbagctl.in index 91d2e628..e2a850e3 100755 --- a/tools/ratbagctl.in +++ b/tools/ratbagctl.in @@ -522,7 +522,11 @@ def func_profile_get(r, args): def func_profile_name_get(r, args): p, d = find_profile(r, args) - print(p.name) + # See https://github.com/libratbag/libratbag/issues/617 + # ratbag converts to ascii, so this has no real effect there, but + # ratbag-command may still have a non-ascii string. + string = bytes(p.name, 'utf-8', 'ignore') + print(string.decode('utf-8')) def func_profile_name_set(r, args):