agent: print error message character in hex form if it's unprintable

Currently, when the agent manager is sent a registration request
containing UTF-8 characters, it will form an invalid error message
using only one of the bytes from the UTF-8 sequence, which causes
an assertion in glib to fail, which replaces the returned error message
with "[Invalid UTF-8]". It will also print an assertion failure to the
console, or crash NetworkManager on non-release builds.

This commit makes it so that it instead prints out the character in
hexadecimal form if it isn't normally printable, so that it is once
again a valid UTF-8 string.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1965

Fixes: a30cf19858 ('agent: add agent manager and minimal agent class')
This commit is contained in:
Jan Vaclav 2024-06-03 13:11:44 +02:00
parent 6de187cb37
commit c9327b2e8b

View file

@ -327,11 +327,17 @@ validate_identifier(const char *identifier, GError **error)
/* FIXME: do complete validation here */
while (p && *p) {
if (!g_ascii_isalnum(*p) && (*p != '_') && (*p != '-') && (*p != '.')) {
char invalid_char[5] = {*p};
if (!g_ascii_isprint(*p)) {
g_snprintf(invalid_char, sizeof(invalid_char), "\\x%02x", *p);
}
g_set_error(error,
NM_AGENT_MANAGER_ERROR,
NM_AGENT_MANAGER_ERROR_INVALID_IDENTIFIER,
"Identifier contains invalid character '%c'",
*p);
"Identifier contains invalid character '%s'",
invalid_char);
return FALSE;
}