Skip to content

Commit 931acbf

Browse files
committed
PCRE2 and PCRE binary compatibility.
With this change, dynamic modules using nginx regex interface can be used regardless of the variant of the PCRE library nginx was compiled with. If a module is compiled with different PCRE library variant, in case of ngx_regex_exec() errors it will report wrong function name in error messages. This is believed to be tolerable, given that fixing this will require interface changes.
1 parent c6fec0b commit 931acbf

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/core/ngx_regex.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ngx_regex_compile(ngx_regex_compile_t *rc)
118118
char *p;
119119
u_char errstr[128];
120120
size_t erroff;
121+
uint32_t options;
121122
pcre2_code *re;
122123
ngx_regex_elt_t *elt;
123124
pcre2_general_context *gctx;
@@ -152,11 +153,24 @@ ngx_regex_compile(ngx_regex_compile_t *rc)
152153
ngx_regex_malloc_done();
153154
}
154155

156+
options = 0;
157+
158+
if (rc->options & NGX_REGEX_CASELESS) {
159+
options |= PCRE2_CASELESS;
160+
}
161+
162+
if (rc->options & ~NGX_REGEX_CASELESS) {
163+
rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
164+
"regex \"%V\" compilation failed: invalid options",
165+
&rc->pattern)
166+
- rc->err.data;
167+
return NGX_ERROR;
168+
}
169+
155170
ngx_regex_malloc_init(rc->pool);
156171

157-
re = pcre2_compile(rc->pattern.data, rc->pattern.len,
158-
(uint32_t) rc->options, &errcode, &erroff,
159-
ngx_regex_compile_context);
172+
re = pcre2_compile(rc->pattern.data, rc->pattern.len, options,
173+
&errcode, &erroff, ngx_regex_compile_context);
160174

161175
/* ensure that there is no current pool */
162176
ngx_regex_malloc_done();
@@ -252,11 +266,26 @@ ngx_regex_compile(ngx_regex_compile_t *rc)
252266
char *p;
253267
pcre *re;
254268
const char *errstr;
269+
ngx_uint_t options;
255270
ngx_regex_elt_t *elt;
256271

272+
options = 0;
273+
274+
if (rc->options & NGX_REGEX_CASELESS) {
275+
options |= PCRE_CASELESS;
276+
}
277+
278+
if (rc->options & ~NGX_REGEX_CASELESS) {
279+
rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
280+
"regex \"%V\" compilation failed: invalid options",
281+
&rc->pattern)
282+
- rc->err.data;
283+
return NGX_ERROR;
284+
}
285+
257286
ngx_regex_malloc_init(rc->pool);
258287

259-
re = pcre_compile((const char *) rc->pattern.data, (int) rc->options,
288+
re = pcre_compile((const char *) rc->pattern.data, (int) options,
260289
&errstr, &erroff, NULL);
261290

262291
/* ensure that there is no current pool */
@@ -413,6 +442,15 @@ ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s, int *captures, ngx_uint_t size)
413442
return rc;
414443
}
415444

445+
#else
446+
447+
ngx_int_t
448+
ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s, int *captures, ngx_uint_t size)
449+
{
450+
return pcre_exec(re->code, re->extra, (const char *) s->data, s->len,
451+
0, 0, captures, size);
452+
}
453+
416454
#endif
417455

418456

src/core/ngx_regex.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <pcre2.h>
2020

2121
#define NGX_REGEX_NO_MATCHED PCRE2_ERROR_NOMATCH /* -1 */
22-
#define NGX_REGEX_CASELESS PCRE2_CASELESS
2322

2423
typedef pcre2_code ngx_regex_t;
2524

@@ -28,7 +27,6 @@ typedef pcre2_code ngx_regex_t;
2827
#include <pcre.h>
2928

3029
#define NGX_REGEX_NO_MATCHED PCRE_ERROR_NOMATCH /* -1 */
31-
#define NGX_REGEX_CASELESS PCRE_CASELESS
3230

3331
typedef struct {
3432
pcre *code;
@@ -38,10 +36,13 @@ typedef struct {
3836
#endif
3937

4038

39+
#define NGX_REGEX_CASELESS 0x00000001
40+
41+
4142
typedef struct {
4243
ngx_str_t pattern;
4344
ngx_pool_t *pool;
44-
ngx_int_t options;
45+
ngx_uint_t options;
4546

4647
ngx_regex_t *regex;
4748
int captures;
@@ -61,19 +62,13 @@ typedef struct {
6162
void ngx_regex_init(void);
6263
ngx_int_t ngx_regex_compile(ngx_regex_compile_t *rc);
6364

64-
#if (NGX_PCRE2)
65-
6665
ngx_int_t ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s, int *captures,
6766
ngx_uint_t size);
68-
#define ngx_regex_exec_n "pcre2_match()"
6967

68+
#if (NGX_PCRE2)
69+
#define ngx_regex_exec_n "pcre2_match()"
7070
#else
71-
72-
#define ngx_regex_exec(re, s, captures, size) \
73-
pcre_exec(re->code, re->extra, (const char *) (s)->data, (s)->len, 0, 0, \
74-
captures, size)
7571
#define ngx_regex_exec_n "pcre_exec()"
76-
7772
#endif
7873

7974
ngx_int_t ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy