Handle invalid characters.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Mooffie 2016-09-25 18:30:45 +03:00 committed by Andrew Borodin
parent cc8fcdcfc0
commit 64e6ccef7f
2 changed files with 19 additions and 4 deletions

View File

@ -42,7 +42,8 @@
typedef enum
{
MC_SEARCH_HEX_E_OK,
MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
MC_SEARCH_HEX_E_INVALID_CHARACTER
} mc_search_hex_parse_error_t;
/*** file scope type declarations ****************************************************************/
@ -76,7 +77,6 @@ mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err
*tmp_str2++ = ' ';
}
g_strchug (tmp_str); /* trim leadind whitespaces */
tmp_str_len = strlen (tmp_str);
while (loop < tmp_str_len && error == MC_SEARCH_HEX_E_OK)
@ -84,8 +84,14 @@ mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err
unsigned int val;
int ptr;
if (g_ascii_isspace (tmp_str[loop]))
{
/* Eat-up whitespace between tokens. */
while (g_ascii_isspace (tmp_str[loop]))
loop++;
}
/* cppcheck-suppress invalidscanf */
if (sscanf (tmp_str + loop, "%x%n", &val, &ptr) == 1)
else if (sscanf (tmp_str + loop, "%x%n", &val, &ptr) == 1)
{
if (val > 255)
error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
@ -112,7 +118,7 @@ mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err
loop += loop2;
}
else
loop++;
error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
}
g_free (tmp_str);
@ -159,6 +165,9 @@ mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se
_
("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
break;
case MC_SEARCH_HEX_E_INVALID_CHARACTER:
desc = _("Invalid character");
break;
default:
desc = "";
}

View File

@ -79,6 +79,12 @@ static const struct test_hex_translate_to_regex_ds
NULL,
MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
},
{
/* Error: Invalid characters */
"1 z 2",
NULL,
MC_SEARCH_HEX_E_INVALID_CHARACTER
},
};
/* *INDENT-ON* */