From patchwork Mon Oct 10 17:41:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13002777 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A72EC433FE for ; Mon, 10 Oct 2022 17:41:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229483AbiJJRle (ORCPT ); Mon, 10 Oct 2022 13:41:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229604AbiJJRlc (ORCPT ); Mon, 10 Oct 2022 13:41:32 -0400 Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F065661D4C for ; Mon, 10 Oct 2022 10:41:31 -0700 (PDT) Received: by mail-io1-xd31.google.com with SMTP id d142so3013529iof.7 for ; Mon, 10 Oct 2022 10:41:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=I5RflWeMn2+94lg90TehdKgz11e4y+p7hgOENTVL9lc=; b=szmgOIBDsurzmSrUrr7fES8BeUxPLl5EVNpWqfbWdQNle3nMfAcyRuuYKBMWoy70wl pF0ubkivIYxdY3i+oqGhmQCbAmvgWlyv6G2Q37cixDpX//PVR51KUKjV/+mxcqrPVv3e L5Mc4D05wZ2bRGuNi1Nb0krdleFHgzyHYe6vTyToO7oEujhWot3Qouore5RrZ476LXqU BqPbejXm+R5yWBB6c6OVT/cfL2mf58CCPpGMd/s/hEXBR0YzCcr+Z665zhI8QsQTLpzF 9w8of0DXGmq9j+HDgKjTaLX1Z/8lhyv70jdCNBW9/4btQePFB1s8GwFO4ABR1O4xnJRw 8QIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=I5RflWeMn2+94lg90TehdKgz11e4y+p7hgOENTVL9lc=; b=drQOI12jx7ShJy1EZctxsaGyx/0BwJ7oZrSyBG+AkfDmWtEVVYqQ+6L1UupCz9cdeC i91JYHrFn6d3ZBYa7+xTxglInI2TePWowVFGWon+sFjzGI+X/LU9jtkVZkdkYoTVwZUs Dr0B76ZaOz5ljh37ZHD4Oe0iRSDKrAGSzb5kOFcLIIvbXcfNGtlYwIM5jsUMOBu5N04w ZV6lS/XucbrJH8u6sBHBxPacwobrDnEjiVAi62zbk/WJe5xuuMG90D9O620wRjgQhuFk g6LzmLHwVDTZSaqQ1GyAXl5pkeowMl7CQZxTXWJ9mWpZxoNmyhSyMdkZQWmhhP3tOgKm C2Pw== X-Gm-Message-State: ACrzQf2M5oC7bLqiWMsSXpiObDsksBCSCTG3sdnqFKsAK5Lphi2ZHdwL xRndRGLu3HiUWvldzkm7/YdsNyjHi549+S0v X-Google-Smtp-Source: AMsMyM5NsX5SgmuH/2bASPbNE+Pn8B2ZwE7vLfSJrUPdJrRnpTTphx1MxvACAErl0dtxyQS/PE6Tww== X-Received: by 2002:a05:6638:1606:b0:35a:5472:fa1b with SMTP id x6-20020a056638160600b0035a5472fa1bmr10238833jas.241.1665423691166; Mon, 10 Oct 2022 10:41:31 -0700 (PDT) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id q14-20020a02c8ce000000b0036378825c31sm4214451jao.70.2022.10.10.10.41.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 10:41:30 -0700 (PDT) Date: Mon, 10 Oct 2022 13:41:30 -0400 From: Taylor Blau To: git@vger.kernel.org Cc: orygaw@protonmail.com, rsbecker@nexbridge.com, gitster@pobox.com Subject: [PATCH 1/2] t4202: demonstrate `git log --invert-grep` segfault Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When `--invert-grep` is given without a pattern, `git log` behaves as normal. But as of f41fb662f5 (revisions API: have release_revisions() release "grep_filter", 2022-04-13), this doesn't quite work because we try to dereference the NULL `grep_expr` pointer in `free_pattern_expr()`, leading to a sgefault. The subsequent patch will explain the bug, provide a fix, and update this test to expect success. Signed-off-by: Taylor Blau --- t/t4202-log.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/t/t4202-log.sh b/t/t4202-log.sh index cc15cb4ff6..e3ec5f5661 100755 --- a/t/t4202-log.sh +++ b/t/t4202-log.sh @@ -297,6 +297,12 @@ test_expect_success 'log --invert-grep --grep -i' ' fi ' +test_expect_failure 'log --invert-grep (no --grep)' ' + git log --pretty="tformat:%s" >expect && + git log --invert-grep --pretty="tformat:%s" >actual && + test_cmp expect actual +' + test_expect_success 'log --grep option parsing' ' echo second >expect && git log -1 --pretty="tformat:%s" --grep sec >actual && From patchwork Mon Oct 10 17:41:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taylor Blau X-Patchwork-Id: 13002778 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAD52C433F5 for ; Mon, 10 Oct 2022 17:41:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbiJJRlm (ORCPT ); Mon, 10 Oct 2022 13:41:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229711AbiJJRlj (ORCPT ); Mon, 10 Oct 2022 13:41:39 -0400 Received: from mail-il1-x131.google.com (mail-il1-x131.google.com [IPv6:2607:f8b0:4864:20::131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F62B6AA3E for ; Mon, 10 Oct 2022 10:41:34 -0700 (PDT) Received: by mail-il1-x131.google.com with SMTP id q18so3209548ils.12 for ; Mon, 10 Oct 2022 10:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ttaylorr-com.20210112.gappssmtp.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+iQOa+H2UQieBO/ih0IN1BGbprL6801CQJjhC1IpcI4=; b=eIMFTpl7RPSUespf3qxUYEYISsSPPy5+A5/gij2rAQpn+Xcikcqgc59wzzLLZgWYUT ZywlT8FKgFd8y8W7dxqdIuq2ydo2FfmUAgKL0+9Fzmo7yp61ANt2RBIiFgrv3rTtwt8t rZXjgDDMWeqlKqDXL35pLjgWFr5ObvRmNk2RnULAoPFjv7TZIxNWBX7+HWPMIC4J2fY8 5U5Gh8tMPMo132F8jgpfCrtTxrMZvk3plgsHEeJMimJB/ghVa0cYVSWEHM9+yv/7hUl+ WN3aF7yryEOhc5wzNZw6L/rFWV0dORhQ460kjDjEEV02toK95La3qcvIJrq2wnd9uhNP c3uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+iQOa+H2UQieBO/ih0IN1BGbprL6801CQJjhC1IpcI4=; b=T3GEu5rHl5aFddWyFfB7RFErhiDmo42MxIk5ODWt1sWYnWL6GWsuyFhLT4CgbVJ5y/ CR3eh9gh+DLer1+4C0QMvbxqNYYnawH+8LFFtd1moRQslvlagx/yDaDmgcbx3KceCnpx mF6VBd24zwKLPCzVdOfZ+aT6e50VZljStuR2iwsEvt4PdygD+aUH9W3BEc3ZomdU05zr +TwK+t0reL90JdK3TB8DnDEEw2z3kDL/unztfkqVjFwodNM/WjKNn8b+DvHYB1Nk2A0F o9gQgXPu1kDRUzIweNqSpaw64g7BFcwCK4cuwdi2MRTD7fzKtarGaepY5YRlWZCvleKW uhFw== X-Gm-Message-State: ACrzQf0lEhW4M2WWW9ktE8C3KtdSBkmTfhBTRIVxo0Rma4Kf9IirJjgU bpSWCr88bq0oA7gpyrHKWmg/5zwOwKWZAfeu X-Google-Smtp-Source: AMsMyM5x6/kKnf1oPqL4hS2O4JzlnGYN2A76Wpn4s9gYKxBzIJ2UADEVI7lwf+lJ4x7dPCDUVRpHCg== X-Received: by 2002:a92:ca0d:0:b0:2fc:24a6:9115 with SMTP id j13-20020a92ca0d000000b002fc24a69115mr4251796ils.70.1665423693805; Mon, 10 Oct 2022 10:41:33 -0700 (PDT) Received: from localhost (104-178-186-189.lightspeed.milwwi.sbcglobal.net. [104.178.186.189]) by smtp.gmail.com with ESMTPSA id bq8-20020a056638468800b00363b0517662sm1897185jab.12.2022.10.10.10.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Oct 2022 10:41:33 -0700 (PDT) Date: Mon, 10 Oct 2022 13:41:32 -0400 From: Taylor Blau To: git@vger.kernel.org Cc: orygaw@protonmail.com, rsbecker@nexbridge.com, gitster@pobox.com Subject: [PATCH 2/2] grep.c: tolerate NULL grep_expr in free_pattern_expr() Message-ID: <7e094882c2a71894416089f894557a9eae07e8f8.1665423686.git.me@ttaylorr.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As demonstrated in the previous commit, `git log --invert-grep` without a `--grep` argument causes a segfault after f41fb662f5 (revisions API: have release_revisions() release "grep_filter", 2022-04-13). The segfault occurs in `free_pattern_expr()`, which crashes on trying to switch on `x->node` when given a NULL pointer. Usually we avoid calling `free_pattern_expr()` without a pattern as indicated by the `extended` bit being zero. But it is possible to get into a state where the `extended` bit is non-zero, but the `pattern_expression` is still NULL. This happens because the `--invert-grep` option sets the `no_body_match` bit. When we call `compile_grep_patterns()`, we set `opt->extended = 1`. But the `pattern_expression` is left as NULL, since we return with a NULL `header_expr`. So when we try to call `free_pattern_expr()`, things go awry, since `free_grep_patterns()` expects a non-NULL argument. Instead, teach `free_grep_patterns()` to tolerate a NULL argument (treating it as a noop), and avoid checking whether or not the `extended` bit is set, since `free_pattern_expr()` will handle its argument regardless. Signed-off-by: Taylor Blau --- grep.c | 5 +++-- t/t4202-log.sh | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/grep.c b/grep.c index 52a894c989..bcc6e63365 100644 --- a/grep.c +++ b/grep.c @@ -752,6 +752,9 @@ void compile_grep_patterns(struct grep_opt *opt) static void free_pattern_expr(struct grep_expr *x) { + if (!x) + return; + switch (x->node) { case GREP_NODE_TRUE: case GREP_NODE_ATOM: @@ -790,8 +793,6 @@ void free_grep_patterns(struct grep_opt *opt) free(p); } - if (!opt->extended) - return; free_pattern_expr(opt->pattern_expression); } diff --git a/t/t4202-log.sh b/t/t4202-log.sh index e3ec5f5661..44f7ef0ea2 100755 --- a/t/t4202-log.sh +++ b/t/t4202-log.sh @@ -297,7 +297,7 @@ test_expect_success 'log --invert-grep --grep -i' ' fi ' -test_expect_failure 'log --invert-grep (no --grep)' ' +test_expect_success 'log --invert-grep (no --grep)' ' git log --pretty="tformat:%s" >expect && git log --invert-grep --pretty="tformat:%s" >actual && test_cmp expect actual