Commit cc9f0716 authored by Thomas Haller's avatar Thomas Haller

libnm: cleanup _nm_utils_parse_tc_handle()

- g_ascii_strtoll() accepts leading spaces, but it leaves
  the end pointer at the first space after the digit. That means,
  we accepted "1: 0" but not "1 :0". We should either consistently
  accept spaces around the digits/colon or reject it.

- g_ascii_strtoll() accepts "\v" as a space (just like `man 3 isspace`
  comments that "\v" is a space in C and POSIX locale.
  For some reasons (unknown to me) g_ascii_isspace() does not treat
  "\v" as space. And neither does NM_ASCII_SPACES and
  nm_str_skip_leading_spaces().
  We should be consistent about what we consider spaces and what not.
  It's already odd to accept '\n' as spaces here, but well, lets do
  it for the sake of consistency (so that it matches with our
  understanding of ASCII spaces, albeit not POSIX's).

- don't use bogus error domains in "g_set_error (error, 1, 0, ..."
  That is a bug and we have NM_UTILS_ERROR exactly for error instances
  with unspecified domain and code.

- as before, accept a trailing ":" with omitted minor number.

- reject all unexpected characters. strtoll() accepts '+' / '-'
  and a "0x" prefix of the numbers (and leading POSIX spaces). Be
  strict here and only accepts NM_ASCII_SPACES, ':', and hexdigits.
  In particular, don't accept the "0x" prefix.

This parsing would be significantly simpler to implement, if we could
just strdup() the string, split the string at the colon delimiter and
use _nm_utils_ascii_str_to_int64() which gets leading/trailing spaces
right. But let's save the "overhead" of an additional alloc.
parent fac95d00
...@@ -2279,21 +2279,42 @@ _nm_utils_string_append_tc_parent (GString *string, const char *prefix, guint32 ...@@ -2279,21 +2279,42 @@ _nm_utils_string_append_tc_parent (GString *string, const char *prefix, guint32
guint32 guint32
_nm_utils_parse_tc_handle (const char *str, GError **error) _nm_utils_parse_tc_handle (const char *str, GError **error)
{ {
gint64 maj, min; gint64 maj;
char *sep; gint64 min = 0;
const char *sep;
maj = g_ascii_strtoll (str, &sep, 0x10); nm_assert (str);
if (*sep == ':')
min = g_ascii_strtoll (&sep[1], &sep, 0x10); maj = g_ascii_strtoll (str, (char **) &sep, 0x10);
else if (sep == str)
min = 0; goto fail;
sep = nm_str_skip_leading_spaces (sep);
if (*sep != '\0' || maj <= 0 || maj > 0xffff || min < 0 || min > 0xffff) { if (sep[0] == ':') {
g_set_error (error, 1, 0, _("'%s' is not a valid handle."), str); const char *str2 = &sep[1];
return TC_H_UNSPEC;
min = g_ascii_strtoll (str2, (char **) &sep, 0x10);
sep = nm_str_skip_leading_spaces (sep);
if (sep[0] != '\0')
goto fail;
} else if (sep[0] != '\0')
goto fail;
if ( maj <= 0
|| maj > 0xffff
|| min < 0
|| min > 0xffff
|| !NM_STRCHAR_ALL (str, ch, ( g_ascii_isxdigit (ch)
|| ch == ':'
|| g_ascii_isspace (ch)))) {
goto fail;
} }
return TC_H_MAKE (maj << 16, min); return TC_H_MAKE (((guint32) maj) << 16, (guint32) min);
fail:
nm_utils_error_set (error, NM_UTILS_ERROR_UNKNOWN, _("'%s' is not a valid handle."), str);
return TC_H_UNSPEC;
} }
#define TC_ATTR_SPEC_PTR(name, type, no_value, consumes_rest, str_type) \ #define TC_ATTR_SPEC_PTR(name, type, no_value, consumes_rest, str_type) \
......
...@@ -3117,27 +3117,27 @@ test_parse_tc_handle (void) ...@@ -3117,27 +3117,27 @@ test_parse_tc_handle (void)
_parse_tc_handle_inval (" "); _parse_tc_handle_inval (" ");
_parse_tc_handle_inval (" \n"); _parse_tc_handle_inval (" \n");
_parse_tc_handle_valid ("1", 1, 0); _parse_tc_handle_valid ("1", 1, 0);
_parse_tc_handle_inval(" 1 "); _parse_tc_handle_valid(" 1 ", 1, 0);
_parse_tc_handle_valid ("1:", 1, 0); _parse_tc_handle_valid ("1:", 1, 0);
_parse_tc_handle_inval ("1: "); _parse_tc_handle_valid ("1: ", 1, 0);
_parse_tc_handle_valid ("1:0", 1, 0); _parse_tc_handle_valid ("1:0", 1, 0);
_parse_tc_handle_inval ("1 :0"); _parse_tc_handle_valid ("1 :0", 1, 0);
_parse_tc_handle_inval ("1 \t\n\f\r:0"); _parse_tc_handle_valid ("1 \t\n\f\r:0", 1, 0);
_parse_tc_handle_inval ("1 \t\n\f\r\v:0"); _parse_tc_handle_inval ("1 \t\n\f\r\v:0");
_parse_tc_handle_inval (" 1 : 0 "); _parse_tc_handle_valid (" 1 : 0 ", 1, 0);
_parse_tc_handle_valid (" \t\v\n1: 0", 1, 0); _parse_tc_handle_inval (" \t\v\n1: 0");
_parse_tc_handle_valid ("1:2", 1, 2); _parse_tc_handle_valid ("1:2", 1, 2);
_parse_tc_handle_valid ("01:02", 1, 2); _parse_tc_handle_valid ("01:02", 1, 2);
_parse_tc_handle_valid ("0x01:0x02", 1, 2); _parse_tc_handle_inval ("0x01:0x02");
_parse_tc_handle_valid (" 01: 02", 1, 2); _parse_tc_handle_valid (" 01: 02", 1, 2);
_parse_tc_handle_valid ("019: 020", 0x19, 0x20); _parse_tc_handle_valid ("019: 020", 0x19, 0x20);
_parse_tc_handle_valid ("FFFF: 020", 0xFFFF, 0x20); _parse_tc_handle_valid ("FFFF: 020", 0xFFFF, 0x20);
_parse_tc_handle_valid ("FfFF: ffff", 0xFFFF, 0xFFFF); _parse_tc_handle_valid ("FfFF: ffff", 0xFFFF, 0xFFFF);
_parse_tc_handle_valid ("FFFF", 0xFFFF, 0); _parse_tc_handle_valid ("FFFF", 0xFFFF, 0);
_parse_tc_handle_valid ("0xFFFF", 0xFFFF, 0); _parse_tc_handle_inval ("0xFFFF");
_parse_tc_handle_inval ("10000"); _parse_tc_handle_inval ("10000");
_parse_tc_handle_valid ("\t\n\f\r FFFF", 0xFFFF, 0); _parse_tc_handle_valid ("\t\n\f\r FFFF", 0xFFFF, 0);
_parse_tc_handle_valid ("\t\n\f\r \vFFFF", 0xFFFF, 0); _parse_tc_handle_inval ("\t\n\f\r \vFFFF");
} }
/*****************************************************************************/ /*****************************************************************************/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment