Ticket #3694: 3694-0004-Hex-patterns-handle-invalid-characters.patch

File 3694-0004-Hex-patterns-handle-invalid-characters.patch, 2.9 KB (added by mooffie, 2 years ago)
  • lib/search/hex.c

    From 0935dc82e9731b12e437ef2723bdca33a6146693 Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Sun, 25 Sep 2016 18:30:45 +0300
    Subject: [PATCH 4/8] Hex patterns: handle invalid characters.
    
    ---
     lib/search/hex.c                          | 17 +++++++++++++----
     tests/lib/search/hex_translate_to_regex.c |  6 ++++++
     2 files changed, 19 insertions(+), 4 deletions(-)
    
    diff --git a/lib/search/hex.c b/lib/search/hex.c
    index 524a322..9395543 100644
    a b  
    4242typedef enum 
    4343{ 
    4444    MC_SEARCH_HEX_E_OK, 
    45     MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE 
     45    MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE, 
     46    MC_SEARCH_HEX_E_INVALID_CHARACTER 
    4647} mc_search_hex_parse_error_t; 
    4748 
    4849/*** file scope type declarations ****************************************************************/ 
    mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err 
    7677        *tmp_str2++ = ' '; 
    7778    } 
    7879 
    79     g_strchug (tmp_str);        /* trim leadind whitespaces */ 
    8080    tmp_str_len = strlen (tmp_str); 
    8181 
    8282    while (loop < tmp_str_len && error == MC_SEARCH_HEX_E_OK) 
    mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err 
    8484        unsigned int val; 
    8585        int ptr; 
    8686 
     87        if (g_ascii_isspace (tmp_str[loop])) 
     88        { 
     89            /* Eat-up whitespace between tokens. */ 
     90            while (g_ascii_isspace (tmp_str[loop])) 
     91                loop++; 
     92        } 
    8793        /* cppcheck-suppress invalidscanf */ 
    88         if (sscanf (tmp_str + loop, "%x%n", &val, &ptr) == 1) 
     94        else if (sscanf (tmp_str + loop, "%x%n", &val, &ptr) == 1) 
    8995        { 
    9096            if (val > 255) 
    9197                error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE; 
    mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err 
    112118            loop += loop2; 
    113119        } 
    114120        else 
    115             loop++; 
     121            error = MC_SEARCH_HEX_E_INVALID_CHARACTER; 
    116122    } 
    117123 
    118124    g_free (tmp_str); 
    mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se 
    159165                _ 
    160166                ("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)"); 
    161167            break; 
     168        case MC_SEARCH_HEX_E_INVALID_CHARACTER: 
     169            desc = _("Invalid character"); 
     170            break; 
    162171        default: 
    163172            desc = ""; 
    164173        } 
  • tests/lib/search/hex_translate_to_regex.c

    diff --git a/tests/lib/search/hex_translate_to_regex.c b/tests/lib/search/hex_translate_to_regex.c
    index c72dff9..4b580ae 100644
    a b static const struct test_hex_translate_to_regex_ds 
    7979        NULL, 
    8080        MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE 
    8181    }, 
     82    { 
     83        /* Error: Invalid characters */ 
     84        "1 z 2", 
     85        NULL, 
     86        MC_SEARCH_HEX_E_INVALID_CHARACTER 
     87    }, 
    8288}; 
    8389/* *INDENT-ON* */ 
    8490