Ticket #3694: 3694-0002-Hex-patterns-report-errors-to-the-user.patch

File 3694-0002-Hex-patterns-report-errors-to-the-user.patch, 6.4 KB (added by mooffie, 7 years ago)
  • lib/search/hex.c

    From c2246ff478bb289674494f1ea47e15dff021e2fb Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Sun, 25 Sep 2016 14:09:43 +0300
    Subject: [PATCH 2/8] Hex patterns: report errors to the user.
    
    Instead of silently accepting invalid patterns, we notify the user of errors.
    ---
     lib/search/hex.c                          | 55 +++++++++++++++++++++++++++----
     tests/lib/search/hex_translate_to_regex.c | 42 ++++++++++++++++++-----
     2 files changed, 82 insertions(+), 15 deletions(-)
    
    diff --git a/lib/search/hex.c b/lib/search/hex.c
    index 3503ff8..a56d173 100644
    a b  
    3939 
    4040/*** file scope macro definitions ****************************************************************/ 
    4141 
     42typedef enum 
     43{ 
     44    MC_SEARCH_HEX_E_OK, 
     45    MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE 
     46} mc_search_hex_parse_error_t; 
     47 
    4248/*** file scope type declarations ****************************************************************/ 
    4349 
    4450/*** file scope variables ************************************************************************/ 
     
    4652/*** file scope functions ************************************************************************/ 
    4753 
    4854static GString * 
    49 mc_search__hex_translate_to_regex (const GString * astr) 
     55mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_error_t * error_ptr, 
     56                                   int *error_pos_ptr) 
    5057{ 
    5158    GString *buff; 
    5259    gchar *tmp_str, *tmp_str2; 
    5360    gsize tmp_str_len; 
    5461    gsize loop = 0; 
     62    mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK; 
    5563 
    5664    buff = g_string_sized_new (64); 
    5765    tmp_str = g_strndup (astr->str, astr->len); 
    mc_search__hex_translate_to_regex (const GString * astr) 
    7179    g_strchug (tmp_str);        /* trim leadind whitespaces */ 
    7280    tmp_str_len = strlen (tmp_str); 
    7381 
    74     while (loop < tmp_str_len) 
     82    while (loop < tmp_str_len && error == MC_SEARCH_HEX_E_OK) 
    7583    { 
    7684        unsigned int val; 
    7785        int ptr; 
    mc_search__hex_translate_to_regex (const GString * astr) 
    8088        if (sscanf (tmp_str + loop, "%x%n", &val, &ptr)) 
    8189        { 
    8290            if (val > 255) 
    83                 loop++; 
     91                error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE; 
    8492            else 
    8593            { 
    8694                g_string_append_printf (buff, "\\x%02X", val); 
    mc_search__hex_translate_to_regex (const GString * astr) 
    109117 
    110118    g_free (tmp_str); 
    111119 
     120    if (error != MC_SEARCH_HEX_E_OK) 
     121    { 
     122        g_string_free (buff, TRUE); 
     123        if (error_ptr != NULL) 
     124            *error_ptr = error; 
     125        if (error_pos_ptr != NULL) 
     126            *error_pos_ptr = loop; 
     127        return NULL; 
     128    } 
     129 
    112130    return buff; 
    113131} 
    114132 
    mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se 
    119137                                     mc_search_cond_t * mc_search_cond) 
    120138{ 
    121139    GString *tmp; 
     140    mc_search_hex_parse_error_t error; 
     141    int error_pos; 
    122142 
    123143    g_string_ascii_down (mc_search_cond->str); 
    124     tmp = mc_search__hex_translate_to_regex (mc_search_cond->str); 
    125     g_string_free (mc_search_cond->str, TRUE); 
    126     mc_search_cond->str = tmp; 
     144    tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos); 
     145    if (tmp != NULL) 
     146    { 
     147        g_string_free (mc_search_cond->str, TRUE); 
     148        mc_search_cond->str = tmp; 
     149        mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond); 
     150    } 
     151    else 
     152    { 
     153        const char *desc; 
     154 
     155        switch (error) 
     156        { 
     157        case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE: 
     158            desc = 
     159                _ 
     160                ("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)"); 
     161            break; 
     162        default: 
     163            desc = ""; 
     164        } 
    127165 
    128     mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond); 
     166        lc_mc_search->error = MC_SEARCH_E_INPUT; 
     167        lc_mc_search->error_str = 
     168            g_strdup_printf (_("Hex pattern error at position %d:\n%s."), error_pos + 1, desc); 
     169    } 
    129170} 
    130171 
    131172/* --------------------------------------------------------------------------------------------- */ 
  • 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 34c131c..cd53486 100644
    a b static const struct test_hex_translate_to_regex_ds 
    3434{ 
    3535    const char *input_value; 
    3636    const char *expected_result; 
     37    mc_search_hex_parse_error_t expected_error; 
    3738} test_hex_translate_to_regex_ds[] = 
    3839{ 
    3940    { 
    4041        /* Simplest case */ 
    4142        "12 34", 
    42         "\\x12\\x34" 
     43        "\\x12\\x34", 
     44        MC_SEARCH_HEX_E_OK 
    4345    }, 
    4446    { 
    4547        /* Prefixes (0x, 0X) */ 
    4648        "0x12 0X34", 
    47         "\\x12\\x34" 
     49        "\\x12\\x34", 
     50        MC_SEARCH_HEX_E_OK 
    4851    }, 
    4952    { 
    5053        /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */ 
    5154        "012", 
    52         "\\x12" 
     55        "\\x12", 
     56        MC_SEARCH_HEX_E_OK 
    5357    }, 
    5458    { 
    5559        /* Extra whitespace (but not trailing one) */ 
    5660        "  12  34", 
    57         "\\x12\\x34" 
     61        "\\x12\\x34", 
     62        MC_SEARCH_HEX_E_OK 
    5863    }, 
    5964    { 
    6065        /* Min/max values */ 
    6166        "0 ff", 
    62         "\\x00\\xFF" 
     67        "\\x00\\xFF", 
     68        MC_SEARCH_HEX_E_OK 
     69    }, 
     70    { 
     71        /* Error: Number out of range */ 
     72        "100", 
     73        NULL, 
     74        MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE 
     75    }, 
     76    { 
     77        /* Error: Number out of range (negative) */ 
     78        "-1", 
     79        NULL, 
     80        MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE 
    6381    }, 
    6482}; 
    6583/* *INDENT-ON* */ 
    START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_rege 
    7290    /* given */ 
    7391    GString *tmp = g_string_new (data->input_value); 
    7492    GString *dest_str; 
     93    mc_search_hex_parse_error_t error; 
    7594 
    7695    /* when */ 
    77     dest_str = mc_search__hex_translate_to_regex (tmp); 
     96    dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL); 
    7897 
    7998    /* then */ 
    8099    g_string_free (tmp, TRUE); 
    81100 
    82     mctest_assert_str_eq (dest_str->str, data->expected_result); 
    83     g_string_free (dest_str, TRUE); 
     101    if (dest_str != NULL) 
     102    { 
     103        mctest_assert_str_eq (dest_str->str, data->expected_result); 
     104        g_string_free (dest_str, TRUE); 
     105    } 
     106    else 
     107    { 
     108        mctest_assert_int_eq (error, data->expected_error); 
     109    } 
    84110} 
    85111/* *INDENT-OFF* */ 
    86112END_PARAMETRIZED_TEST