Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a problem with PCRE2 and nedmalloc, found via Azure Pipelines #306

Conversation

dscho
Copy link
Member

@dscho dscho commented Aug 5, 2019

For a couple of days, maybe even a week, pu fails consistently, in the Windows job where it tests t7816. The symptom is a segmentation fault.

I finally got to diagnose this, and it looked at first as if there was yet another buffer overrun that was so small that valgrind failed to detect it (see #178). The problem is another one, though: we ask PCRE2 to allocate a table (and it uses the system allocator for that), and then try to release it using nedmalloc's free() replacement.

This is squarely a problem with cb/pcre2-chartables-leakfix in conjunction with an overridden allocator.

Junio, for your convenience, I rebased this patch directly on top of ab/pcre-v2 and pushed out the let-pcre2-respect-nedmalloc branch at https://github.com/dscho/git ready to be pulled. The rebased version is not technically a bug fix, as I do not see any way that ab/pcre-v2 uses mismatched allocators for malloc()/free(), but just in case that you wanted to have it in v2.23.0 and not on top cb/pcre2-chartables-leakfix...

Cc: Carlo Marcelo Arenas Belón carenas@gmail.com

carenas and others added 2 commits August 2, 2019 09:19
94da919 ("grep: add support for PCRE v2", 2017-06-01) introduced
a small memory leak visible with valgrind in t7813.

Complete the creation of a PCRE2 specific variable that was missing from
the original change and free the generated table just like it is done
for PCRE1.

The table cleanup use free as there is no global context defined when
it was created (pcre2_maketables is passed a NULL pointer) but if that
gets ever changed will need to be updated in tandem.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Since 7d3bf76 (grep: avoid leak of chartables in PCRE2, 2019-08-01),
we try to release the UTF-8 tables obtained via `pcre2_maketables()`. To
do that, we use the function `free()`. That is all fine and dandy as
long as that refers to the system allocator.

However, when we compile Git with `USE_NED_ALLOCATOR` (notably on
Windows), then `free()` actually calls `nedfree()`. But
`pcre2_maketables()` allocated the tables using the system allocator
because we did not tell it to use nedmalloc instead.

This leads to segmentation faults when the UTF-8 tables are released,
most notably in the `t7816-grep-binary-pattern.sh` test script.

PCRE2 does have an option to override the allocator it should use, and
this patch calls upon it.

As there are other ways to override the system allocator than
`USE_NED_ALLOCATOR`, we choose to specify the allocator we want to use
specifically, even if we still use the system allocator.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
@dscho
Copy link
Member Author

dscho commented Aug 5, 2019

/submit

@gitgitgadget
Copy link

gitgitgadget bot commented Aug 5, 2019

Submitted as pull.306.git.gitgitgadget@gmail.com

grep.c Show resolved Hide resolved
@dscho
Copy link
Member Author

dscho commented Oct 18, 2019

Closing in favor of #402

@dscho dscho closed this Oct 18, 2019
@dscho dscho deleted the fix-for-pcre-chartables-leakfix branch October 18, 2019 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-work These patches have pending issues that need to be resolved before submitting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants