From patchwork Thu Apr 21 15:33:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12821871 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 D3FABC433EF for ; Thu, 21 Apr 2022 15:34:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1390133AbiDUPg5 (ORCPT ); Thu, 21 Apr 2022 11:36:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349454AbiDUPgy (ORCPT ); Thu, 21 Apr 2022 11:36:54 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F7222ED4D for ; Thu, 21 Apr 2022 08:34:04 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id w4so7198635wrg.12 for ; Thu, 21 Apr 2022 08:34:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IhMGDPAKhoaaNHA5FpXStrnjZFZ5rm5/2QyhN1Dj3Ng=; b=eNHnL4lAV8kFeMeJjcpd2Z3EQT48B6fcDSaITeqHD2SB15bsgq1xAqafK9rsMaBdl4 sHT3MKJz9YRy6CUpFlmzu3sfGhWB3BxNNxdEA5tHnYqkwDNkSpGcPMXAPvvONkjf6/fQ dkryW08Yviy5EjSN6j8+xpsOA8EySBf/mYonelQditntElXbc5Mhxc0NPLNdZcQTeyDM IiIjT945l3Nm4Vd0Lb0wMthtZvd+5/exMP03HjJ5ZH5vUOgrhHEhyIOGyOmQti2bmEq9 YR1bmRb8B/ojWgdVtp2wDe1780Q9q44VCATHrmw/HHkJN8aeBMy1DOXz8TDgOCkbtsoa TImA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IhMGDPAKhoaaNHA5FpXStrnjZFZ5rm5/2QyhN1Dj3Ng=; b=8RVLq+6/jWzG1ECj+FovIX+eZ/ddHQJosVM6eJZKivwzB9lEKSDuPgGsMW5rSzeeWU UFdHcuDfSvJp++S0todSjqELcnakmGZXWHB0MVpe4fbQnobrv0CQktWYwEMoPHdwNN9K tbTegRQCOf3OjffWiN5ITrMgb49EBlQl4mot7tpYLIx1IW+b81EJRlBwSwtgWOrQqWoZ EMpgiQJd3dtPBmC3P5ljS1TIHjdydNnAqPHAx3zc4zGUTFZl9ZUlYQU2oSSgS6CWozc0 LovH34J4aP5ijHor9fdAyXSWWCAzLnOlhXcGOJuBV7AHTATVmtHoR9qjLkLDF5IL61k0 Wb4g== X-Gm-Message-State: AOAM530pG9Z2PKNNE1yNl9g+Ppjlf8l8ld85rKac3rMovunIS++hOxdr s4OvpIbgP9zoFLyEzD7F+ueC4Mi3RhEyBg== X-Google-Smtp-Source: ABdhPJxoFfVwgLPEgPD82in8S5svYO9Rt2UMY0ZsGUEeNd9XvJr49KYihInAFUS1jfV8ecDfo8QJ4A== X-Received: by 2002:a5d:518a:0:b0:20a:c249:5ef5 with SMTP id k10-20020a5d518a000000b0020ac2495ef5mr253626wrv.368.1650555242874; Thu, 21 Apr 2022 08:34:02 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l126-20020a1c2584000000b00387d4f35651sm2370516wml.10.2022.04.21.08.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 08:34:02 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Gregory David , ptm-dev , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 1/2] show-branch: refactor in preparation for next commit Date: Thu, 21 Apr 2022 17:33:47 +0200 Message-Id: X-Mailer: git-send-email 2.36.0.876.g7efc8a7728c In-Reply-To: References: <225b410d-2d98-8c0b-c289-22f753c175d4@p1sec.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Move the code in cmd_show_branch() that formats a reflog message for us into a function, and change the "flags" variable that we never change into a "const", in addition to moving it up a scope in preparation for the subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/show-branch.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/builtin/show-branch.c b/builtin/show-branch.c index 330b0553b9d..499ef76a508 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -618,6 +618,24 @@ static int parse_reflog_param(const struct option *opt, const char *arg, return 0; } +static char *fmt_reflog(char *const logmsg, const timestamp_t ts, const int tz, + const char *fmt) +{ + char *const end = strchr(logmsg, '\n'); + const char *msg; + char *ret; + + if (end) + *end = '\0'; + + msg = *logmsg ? logmsg : "(none)"; + + ret = xstrfmt(fmt, show_date(ts, tz, DATE_MODE(RELATIVE)), msg); + free(logmsg); + + return ret; +} + int cmd_show_branch(int ac, const char **av, const char *prefix) { struct commit *rev[MAX_REVS], *commit; @@ -640,6 +658,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) int topics = 0; int dense = 1; const char *reflog_base = NULL; + const unsigned int flags = 0; struct option builtin_show_branch_options[] = { OPT_BOOL('a', "all", &all_heads, N_("show remote-tracking and local branches")), @@ -720,7 +739,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) struct object_id oid; char *ref; int base = 0; - unsigned int flags = 0; if (ac == 0) { static const char *fake_av[2]; @@ -761,8 +779,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) for (i = 0; i < reflog; i++) { char *logmsg; char *nth_desc; - const char *msg; - char *end; timestamp_t timestamp; int tz; @@ -773,16 +789,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) break; } - end = strchr(logmsg, '\n'); - if (end) - *end = '\0'; - - msg = (*logmsg == '\0') ? "(none)" : logmsg; - reflog_msg[i] = xstrfmt("(%s) %s", - show_date(timestamp, tz, - DATE_MODE(RELATIVE)), - msg); - free(logmsg); + reflog_msg[i] = fmt_reflog(logmsg, timestamp, tz, + "(%s) %s"); nth_desc = xstrfmt("%s@{%d}", *av, base+i); append_ref(nth_desc, &oid, 1); From patchwork Thu Apr 21 15:33:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12821872 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 2A4E1C433EF for ; Thu, 21 Apr 2022 15:34:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1390142AbiDUPhA (ORCPT ); Thu, 21 Apr 2022 11:37:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349101AbiDUPgz (ORCPT ); Thu, 21 Apr 2022 11:36:55 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B766B2ED78 for ; Thu, 21 Apr 2022 08:34:05 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id u3so7252902wrg.3 for ; Thu, 21 Apr 2022 08:34:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2+ggnS5gF/FMbeaKLNYrNSBn0d7GYH4Ak96vH44Ih5w=; b=m77ch3LKmRSUtXWsjxbXA8jmoY70hp3rU9jTK3gyq9Q6WNv66XrclGIlzCR50UaS65 FstzZKXPl8fYJ2bHYKVpDE2II+KzKlj/3dOyKYagAPkHzJwdu+OJdL0XlNwfKJJjKqUC AYNBZaIh7oZ1olRH76ol74KHR7SqN9MWQfHjsPNISR91L2eViSZPJjliploO3ZnLvSts fxky6AnkmjlcUol1H5O+dl8ZTGWaH/QF+G2t1XCcZQEmlfXjK7Whw0WmAfLty/9O50x3 r9EyiRRki6zyljbzHMRsLYb8SVA8RI9e+ot8JAA1Jf/YWeR3W+YEF9Z9mGDfVoyqv5lO 2eKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2+ggnS5gF/FMbeaKLNYrNSBn0d7GYH4Ak96vH44Ih5w=; b=oMZurJxQFaDy2Wx61QP/SbtlxU8iBjj3Y7lA7PaiYn5r99kYwawumuAroQ6rxLDKGh LFExIC6gz+d7HTUjrORCfveOlSt6JRg1J6xd1UXxTjQPrHLIUq1zDegTH3bSNmflRVmy HRRnozOqPn/tx4np4pGuRx0nuiaZ62vyx34Rvp1wmVU593Fr+UVjPn1mgElB5qnSxXS+ +C921t7LHFfBZuNTBAoQx5/5IRMBW57FmuysSFvaZ7GhCKEQow157CkGiOyxNfdcqQjU 8gdMFmH5cBoRqnVhj+UGKQCvse449aA9z53iz/5PF6xFksPV7obMSFWH4SGdNOe6Uiu8 fksw== X-Gm-Message-State: AOAM531ztp5LTYYCni6+E9FlaiGQjm21BZGc01KZ7KyoyCWbZpeYzz6g LO0Y2LqBxdZ1DKlGmt0tU+cEeidhvwBofg== X-Google-Smtp-Source: ABdhPJzR3LTm00pRdca24sD/zEIsoW5OQ7G0qJlCNQmHGozETp1qpL9tuC13Kut0sKtA+SREATDwqA== X-Received: by 2002:adf:e0c5:0:b0:206:1ba3:26aa with SMTP id m5-20020adfe0c5000000b002061ba326aamr201054wri.645.1650555243929; Thu, 21 Apr 2022 08:34:03 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l126-20020a1c2584000000b00387d4f35651sm2370516wml.10.2022.04.21.08.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 08:34:03 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Gregory David , ptm-dev , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 2/2] show-branch: fix SEGFAULT when `--current` and `--reflog` together Date: Thu, 21 Apr 2022 17:33:48 +0200 Message-Id: X-Mailer: git-send-email 2.36.0.876.g7efc8a7728c In-Reply-To: References: <225b410d-2d98-8c0b-c289-22f753c175d4@p1sec.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Gregory David If run `show-branch` with `--current` and `--reflog` simultaneously, a SEGFAULT appears. The bug is that we read over the end of the `reflog_msg` array after having `append_one_rev()` for the current branch without supplying a convenient message to it. It seems that it has been introduced in: Commit 1aa68d6735 (show-branch: --current includes the current branch., 2006-01-11) Signed-off-by: Gregory DAVID Thanks-to: Ævar Arnfjörð Bjarmason Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/show-branch.c | 13 +++++++++++++ t/t3202-show-branch.sh | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/builtin/show-branch.c b/builtin/show-branch.c index 499ef76a508..50c17fb5c31 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -821,6 +821,19 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) } if (!has_head) { const char *name = head; + struct object_id oid; + char *ref; + char *msg; + timestamp_t ts; + int tz; + + if (!dwim_ref(*av, strlen(*av), &oid, &ref, 0)) + die(_("no such ref %s"), *av); + read_ref_at(get_main_ref_store(the_repository), ref, + flags, 0, i, &oid, &msg, &ts, &tz, NULL); + + reflog_msg[ref_name_cnt] = fmt_reflog(msg, ts, tz, + "(%s) (current) %s"); skip_prefix(name, "refs/heads/", &name); append_one_rev(name); } diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh index 7a1be73ce87..7f6ffcf8a57 100755 --- a/t/t3202-show-branch.sh +++ b/t/t3202-show-branch.sh @@ -161,4 +161,47 @@ test_expect_success 'show branch --reflog=2' ' test_cmp actual expect ' +test_expect_success 'show branch --reflog=2 --current' ' + sed "s/^> //" >expect <<-\EOF && + > ! [refs/heads/branch10@{0}] (4 years, 5 months ago) commit: branch10 + > ! [refs/heads/branch10@{1}] (4 years, 5 months ago) commit: branch10 + > * [branch10] (4 years, 5 months ago) (current) branch: Created from initial + > --- + > + * [refs/heads/branch10@{0}] branch10 + > ++* [refs/heads/branch10@{1}] initial + EOF + git show-branch --reflog=2 --current >actual && + test_cmp actual expect +' + +test_expect_success 'show branch --current' ' + sed "s/^> //" >expect <<-\EOF && + > ! [branch1] branch1 + > ! [branch2] branch2 + > ! [branch3] branch3 + > ! [branch4] branch4 + > ! [branch5] branch5 + > ! [branch6] branch6 + > ! [branch7] branch7 + > ! [branch8] branch8 + > ! [branch9] branch9 + > * [branch10] branch10 + > ! [master] initial + > ----------- + > * [branch10] branch10 + > + [branch9] branch9 + > + [branch8] branch8 + > + [branch7] branch7 + > + [branch6] branch6 + > + [branch5] branch5 + > + [branch4] branch4 + > + [branch3] branch3 + > + [branch2] branch2 + > + [branch1] branch1 + > +++++++++*+ [master] initial + EOF + git show-branch --current >actual && + test_cmp actual expect +' + test_done