Browse code

pcre: rewrote subsig parsing to handle '/' within expression pcre: fixed invalid read issues

Kevin Lin authored on 2014/09/18 05:37:14
Showing 2 changed files
... ...
@@ -189,6 +189,9 @@ int cli_pcre_addpatt(struct cli_matcher *root, const char *trigger, const char *
189 189
         cli_errmsg("cli_pcre_addpatt: trigger or pattern cannot be an empty string\n");
190 190
         return CL_EMALFDB;
191 191
     }
192
+    if (cflags && *cflags == '\0') {
193
+        cflags = NULL;
194
+    }
192 195
 
193 196
     if (lsigid)
194 197
         pm_dbgmsg("cli_pcre_addpatt: Adding /%s/%s%s triggered on (%s) as subsig %d for lsigid %d\n",
... ...
@@ -166,24 +166,43 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
166 166
     if (strchr(hexsig, '/')) {
167 167
 #if HAVE_PCRE
168 168
         /* expected format => ^offset:trigger/regex/[cflags]$ */
169
-	const char *stokens[PCRE_TOKENS];
170
-	size_t stoken_count;
169
+	const char *trigger, *pattern, *cflags;
170
+        char *start, *end;
171
+
172
+        /* get checked */
173
+        if (hexsig[0] == '/') {
174
+            cli_errmsg("cli_parseadd(): PCRE subsig must contain logical trigger\n");
175
+            return CL_EMALFDB;
176
+        }
171 177
 
172 178
         /* get copied */
173
-        hexcpy = cli_calloc(hexlen, sizeof(char));
179
+        hexcpy = cli_calloc(hexlen+1, sizeof(char));
174 180
         if(!hexcpy)
175 181
             return CL_EMEM;
176 182
         strncpy(hexcpy, hexsig, hexlen);
177 183
 
178
-        /* get tokened */
179
-        stoken_count = cli_strtokenize(hexcpy, '/', PCRE_TOKENS, stokens);
180
-        if (stoken_count != 2 && stoken_count != 3) {
181
-            cli_errmsg("cli_parseadd(): invalid number of tokens for pcre subsig: %d\n", stoken_count);
184
+        /* get delimiters-ed */
185
+        start = strchr(hexcpy, '/');
186
+        end = strrchr(hexcpy, '/');
187
+        if (start == end) {
188
+            cli_errmsg("cli_parseadd(): PCRE expression must be delimited by '/'\n");
189
+            free(hexcpy);
182 190
             return CL_EMALFDB;
183 191
         }
184 192
 
193
+        /* get NULL-ed */
194
+        *start = '\0';
195
+        *end = '\0';
196
+
197
+        /* get tokens-ed */
198
+        trigger = hexcpy;
199
+        pattern = start+1;
200
+        cflags = end+1;
201
+        if (*cflags == '\0') /* get compat-ed */
202
+            cflags = NULL;
203
+
185 204
         /* normal trigger, get added */
186
-        ret = cli_pcre_addpatt(root, stokens[0], stokens[1], stokens[2], offset, lsigid, options);
205
+        ret = cli_pcre_addpatt(root, trigger, pattern, cflags, offset, lsigid, options);
187 206
         free(hexcpy);
188 207
         return ret;
189 208
 #else