Skip to content

Commit 6454fac

Browse files
authored
pcre2test: dynamically allocate buffer for JITTARGET (#835)
Always ask `pcre2_config()` for the expected size of buffers and abort if it is too small. In the documentation, remove the description of a static buffer size for JITTARGET.
1 parent dd75533 commit 6454fac

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

doc/pcre2api.3

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH PCRE2API 3 "19 October 2025" "PCRE2 10.48-DEV"
1+
.TH PCRE2API 3 "29 October 2025" "PCRE2 10.48-DEV"
22
.SH NAME
33
PCRE2 - Perl-compatible regular expressions (revised API)
44
.sp
@@ -1296,14 +1296,15 @@ documentation for more details.
12961296
.sp
12971297
PCRE2_CONFIG_JITTARGET
12981298
.sp
1299-
The \fIwhere\fP argument should point to a buffer that is at least 64 code
1300-
units long. (The exact length required can be found by calling
1301-
\fBpcre2_config()\fP with \fBwhere\fP set to NULL.) The buffer is filled with a
1302-
string that contains the name of the architecture for which the JIT compiler is
1303-
configured, for example "x86 32bit (little endian + unaligned)". If JIT support
1304-
is not available, PCRE2_ERROR_BADOPTION is returned, otherwise the number of
1305-
code units used is returned. This is the length of the string, plus one unit
1306-
for the terminating zero.
1299+
The \fIwhere\fP argument should point to a buffer that is suitably aligned and
1300+
wide enough to hold the full string. The exact length required is returned when
1301+
calling \fBpcre2_config()\fP with \fBwhere\fP set to NULL first. The buffer is
1302+
filled with a string that contains the name of the architecture for which the JIT
1303+
compiler is configured at build time, for example, a 64-bit ARM CPU that supports
1304+
the Armv8.1 extension writes "ARM-64 (LSE) 64bit (little endian + unaligned)".
1305+
If JIT support is not available, PCRE2_ERROR_BADOPTION is returned, otherwise
1306+
the number of code units used is returned. This is the length of the string,
1307+
plus one code unit for the NUL character.
13071308
.sp
13081309
PCRE2_CONFIG_LINKSIZE
13091310
.sp
@@ -4606,6 +4607,6 @@ Cambridge, England.
46064607
.rs
46074608
.sp
46084609
.nf
4609-
Last updated: 19 October 2025
4610+
Last updated: 29 October 2025
46104611
Copyright (c) 1997-2024 University of Cambridge.
46114612
.fi

src/pcre2test.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,9 +2942,9 @@ static int pcre2_config(uint32_t what, void *where)
29422942
DISPATCH(return, pcre2_config_, (what, where));
29432943
}
29442944

2945-
static void config_str(uint32_t what, char *where)
2945+
static char *config_str(uint32_t what, char *where, int size)
29462946
{
2947-
DISPATCH(, config_str_, (what, where));
2947+
DISPATCH(return, config_str_, (what, where, size));
29482948
}
29492949

29502950
static BOOL decode_modifiers(uint8_t *p, int ctx, patctl *pctl, datctl *dctl)
@@ -3014,7 +3014,7 @@ static void
30143014
print_version(FILE *f, BOOL include_mode)
30153015
{
30163016
char buf[VERSION_SIZE];
3017-
config_str(PCRE2_CONFIG_VERSION, buf);
3017+
config_str(PCRE2_CONFIG_VERSION, buf, sizeof(buf));
30183018
fprintf(f, "PCRE2 version %s", buf);
30193019
if (include_mode)
30203020
{
@@ -3033,7 +3033,7 @@ static void
30333033
print_unicode_version(FILE *f)
30343034
{
30353035
char buf[VERSION_SIZE];
3036-
config_str(PCRE2_CONFIG_UNICODE_VERSION, buf);
3036+
config_str(PCRE2_CONFIG_UNICODE_VERSION, buf, sizeof(buf));
30373037
fprintf(f, "Unicode version %s", buf);
30383038
}
30393039

@@ -3046,9 +3046,9 @@ fprintf(f, "Unicode version %s", buf);
30463046
static void
30473047
print_jit_target(FILE *f)
30483048
{
3049-
char buf[VERSION_SIZE];
3050-
config_str(PCRE2_CONFIG_JITTARGET, buf);
3049+
char *buf = config_str(PCRE2_CONFIG_JITTARGET, NULL, 0);
30513050
fputs(buf, f);
3051+
free(buf);
30523052
}
30533053

30543054

src/pcre2test_inc.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -558,24 +558,41 @@ return 0;
558558
559559
Arguments:
560560
what the item to read
561-
where the 8-bit buffer to receive the string
561+
where the 8-bit buffer to receive the string (NULLABLE)
562+
size sizeof(where) or 0 to ask for the buffer to be allocated
563+
564+
Returns: the string where the data was written
562565
*/
563566

564-
static void
565-
config_str(uint32_t what, char *where)
567+
static char *
568+
config_str(uint32_t what, char *where, int size)
566569
{
567-
int r1, r2;
568-
PCRE2_UCHAR buf[VERSION_SIZE];
570+
int r2;
571+
PCRE2_UCHAR *buf;
572+
int needed_len;
569573

570-
r1 = pcre2_config(what, NULL);
571-
r2 = pcre2_config(what, buf);
572-
if (r1 < 0 || r1 != r2 || r1 >= VERSION_SIZE)
574+
needed_len = pcre2_config(what, NULL);
575+
if (needed_len <= 0)
573576
{
574577
cfprintf(clr_test_error, stderr, "pcre2test: Error in pcre2_config(%d)\n", what);
575578
exit(1);
576579
}
580+
else if (size != 0 && needed_len > size)
581+
{
582+
cfprintf(clr_test_error, stderr,
583+
"pcre2test: Static buffer provided to pcre2_config(%d) too small\n", what);
584+
exit(1);
585+
}
586+
587+
buf = malloc(needed_len * sizeof(PCRE2_UCHAR));
588+
r2 = pcre2_config(what, buf);
589+
PCRE2_ASSERT(r2 == needed_len);
590+
591+
if (where == NULL) where = malloc(needed_len);
592+
while (r2-- > 0) where[r2] = (char)buf[r2];
593+
free(buf);
577594

578-
while (r1-- > 0) where[r1] = (char)buf[r1];
595+
return where;
579596
}
580597

581598

0 commit comments

Comments
 (0)