Skip to content

Commit e669b61

Browse files
carenasNWilson
authored andcommitted
pcre2test: memory reports only compiled memory usage for code/data (#418)
Cherry-pick of 5790662. Since 05aafb2 (Implement pcre2_set_max_pattern_compiled_length() and set this limit in the fuzzer, 2024-04-24), the memory modifier has reported the full size of the allocated "code" returned by `pcre2_compile`. Problem is that the size of the structure used to hold that in memory also depends on the platform ABI and even alignment by the compiler, and has been therefore fragile to compare. Revert to reporting only the additional memory that `pcre2_compile()` will use for the compiled pattern (including any data tables) and make sure that the limit provided with `pcre2_set_max_pattern_compiled_length()` also avoid the internal struct overhead. Fixes: #415
1 parent 6ae58be commit e669b61

12 files changed

+546
-1022
lines changed

doc/pcre2_set_max_pattern_compiled_length.3

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH PCRE2_SET_MAX_PATTERN_COMPILED_LENGTH 3 "24 April 2024" "PCRE2 10.44"
1+
.TH PCRE2_SET_MAX_PATTERN_COMPILED_LENGTH 3 "8 Jun 2024" "PCRE2 10.45"
22
.SH NAME
33
PCRE2 - Perl-compatible regular expressions (revised API)
44
.SH SYNOPSIS
@@ -15,9 +15,9 @@ PCRE2 - Perl-compatible regular expressions (revised API)
1515
.rs
1616
.sp
1717
This function sets, in a compile context, the maximum size (in bytes) for the
18-
memory needed to hold the compiled version of a pattern that is compiled with
19-
this context. The result is always zero. If a pattern that is passed to
20-
\fBpcre2_compile()\fP with this context needs more memory, an error is
18+
memory needed to hold the compiled version of a pattern that is using this
19+
context. The result is always zero. If a pattern that is passed to
20+
\fBpcre2_compile()\fP referencing this context needs more memory, an error is
2121
generated. The default is the largest number that a PCRE2_SIZE variable can
2222
hold, which is effectively unlimited.
2323
.P

src/pcre2_compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10608,8 +10608,7 @@ block for storing the compiled pattern and names table. Integer overflow should
1060810608
no longer be possible because nowadays we limit the maximum value of
1060910609
cb.names_found and cb.name_entry_size. */
1061010610

10611-
re_blocksize = sizeof(pcre2_real_code) +
10612-
CU2BYTES(length +
10611+
re_blocksize = CU2BYTES(length +
1061310612
(PCRE2_SIZE)cb.names_found * (PCRE2_SIZE)cb.name_entry_size);
1061410613

1061510614
if (re_blocksize > ccontext->max_pattern_compiled_length)
@@ -10618,6 +10617,7 @@ if (re_blocksize > ccontext->max_pattern_compiled_length)
1061810617
goto HAD_CB_ERROR;
1061910618
}
1062010619

10620+
re_blocksize += sizeof(pcre2_real_code);
1062110621
re = (pcre2_real_code *)
1062210622
ccontext->memctl.malloc(re_blocksize, ccontext->memctl.memory_data);
1062310623
if (re == NULL)

src/pcre2test.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4397,7 +4397,7 @@ static void
43974397
show_memory_info(void)
43984398
{
43994399
uint32_t name_count, name_entry_size;
4400-
PCRE2_SIZE size, cblock_size;
4400+
PCRE2_SIZE size, cblock_size, data_size;
44014401

44024402
/* One of the test_mode values will always be true, but to stop a compiler
44034403
warning we must initialize cblock_size. */
@@ -4417,18 +4417,19 @@ if (test_mode == PCRE32_MODE) cblock_size = sizeof(pcre2_real_code_32);
44174417
(void)pattern_info(PCRE2_INFO_NAMECOUNT, &name_count, FALSE);
44184418
(void)pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &name_entry_size, FALSE);
44194419

4420-
/* The uint32_t variables are cast before multiplying to stop code analyzers
4421-
grumbling about potential overflow. */
4420+
/* The uint32_t variables are cast before multiplying to avoid potential
4421+
integer overflow. */
4422+
data_size = (PCRE2_SIZE)name_count * (PCRE2_SIZE)name_entry_size * (PCRE2_SIZE)code_unit_size;
44224423

4423-
fprintf(outfile, "Memory allocation - compiled block : %" SIZ_FORM "\n", size);
4424-
fprintf(outfile, "Memory allocation - code portion : %" SIZ_FORM "\n", size -
4425-
(PCRE2_SIZE)name_count * (PCRE2_SIZE)name_entry_size * (PCRE2_SIZE)code_unit_size -
4426-
cblock_size);
4424+
fprintf(outfile, "Memory allocation - code size : %" SIZ_FORM "\n", size -
4425+
cblock_size - data_size);
4426+
if (data_size != 0)
4427+
fprintf(outfile, "Memory allocation - data size : %" SIZ_FORM "\n", data_size);
44274428

44284429
if (pat_patctl.jit != 0)
44294430
{
44304431
(void)pattern_info(PCRE2_INFO_JITSIZE, &size, FALSE);
4431-
fprintf(outfile, "Memory allocation - JIT code : %" SIZ_FORM "\n", size);
4432+
fprintf(outfile, "Memory allocation - JIT code : %" SIZ_FORM "\n", size);
44324433
}
44334434
}
44344435

0 commit comments

Comments
 (0)