-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3694-0005-Hex-patterns-fix-quotes-handling.patch
158 lines (149 loc) · 4.66 KB
/
3694-0005-Hex-patterns-fix-quotes-handling.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
From 7de6cb1aaf35008c606c27304ec446498e8447c2 Mon Sep 17 00:00:00 2001
From: Mooffie <mooffie@gmail.com>
Date: Sun, 25 Sep 2016 20:57:12 +0300
Subject: [PATCH 5/8] Hex patterns: fix quotes handling.
Note: considering that this feature hasn't worked, we may consider removing it
entirely or partially (e.g., escaping) in order to simplify the code, as nobody
has grown used to it. It seems, based on the "hex mode" mentioned in the manual
page, that in the past there was no "normal" search in hex mode, and quoted
strings were the only easy way to look for text. This is no longer the case
nowadays.
Note: the characters in the quoted string are copied out as-is to the regexp.
No regexp-quoting is currently done. We may want to revisit this issue when we
work on ticket #3695.
---
lib/search/hex.c | 28 +++++++++------
tests/lib/search/hex_translate_to_regex.c | 57 +++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 10 deletions(-)
diff --git a/lib/search/hex.c b/lib/search/hex.c
index 9395543..1180016 100644
--- a/lib/search/hex.c
+++ b/lib/search/hex.c
@@ -43,7 +43,8 @@ typedef enum
{
MC_SEARCH_HEX_E_OK,
MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
- MC_SEARCH_HEX_E_INVALID_CHARACTER
+ MC_SEARCH_HEX_E_INVALID_CHARACTER,
+ MC_SEARCH_HEX_E_UNMATCHED_QUOTES
} mc_search_hex_parse_error_t;
/*** file scope type declarations ****************************************************************/
@@ -101,21 +102,26 @@ mc_search__hex_translate_to_regex (const GString * astr, mc_search_hex_parse_err
loop += ptr;
}
}
- else if (*(tmp_str + loop) == '"')
+ else if (tmp_str[loop] == '"')
{
- gsize loop2 = 0;
+ gsize loop2;
- loop++;
- while (loop + loop2 < tmp_str_len)
+ loop2 = loop + 1;
+
+ while (loop2 < tmp_str_len)
{
- if (*(tmp_str + loop + loop2) == '"' &&
- !strutils_is_char_escaped (tmp_str, tmp_str + loop + loop2))
+ if (tmp_str[loop2] == '"')
break;
+ if (tmp_str[loop2] == '\\' && loop2 + 1 < tmp_str_len)
+ loop2++;
+ g_string_append_c (buff, tmp_str[loop2]);
loop2++;
}
- g_string_append_len (buff, tmp_str + loop, loop2 - 1);
- loop += loop2;
+ if (tmp_str[loop2] == '\0')
+ error = MC_SEARCH_HEX_E_UNMATCHED_QUOTES;
+ else
+ loop = loop2 + 1;
}
else
error = MC_SEARCH_HEX_E_INVALID_CHARACTER;
@@ -146,7 +152,6 @@ mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se
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, &error, &error_pos);
if (tmp != NULL)
{
@@ -168,6 +173,9 @@ mc_search__cond_struct_new_init_hex (const char *charset, mc_search_t * lc_mc_se
case MC_SEARCH_HEX_E_INVALID_CHARACTER:
desc = _("Invalid character");
break;
+ case MC_SEARCH_HEX_E_UNMATCHED_QUOTES:
+ desc = _("Unmatched quotes character");
+ break;
default:
desc = "";
}
diff --git a/tests/lib/search/hex_translate_to_regex.c b/tests/lib/search/hex_translate_to_regex.c
index 4b580ae..258dc25 100644
--- a/tests/lib/search/hex_translate_to_regex.c
+++ b/tests/lib/search/hex_translate_to_regex.c
@@ -85,6 +85,63 @@ static const struct test_hex_translate_to_regex_ds
NULL,
MC_SEARCH_HEX_E_INVALID_CHARACTER
},
+ /*
+ * Quotes.
+ */
+ {
+ " \"abc\" ",
+ "abc",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ /* Preserve upper/lower case */
+ "\"aBc\"",
+ "aBc",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ " 12\"abc\"34 ",
+ "\\x12abc\\x34",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ "\"a\"\"b\"",
+ "ab",
+ MC_SEARCH_HEX_E_OK
+ },
+ /* Empty quotes */
+ {
+ "\"\"",
+ "",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ "12 \"\"",
+ "\\x12",
+ MC_SEARCH_HEX_E_OK
+ },
+ /* Error: Unmatched quotes */
+ {
+ "\"a",
+ NULL,
+ MC_SEARCH_HEX_E_UNMATCHED_QUOTES
+ },
+ {
+ "\"",
+ NULL,
+ MC_SEARCH_HEX_E_UNMATCHED_QUOTES
+ },
+ /* Escaped quotes */
+ {
+ "\"a\\\"b\"",
+ "a\"b",
+ MC_SEARCH_HEX_E_OK
+ },
+ {
+ "\"a\\\\b\"",
+ "a\\b",
+ MC_SEARCH_HEX_E_OK
+ },
};
/* *INDENT-ON* */
--
2.9.3