From patchwork Tue Aug 6 16:36:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= X-Patchwork-Id: 11079409 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 46E8C746 for ; Tue, 6 Aug 2019 16:37:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 36ECB20174 for ; Tue, 6 Aug 2019 16:37:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2ACBD2849D; Tue, 6 Aug 2019 16:37:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2FDA20174 for ; Tue, 6 Aug 2019 16:37:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387841AbfHFQhD (ORCPT ); Tue, 6 Aug 2019 12:37:03 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34186 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726877AbfHFQhB (ORCPT ); Tue, 6 Aug 2019 12:37:01 -0400 Received: by mail-pf1-f193.google.com with SMTP id b13so41795889pfo.1 for ; Tue, 06 Aug 2019 09:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NWDwMajnzuNnNydXZ2TpOxl7woySDxL4YmO8kuthpR0=; b=H/KURsP343PRfEOrVk1bnjmeQBpz4C3UyucwXIAZg/GLsUB/+Am+Ldr+GCCGpinRG0 tHGAVcYccT18CBeEDQYZAZoZqE0HHhwj+7l+7TXSEt+yNFSmCO+no8wy736452y7ImxD 2yIzbLbZ4x0qU7NATW0yfbyVUybZWBqHk44m0eu2LWb5XjIKw7hmuSQsTOgGTQjKwMqK 8JB/4ry3hQRg6JjSCkAjf/AXEsJyEMJTUNePx8hKp2phxW6Yf/gANauN4hLPl5lLQkLT czXMx2cbbBy4mKK8QJE36Skjk8vnvZZlpatdZh1pH5LFT6GTm2buTRxhIlPfJoOCMnkq PaQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NWDwMajnzuNnNydXZ2TpOxl7woySDxL4YmO8kuthpR0=; b=SlzYbK6QmTcbcxuY9XBXC/S9PqK7BW1T48xWv5ZBGAcrO78J/AN2DPu3w22M0lAiaj tExXCzuknTDcWlsFziXMYYDgYOH1QqAJfdE9mP8rTb6+iGSnTcZ+kQkjoieqszCo0Xso 5juBXxibqxNukAWUVpJN5JMaYjr6CwkOo53iMPi7UvJw8p4j2X4jty5FkXdvpqHjkJLY ka6Mi+csq39nPTPRyfaImJpO0y8Pf/rtbheN6lbEQDGkZ2+le3/ENvNHR/XgKGhBo3aD Pmd5ADTzpFe9zzW4NNOZryDfKmd78RxVH0aFMkuW//AWczoSqUFIjXZfDZvKkmPgmOWY 3KmA== X-Gm-Message-State: APjAAAURC2grLd4CcFvoB7xeiT+af6ewd/rtM+D9+job8iLf9bhGQW5P O7tjE+MSpHlcEJTipqKKgoujhsmd/Ac= X-Google-Smtp-Source: APXvYqzvD1WBFVme3w80a4GZPSM0RDZjHmDekSZwDQeYZKwjw96d4mgEfp7Q7ZF6vujOWueBCYO1lQ== X-Received: by 2002:a17:90a:21ac:: with SMTP id q41mr4193358pjc.31.1565109420844; Tue, 06 Aug 2019 09:37:00 -0700 (PDT) Received: from localhost.localdomain (c-67-188-192-166.hsd1.ca.comcast.net. [67.188.192.166]) by smtp.gmail.com with ESMTPSA id k36sm89364611pgl.42.2019.08.06.09.37.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Aug 2019 09:37:00 -0700 (PDT) From: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= To: git@vger.kernel.org Cc: gitster@pobox.com, johannes.schindelin@gmx.de, l.s.r@web.de, avarab@gmail.com, michal.kiedrowicz@gmail.com Subject: [RFC PATCH v3 1/3] grep: make PCRE1 aware of custom allocator Date: Tue, 6 Aug 2019 09:36:56 -0700 Message-Id: <20190806163658.66932-2-carenas@gmail.com> X-Mailer: git-send-email 2.23.0.rc1 In-Reply-To: <20190806163658.66932-1-carenas@gmail.com> References: <20190806085014.47776-1-carenas@gmail.com> <20190806163658.66932-1-carenas@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 63e7e9d8b6 ("git-grep: Learn PCRE", 2011-05-09) didn't include a way to override the system alocator, and so it is incompatible with USE_NED_ALLOCATOR as reported by Dscho[1] (in similar code from PCRE2) Make the minimum change possible to ensure this combination is supported by extending grep_init to set the PCRE1 specific functions to the NED versions (using the aliased names though) and therefore making sure all alocations are done inside PCRE1 with the same allocator than the rest of git. This change might have performance impact (hopefully for the best) and so will be good idea to test it in a platform where NED might have a positive impact (ex: Windows 7) [1] https://public-inbox.org/git/pull.306.git.gitgitgadget@gmail.com Signed-off-by: Carlo Marcelo Arenas Belón --- Makefile | 2 +- grep.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bd246f2989..4b384f3759 100644 --- a/Makefile +++ b/Makefile @@ -1764,7 +1764,7 @@ ifdef NATIVE_CRLF endif ifdef USE_NED_ALLOCATOR - COMPAT_CFLAGS += -Icompat/nedmalloc + COMPAT_CFLAGS += -DUSE_NED_ALLOCATOR -Icompat/nedmalloc COMPAT_OBJS += compat/nedmalloc/nedmalloc.o OVERRIDE_STRDUP = YesPlease endif diff --git a/grep.c b/grep.c index cd952ef5d3..0154998695 100644 --- a/grep.c +++ b/grep.c @@ -150,12 +150,22 @@ int grep_config(const char *var, const char *value, void *cb) * Initialize one instance of grep_opt and copy the * default values from the template we read the configuration * information in an earlier call to git_config(grep_config). + * + * If using PCRE make sure that the library is configured + * to use the right allocator (ex: NED) */ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) { struct grep_opt *def = &grep_defaults; int i; +#ifdef USE_NED_ALLOCATOR +#ifdef USE_LIBPCRE1 + pcre_malloc = malloc; + pcre_free = free; +#endif +#endif + memset(opt, 0, sizeof(*opt)); opt->repo = repo; opt->prefix = prefix; From patchwork Tue Aug 6 16:36:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= X-Patchwork-Id: 11079405 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BD5721709 for ; Tue, 6 Aug 2019 16:37:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ACB0D20174 for ; Tue, 6 Aug 2019 16:37:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A07DB2849D; Tue, 6 Aug 2019 16:37:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1810E20174 for ; Tue, 6 Aug 2019 16:37:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387860AbfHFQhD (ORCPT ); Tue, 6 Aug 2019 12:37:03 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:47096 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387810AbfHFQhD (ORCPT ); Tue, 6 Aug 2019 12:37:03 -0400 Received: by mail-pf1-f193.google.com with SMTP id c3so18658389pfa.13 for ; Tue, 06 Aug 2019 09:37:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PLQSfcY2hO8y3jJA5LQ+OeNzX9pjT4cZLrGyf4LmA38=; b=ZdVJXIleqGY/ZyX3IU6ZVH2Xa+Opfm92wgQcRQHVloseFoamNLVpl2/yS85H13dqWE 8QU1wEvQXk7ZvTZ23yZ28mK6y4RbeKbmlkG/aiD3BgDNW86SdenAkxpqXPfG54lzS1Yd /Sbj6BqjMhwFxLmaG8UsbreThXtdnPGBkKb4kFoKzt7lEiUbhsBdwo6h7qhGhVO1tnPU QL/L7qykOhBdngtgcdaGAIQidBAO8NPPrVSzEUOYHqxobsIhLekxPK1QJzhkJu59iJbm EAvEPuD6vqxcLXwZF0mvVPUkBvZc583PdQMcEU2dTP0EE4NAsrB9pst7v6W7J97Nl39X RCmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PLQSfcY2hO8y3jJA5LQ+OeNzX9pjT4cZLrGyf4LmA38=; b=E07UOFdt17HYpdq8JKlauJ7C0Zm+HK7Frp6xakGbRMcUmGHLB8ea2zRd8slVkFGh5P V5DMM4r1cJN6KhPZXjyCgxvv/A17pWwSr5Rqb1TpWUpP2GTeg8Nzdbi9qgz1K3Z0ibMp oPt9OJkYq99hBC2cgF9gmb+pnY64uFzlscS1uMVKDbhHAZ6jfznwFv1i6KwpZKRMiRkv rceRbYANXm/I8ZFs7n9HSh0Xs0j3aWNep82MqonKJSZZ8cWIqTf49enRMoQw08j2MIL+ xi7MNZpf4V9Jv/GzOIeEW7KtORJO7TKonAClBqazY3DkRiEOT/pDz7aV27GbZuuV/UMU VWQQ== X-Gm-Message-State: APjAAAUnQxyKluJe1+GGhlM1oXPLEZGIK3pZy+4DBRCKaOqcV0VsE44M mEkBzpL+DMt/efpJJ29fJVO/xnVPJCs= X-Google-Smtp-Source: APXvYqzdRhl5DSw/MzlNflp3YgDIl1TmZSytscIQ3Oxr9K1lBa3b7JVECGDV/ueB0CaE/9OEnrUMYg== X-Received: by 2002:a17:90a:5806:: with SMTP id h6mr3983993pji.126.1565109421819; Tue, 06 Aug 2019 09:37:01 -0700 (PDT) Received: from localhost.localdomain (c-67-188-192-166.hsd1.ca.comcast.net. [67.188.192.166]) by smtp.gmail.com with ESMTPSA id k36sm89364611pgl.42.2019.08.06.09.37.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Aug 2019 09:37:01 -0700 (PDT) From: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= To: git@vger.kernel.org Cc: gitster@pobox.com, johannes.schindelin@gmx.de, l.s.r@web.de, avarab@gmail.com, michal.kiedrowicz@gmail.com Subject: [RFC PATCH v3 2/3] grep: make PCRE2 aware of custom allocator Date: Tue, 6 Aug 2019 09:36:57 -0700 Message-Id: <20190806163658.66932-3-carenas@gmail.com> X-Mailer: git-send-email 2.23.0.rc1 In-Reply-To: <20190806163658.66932-1-carenas@gmail.com> References: <20190806085014.47776-1-carenas@gmail.com> <20190806163658.66932-1-carenas@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 94da9193a6 (grep: add support for PCRE v2, 2017-06-01) didn't include a way to override the system allocator, and so it is incompatible with USE_NED_ALLOCATOR. The problem was made visible when an attempt to avoid a leak in a data structure that is created by the library was passed to NED's free for disposal triggering a segfault in Windows. PCRE2 requires the use of a general context to override the allocator and therefore, there is a lot more code needed than in PCRE1, including a couple of wrapper functions. Extend the grep API with a "destructor" that could be called to cleanup any objects that were created and used globally. Update builtin/grep to use that new API, but any other future users should make sure to have matching grep_init/grep_destroy calls if they are using the pattern matching functionality (currently only relevant when using both NED and PCRE2) Move some of the logic that was before done per thread (in the workers) into an earlier phase to avoid degrading performance, but as the use of PCRE2 with NED is better understood it is expected more of its functions will be instructed to use the custom allocator as well as was done in the original code[1] this work was based on. [1] https://public-inbox.org/git/3397e6797f872aedd18c6d795f4976e1c579514b.1565005867.git.gitgitgadget@gmail.com/ Reported-by: Johannes Schindelin Signed-off-by: Carlo Marcelo Arenas Belón --- builtin/grep.c | 1 + grep.c | 36 +++++++++++++++++++++++++++++++++++- grep.h | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/builtin/grep.c b/builtin/grep.c index 560051784e..e49c20df60 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -1145,5 +1145,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix) run_pager(&opt, prefix); clear_pathspec(&pathspec); free_grep_patterns(&opt); + grep_destroy(); return !hit; } diff --git a/grep.c b/grep.c index 0154998695..3e5bdf94a6 100644 --- a/grep.c +++ b/grep.c @@ -16,6 +16,22 @@ static int grep_source_is_binary(struct grep_source *gs, static struct grep_opt grep_defaults; +#ifdef USE_LIBPCRE2 +static pcre2_general_context *pcre2_global_context; + +#ifdef USE_NED_ALLOCATOR +static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) +{ + return malloc(size); +} + +static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) +{ + return free(pointer); +} +#endif +#endif + static const char *color_grep_slots[] = { [GREP_COLOR_CONTEXT] = "context", [GREP_COLOR_FILENAME] = "filename", @@ -153,6 +169,7 @@ int grep_config(const char *var, const char *value, void *cb) * * If using PCRE make sure that the library is configured * to use the right allocator (ex: NED) + * if any object is created it should be cleaned up in grep_destroy() */ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) { @@ -188,6 +205,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix color_set(opt->colors[i], def->colors[i]); } +void grep_destroy(void) +{ +#ifdef USE_LIBPCRE2 + pcre2_general_context_free(pcre2_global_context); +#endif +} + static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt) { /* @@ -319,6 +343,11 @@ void append_header_grep_pattern(struct grep_opt *opt, void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t) { +#if defined(USE_LIBPCRE2) && defined(USE_NED_ALLOCATOR) + if (!pcre2_global_context && opt->ignore_case && has_non_ascii(pat)) + pcre2_global_context = pcre2_general_context_create( + pcre2_malloc, pcre2_free, NULL); +#endif append_grep_pat(opt, pat, strlen(pat), origin, no, t); } @@ -507,9 +536,14 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt p->pcre2_compile_context = NULL; + /* pcre2_global_context is initialized in append_grep_pattern */ if (opt->ignore_case) { if (has_non_ascii(p->pattern)) { - character_tables = pcre2_maketables(NULL); +#ifdef USE_NED_ALLOCATOR + if (!pcre2_global_context) + BUG("pcre2_global_context uninitialized"); +#endif + character_tables = pcre2_maketables(pcre2_global_context); p->pcre2_compile_context = pcre2_compile_context_create(NULL); pcre2_set_character_tables(p->pcre2_compile_context, character_tables); } diff --git a/grep.h b/grep.h index 1875880f37..526c2db9ef 100644 --- a/grep.h +++ b/grep.h @@ -189,6 +189,7 @@ struct grep_opt { void init_grep_defaults(struct repository *); int grep_config(const char *var, const char *value, void *); void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); +void grep_destroy(void); void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); From patchwork Tue Aug 6 16:36:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= X-Patchwork-Id: 11079411 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BF615746 for ; Tue, 6 Aug 2019 16:37:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEBC220174 for ; Tue, 6 Aug 2019 16:37:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2E282849D; Tue, 6 Aug 2019 16:37:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 49A5020174 for ; Tue, 6 Aug 2019 16:37:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387886AbfHFQhH (ORCPT ); Tue, 6 Aug 2019 12:37:07 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:41943 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387810AbfHFQhE (ORCPT ); Tue, 6 Aug 2019 12:37:04 -0400 Received: by mail-pg1-f195.google.com with SMTP id x15so31528484pgg.8 for ; Tue, 06 Aug 2019 09:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hwmuVTzHmyD9L5KeFW98E8S4xoStRlQOJIZx6ErvQYQ=; b=q6MT+ksCyimb8LfpyJxRn6bnhSO6ZCEKKxAbs07Z2yJtG4k7rG7DiK6JmpPRjOBo1V UJVMZtWMkNLnqXb0bdRHdE++hGR97yqj5nfpI2n/jT/LbFkqkgtKAzgAfVGmMLR2xNtu 2aPu2LjHTW46IisGHIbq+/wigJxlryzLdu2JWlLoGkkRkN2O2onk1v9S1q1eOdLM013d gS2A31oWg/7JSYlgIrFyYZj8WjHHDZx7U3K3dbbMp4iFdm9f5vTpt8iSBrppC1ff6bMg sbzmjPrJQpESJR3ngRHX93GtZ1fNgO1j1Tm7TvQxdsjTED9lgV4ZVctm2Gz9TX60AoTK kJ2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hwmuVTzHmyD9L5KeFW98E8S4xoStRlQOJIZx6ErvQYQ=; b=YnlE7a2oPh3+f9TYMcvjON4aPRjS67jCdDOCpXWUckEEJkirWgERjfACWNR5aw5gBT QAobaqbXHLr4L2CQuAwJgz/JtfAWqTCbn9YrtSzxiiLnxwFPD8Iz8LBLvUnkY5kKWj3j OVhDYtq3dnqYlpZ5aOpF+MvPJYpy/u5njBMFEoxmCxJ3z+AycOBNnNDW022IO646VeiV oEAGTW4mhhdo3fbs1Qr5PsDfQbURRmQSWhiUANcMXey3lRCqclWlEFxLzVU12JGJb/1P ah0oirUbndEdS2vuJkbzb5awjSxyG9NZt2EHoayls5Uxvcp6uztCc9vBmGZAmbVp/EkO Wp0Q== X-Gm-Message-State: APjAAAVAQExhH+cXGY/E8ifxgEgtyQh6scByXUHnmJk1ATKvOmbuwsP5 y+J0Ua45AVhkKkwJpM2jiXJ7UmGutxI= X-Google-Smtp-Source: APXvYqwdG3+JyneSzBvhNUpI6E/HOA/v5e2T4Zje6ep07llpzqNNbUC/oY2nAFKY5NRAgM01SM0ZWQ== X-Received: by 2002:a63:2157:: with SMTP id s23mr245304pgm.167.1565109422822; Tue, 06 Aug 2019 09:37:02 -0700 (PDT) Received: from localhost.localdomain (c-67-188-192-166.hsd1.ca.comcast.net. [67.188.192.166]) by smtp.gmail.com with ESMTPSA id k36sm89364611pgl.42.2019.08.06.09.37.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Aug 2019 09:37:02 -0700 (PDT) From: =?utf-8?q?Carlo_Marcelo_Arenas_Bel=C3=B3n?= To: git@vger.kernel.org Cc: gitster@pobox.com, johannes.schindelin@gmx.de, l.s.r@web.de, avarab@gmail.com, michal.kiedrowicz@gmail.com Subject: [RFC PATCH v3 3/3] grep: avoid leak of chartables in PCRE2 Date: Tue, 6 Aug 2019 09:36:58 -0700 Message-Id: <20190806163658.66932-4-carenas@gmail.com> X-Mailer: git-send-email 2.23.0.rc1 In-Reply-To: <20190806163658.66932-1-carenas@gmail.com> References: <20190806085014.47776-1-carenas@gmail.com> <20190806163658.66932-1-carenas@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 94da9193a6 ("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. Signed-off-by: Carlo Marcelo Arenas Belón --- grep.c | 7 ++++--- grep.h | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/grep.c b/grep.c index 3e5bdf94a6..3d3ea0414e 100644 --- a/grep.c +++ b/grep.c @@ -527,7 +527,6 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt PCRE2_UCHAR errbuf[256]; PCRE2_SIZE erroffset; int options = PCRE2_MULTILINE; - const uint8_t *character_tables = NULL; int jitret; int patinforet; size_t jitsizearg; @@ -543,9 +542,10 @@ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt if (!pcre2_global_context) BUG("pcre2_global_context uninitialized"); #endif - character_tables = pcre2_maketables(pcre2_global_context); + p->pcre2_tables = pcre2_maketables(pcre2_global_context); p->pcre2_compile_context = pcre2_compile_context_create(NULL); - pcre2_set_character_tables(p->pcre2_compile_context, character_tables); + pcre2_set_character_tables(p->pcre2_compile_context, + p->pcre2_tables); } options |= PCRE2_CASELESS; } @@ -649,6 +649,7 @@ static void free_pcre2_pattern(struct grep_pat *p) pcre2_match_data_free(p->pcre2_match_data); pcre2_jit_stack_free(p->pcre2_jit_stack); pcre2_match_context_free(p->pcre2_match_context); + free((void *)p->pcre2_tables); } #else /* !USE_LIBPCRE2 */ static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) diff --git a/grep.h b/grep.h index 526c2db9ef..533165c21a 100644 --- a/grep.h +++ b/grep.h @@ -96,6 +96,7 @@ struct grep_pat { pcre2_compile_context *pcre2_compile_context; pcre2_match_context *pcre2_match_context; pcre2_jit_stack *pcre2_jit_stack; + const uint8_t *pcre2_tables; uint32_t pcre2_jit_on; kwset_t kws; unsigned fixed:1;