Skip to content

Commit 09c07ac

Browse files
authored
Small improvement to combination of substitution callout + overflow (#637)
I reckon that callers are assuming that when you use the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option, it will calculate the entire memory requirement in one go. Just two calls should be sufficient (rather than needing to loop with a gradually-increasing buffer size). However, with a substitution callout this is not true. If you call once with PCRE2_SUBSTITUTE_OVERFLOW_LENGTH, the buffer length returned might still not be sufficient for the second call to succeed. This is because the callout might not be called the first time, but the second time it will be called and can affect control flow, by requiring even more buffer to be used. This occurs even if the callout is completely stateless, idempotent and well-behaved. This fix ensures that when we skip a callout (due to overflow), we still request enough buffer size for either option that the callout might return.
1 parent f15bdd3 commit 09c07ac

File tree

6 files changed

+334
-139
lines changed

6 files changed

+334
-139
lines changed

doc/html/pcre2api.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,9 +4048,18 @@ <h1>pcre2api man page</h1>
40484048
The <b>pcre2_set_substitution_callout()</b> function can be used to specify a
40494049
callout function for <b>pcre2_substitute()</b>. This information is passed in
40504050
a match context. The callout function is called after each substitution has
4051-
been processed, but it can cause the replacement not to happen. The callout
4052-
function is not called for simulated substitutions that happen as a result of
4053-
the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option.
4051+
been processed, but it can cause the replacement not to happen.
4052+
</P>
4053+
<P>
4054+
The callout function is not called for simulated substitutions that happen as a
4055+
result of the PCRE2_SUBSTITUTE_OVERFLOW_LENGTH option. In this mode, when
4056+
substitution processing exceeds the buffer space provided by the caller,
4057+
processing continues by counting code units. The simulation is unable to
4058+
populate the callout block, and so the simulation is pessimistic about the
4059+
required buffer size. Whichever is larger of accepted or rejected substitution
4060+
is reported as the required size. Therefore, the returned buffer length may be
4061+
an overestimate (without a substitution callout, it is normally an exact
4062+
measurement).
40544063
</P>
40554064
<P>
40564065
The first argument of the callout function is a pointer to a substitute callout

0 commit comments

Comments
 (0)