glib-aux,libnm: add nm_ascii_is_regular_char() to validate keys for "ovs-external-ids"

The same will also be used by "ovs-other-config". Also, there should be
a general concept, meaning, we should have a function whether a character
is from some benign set, and not whether we have a character usable for
keys of "ovs-external-ids".
This commit is contained in:
Thomas Haller
2023-01-09 20:57:52 +01:00
parent 2c056cf9a3
commit 4c2db6a3fd
2 changed files with 27 additions and 10 deletions

View File

@@ -53,15 +53,6 @@ G_DEFINE_TYPE(NMSettingOvsExternalIDs, nm_setting_ovs_external_ids, NM_TYPE_SETT
/*****************************************************************************/
static gboolean
_exid_key_char_is_regular(char ch)
{
/* allow words of printable characters, plus some
* special characters, for example to support base64 encoding. */
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')
|| NM_IN_SET(ch, '-', '_', '+', '/', '=', '.');
}
/**
* nm_setting_ovs_external_ids_check_key:
* @key: (allow-none): the key to check
@@ -105,7 +96,7 @@ nm_setting_ovs_external_ids_check_key(const char *key, GError **error)
_("key must be UTF8"));
return FALSE;
}
if (!NM_STRCHAR_ALL(key, ch, _exid_key_char_is_regular(ch))) {
if (!NM_STRCHAR_ALL(key, ch, nm_ascii_is_regular_char(ch))) {
/* Probably OVS is more forgiving about what makes a valid key for
* an external-id. However, we are strict (at least, for now). */
g_set_error_literal(error,

View File

@@ -1101,6 +1101,32 @@ nm_ascii_is_newline(char ch)
return NM_IN_SET(ch, '\n', '\t');
}
static inline gboolean
nm_ascii_is_regular_char(char ch)
{
/* Checks whether "ch" is "regular", which basically
* means it's either a digit, a alpha, or some special
* characters that are suitable for base64 encoding.
*
* The meaning of what "regular" means is not well defined,
* but it's used to validate the keys for "ovs.external-ids"
* dictionary. */
switch (ch) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case '-':
case '_':
case '+':
case '/':
case '=':
case '.':
return TRUE;
default:
return FALSE;
}
}
#define nm_str_skip_leading_spaces(str) \
({ \
typeof(*(str)) *_str_sls = (str); \