git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] grep: plug leak of pcre chartables in PCRE2

From
René Scharfe <l.s.r@web.de>
Date
Jul 30, 2019, 16:52 UTC
Message-ID
<dff4a832-e8a8-789f-04cd-b11fd1671c52@web.de>
In-Reply-To
<CAPUEspgRUymY0Tm0zvfoSsYm0dKh8fy=fW-jN4Roid6oZVY86Q@mail.gmail.com>
Am 30.07.19 um 02:08 schrieb Carlo Arenas:
Show 32 quoted lines
> On Mon, Jul 29, 2019 at 1:35 PM René Scharfe <l.s.r@web.de> wrote:
>>
>> Am 28.07.19 um 03:41 schrieb Carlo Arenas:
>>> On Sat, Jul 27, 2019 at 4:48 PM Ævar Arnfjörð Bjarmason
>>> <avarab@gmail.com> wrote:
>>>>> +     free((void *)p->pcre_tables);
>>>>
>>>> Is the cast really needed? I'm rusty on the rules, removing it from the
>>>> pcre_free() you might have copied this from produces a warning for me,
>>>> but not for free() itself. This is on GCC 8.3.0. How about for you &
>>>> what compiler(s)?
>>>
>>> both will trigger warnings for the same reason
>>> (-Wincompatible-pointer-types-discards-qualifiers)
>>> with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
>>>
>>> gcc-9 in macOS triggers 2 "warnings"; one for discarding the const
>>> qualifier (-Wdiscarded-qualifiers)
>>> and another for mismatching parameter to free():
>>>
>>> note: expected 'void *' but argument is of type 'const uint8_t *' {aka
>>> 'const unsigned char *'}
>>
>> Right: pcre_maketables() returns a const pointer, and you have to cast
>> away this const'ness at some point if you want to free(3) that memory.
>> Returning a non-const pointer would be more fitting, but I guess the
>> idea is that users of the library are not supposed to change the
>> contents of the table.
>
> note that this pointer was generated by pcre2_maketables() instead
> which is actually "const uint8_t *", but yes these tables are meant to
> be inmutable and that is why they are "cost".

Only the const'ness matters in regards to the need for casting a pointer to feed to free(3).

Doing the cast in a library function or using an opaque pointer type instead of a fake const pointer would be nicer ways on the part of PCRE2 to keep callers from messing with the table. Forcing users to cast const away or leak is not very nice.. Nothing we can do about it, except perhaps adding a wish list item for PCRE3, I guess.

https://pcre.org/current/doc/html/pcre2_maketables.html says that pcre2_maketables() returns const unsigned char *, by the way. I don't get the PCRE2_SUFFIX business in pcre2.h ("Define macros that generate width-specific names from generic versions."), though.

Show 8 quoted lines
>> But wouldn't it be more correct to use pcre_free()?  As long as we keep
>> pcre_malloc() and pcre_free() at their default values it doesn't matter
>> in practice, but using free(3) directly is a layering violation, no?
>
> yes, but that is the only option PCRE2 gives when not using a global
> context which is what the comment in the commit refers to.
>
> FWIW pcre_free() doesn't exist anymore in PCRE2.

OK, and while https://pcre.org/original/doc/html/pcreapi.html#TOC1 says that pcre_malloc() is used by pcre_maketables() (and thus the result should be passed to pcre_free() after use), https://pcre.org/current/doc/html/pcre2_maketables.html says pcre2_maketables() uses malloc(3), so that pointer needs to go to free(3) at the end. Missed the second part in my earlier reply.

Show 8 quoted lines
>> Perhaps just UNLEAK that thing?  There is only a single way to build it
>> and we can reuse it throughout the lifetime of the program, so there is
>> no real need to clean it up before the OS does.
>
> That would be a better fit if it would be created once in cmd_grep and
> then shared with all worker threads (which I thought would be nice to
> do in the future anyway), but this change was trying to be conservative
> and just to the minimum to close the leak.
Sure.  I wonder how sharing between threads would influence performance..
René
Previous: Carlo ArenasNext: Carlo Marcelo Arenas Belón
Message 11 of 15 in “grep: memory leak in PCRE2”
  1. 0/3 grep: memory leak in PCRE2Carlo Marcelo Arenas Belón, Jul 27, 2019
  2. 1/3 grep: make pcre1_tables version agnosticCarlo Marcelo Arenas Belón, Jul 27, 2019
  3. Ævar Arnfjörð BjarmasonJul 27, 2019
  4. Carlo ArenasJul 28, 2019
  5. 2/3 grep: use pcre_tables also for PCRE2Carlo Marcelo Arenas Belón, Jul 27, 2019
  6. 3/3 grep: plug leak of pcre chartables in PCRE2Carlo Marcelo Arenas Belón, Jul 27, 2019
  7. Ævar Arnfjörð BjarmasonJul 27, 2019
  8. Carlo ArenasJul 28, 2019
  9. René ScharfeJul 29, 2019
  10. Carlo ArenasJul 30, 2019
  11. René ScharfeJul 30, 2019
  12. grep: avoid leak of chartables in PCRE2Carlo Marcelo Arenas Belón, Aug 1, 2019
  13. Junio C HamanoAug 2, 2019
  14. Carlo ArenasAug 3, 2019
  15. Junio C HamanoAug 5, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.