-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3694-0002-Hex-patterns-report-errors-to-the-user.patch
210 lines (192 loc) · 6.38 KB
/
3694-0002-Hex-patterns-report-errors-to-the-user.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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/lib/search/hex.c
+++ b/lib/search/hex.c
@@ -39,6 +39,12 @@
/*** file scope macro definitions ****************************************************************/
+typedef enum
+{
+ MC_SEARCH_HEX_E_OK,
+ MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
+} mc_search_hex_parse_error_t;
+
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
@@ -46,12 +52,14 @@
/*** file scope functions ************************************************************************/
static GString *
-mc_search__hex_translate_to_regex (const GString * astr)
+mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_error_t * error_ptr,
+ int *error_pos_ptr)
{
GString *buff;
gchar *tmp_str, *tmp_str2;
gsize tmp_str_len;
gsize loop = 0;
+ mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
buff = g_string_sized_new (64);
tmp_str = g_strndup (astr->str, astr->len);
@@ -71,7 +79,7 @@ mc_search__hex_translate_to_regex (const GString * astr)
g_strchug (tmp_str); /* trim leadind whitespaces */
tmp_str_len = strlen (tmp_str);
- while (loop < tmp_str_len)
+ while (loop < tmp_str_len && error == MC_SEARCH_HEX_E_OK)
{
unsigned int val;
int ptr;
@@ -80,7 +88,7 @@ mc_search__hex_translate_to_regex (const GString * astr)
if (sscanf (tmp_str + loop, "%x%n", &val, &ptr))
{
if (val > 255)
- loop++;
+ error = MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE;
else
{
g_string_append_printf (buff, "\\x%02X", val);
@@ -109,6 +117,16 @@ mc_search__hex_translate_to_regex (const GString * astr)
g_free (tmp_str);
+ if (error != MC_SEARCH_HEX_E_OK)
+ {
+ g_string_free (buff, TRUE);
+ if (error_ptr != NULL)
+ *error_ptr = error;
+ if (error_pos_ptr != NULL)
+ *error_pos_ptr = loop;
+ return NULL;
+ }
+
return buff;
}
@@ -119,13 +137,36 @@ mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se
mc_search_cond_t * mc_search_cond)
{
GString *tmp;
+ mc_search_hex_parse_error_t error;
+ int error_pos;
g_string_ascii_down (mc_search_cond->str);
- tmp = mc_search__hex_translate_to_regex (mc_search_cond->str);
- g_string_free (mc_search_cond->str, TRUE);
- mc_search_cond->str = tmp;
+ tmp = mc_search__hex_translate_to_regex (mc_search_cond->str, &error, &error_pos);
+ if (tmp != NULL)
+ {
+ g_string_free (mc_search_cond->str, TRUE);
+ mc_search_cond->str = tmp;
+ mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
+ }
+ else
+ {
+ const char *desc;
+
+ switch (error)
+ {
+ case MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE:
+ desc =
+ _
+ ("Number out of range (should be in byte range, 0 <= n <= 0xFF, expressed in hex)");
+ break;
+ default:
+ desc = "";
+ }
- mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
+ lc_mc_search->error = MC_SEARCH_E_INPUT;
+ lc_mc_search->error_str =
+ g_strdup_printf (_("Hex pattern error at position %d:\n%s."), error_pos + 1, desc);
+ }
}
/* --------------------------------------------------------------------------------------------- */
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/tests/lib/search/hex_translate_to_regex.c
+++ b/tests/lib/search/hex_translate_to_regex.c
@@ -34,32 +34,50 @@ static const struct test_hex_translate_to_regex_ds
{
const char *input_value;
const char *expected_result;
+ mc_search_hex_parse_error_t expected_error;
} test_hex_translate_to_regex_ds[] =
{
{
/* Simplest case */
"12 34",
- "\\x12\\x34"
+ "\\x12\\x34",
+ MC_SEARCH_HEX_E_OK
},
{
/* Prefixes (0x, 0X) */
"0x12 0X34",
- "\\x12\\x34"
+ "\\x12\\x34",
+ MC_SEARCH_HEX_E_OK
},
{
/* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */
"012",
- "\\x12"
+ "\\x12",
+ MC_SEARCH_HEX_E_OK
},
{
/* Extra whitespace (but not trailing one) */
" 12 34",
- "\\x12\\x34"
+ "\\x12\\x34",
+ MC_SEARCH_HEX_E_OK
},
{
/* Min/max values */
"0 ff",
- "\\x00\\xFF"
+ "\\x00\\xFF",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ /* Error: Number out of range */
+ "100",
+ NULL,
+ MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
+ },
+ {
+ /* Error: Number out of range (negative) */
+ "-1",
+ NULL,
+ MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
},
};
/* *INDENT-ON* */
@@ -72,15 +90,23 @@ START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_rege
/* given */
GString *tmp = g_string_new (data->input_value);
GString *dest_str;
+ mc_search_hex_parse_error_t error;
/* when */
- dest_str = mc_search__hex_translate_to_regex (tmp);
+ dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
/* then */
g_string_free (tmp, TRUE);
- mctest_assert_str_eq (dest_str->str, data->expected_result);
- g_string_free (dest_str, TRUE);
+ if (dest_str != NULL)
+ {
+ mctest_assert_str_eq (dest_str->str, data->expected_result);
+ g_string_free (dest_str, TRUE);
+ }
+ else
+ {
+ mctest_assert_int_eq (error, data->expected_error);
+ }
}
/* *INDENT-OFF* */
END_PARAMETRIZED_TEST
--
2.9.3