From patchwork Fri Oct 22 18:19:34 2021 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: 12578385 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2D5EC433EF for ; Fri, 22 Oct 2021 18:19:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D1F9461163 for ; Fri, 22 Oct 2021 18:19:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233733AbhJVSWD (ORCPT ); Fri, 22 Oct 2021 14:22:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231472AbhJVSWB (ORCPT ); Fri, 22 Oct 2021 14:22:01 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9818C061764 for ; Fri, 22 Oct 2021 11:19:43 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id r17-20020a7bc091000000b0032ca1d30b30so1305369wmh.0 for ; Fri, 22 Oct 2021 11:19:43 -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=br/X668ExVNUCVD+imCkGkec5xBNUahSkKKpmXCD/bE=; b=Yl0JXOizGeIDWV12ECbzTJtx801IObvzCWCksOE2yw7JMK21UFNhtbIDo8l7r8HocW BNhVlXzvvR8IHbdyyl7lC4Z40S+GBa4LKl46He36pZhX8mWoTm9zZr9oq8kYY4XKkFtO s5i35UM11XN6FbKsaLsB+I9RTREU7EkHRxb7AFgO4hU8m3ts0falN0J5adIbRflNjddv EV88EMwdlhZN1EY5IK/9HDJKCTHs/2WQ2w4XNSWohQdmmIyX3paB8Pf8bceoPo9tJuGH 2SDjkRAjzwmYTchG9eA3d8ERYnx+O7ll/Mo73ywR/CGDAu9f2JIICKvvUYM5dfNZ7+Zh N9+g== 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=br/X668ExVNUCVD+imCkGkec5xBNUahSkKKpmXCD/bE=; b=3RBVzxptMbh+eISDxtljh+I1CKqCOcJ8uZmNe0AmEKCr7IEYAEdI+bt8fpNA8q4wAc eh3Mrhq3Ww0XfeBA0HXSPguK25Jm8I4338wex5BFqCsGoEpaeGNZxkhp3LZ0+FK/WYeA 9M+rFMC8o8pbLkzLK7W/EhJhpqg4a++mccpX5ZCBawOVLz2ZxXTcD7W0OAfoj2NVFnI8 aoS5xSaGQ6fUZzfcHJtDlXh6tiujQ09KTOcArjtrKLlFUhDytf2fv+c2U5SBzC60YeiF AsU8CbTg9TwwvQs0FiCAEAoq4io4esYN1COwaNXZARKmFF8pUdcZifN87Lnzglj6dMTp 5ZFw== X-Gm-Message-State: AOAM531D14Zx/dIz/hY43cIe2VdeFP6J8TsPDPQoNXh6mTiHGUICn39W +PrJ0GdP/n+trBP8vsiJ297ThkkUKJIINA== X-Google-Smtp-Source: ABdhPJycogyH3lCeAayI10ppdpowV6fRBArk50LnsEMgwUGS/fqlOo+X6grmfNIt7LztuvGfntCmPA== X-Received: by 2002:a05:600c:2041:: with SMTP id p1mr1382826wmg.145.1634926781969; Fri, 22 Oct 2021 11:19:41 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:40 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 1/6] usage.c: add a die_message() routine Date: Fri, 22 Oct 2021 20:19:34 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have code in various places that would like to call die(), but wants to defer the exit(128) it would invoke, e.g. to print an additional message, or adjust the exit code. Add a die_message() helper routine to bridge this gap in the API. Functionally this behaves just like the error() routine, except it'll print a "fatal: " prefix, and it will exit with 128 instead of -1, this is so that caller can pas the return value to exit(128), instead of having to hardcode "128". A subsequent commit will migrate various callers that benefit from this function over to it, for now we're just migrating trivial users in usage.c itself. Signed-off-by: Ævar Arnfjörð Bjarmason --- git-compat-util.h | 3 +++ usage.c | 46 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/git-compat-util.h b/git-compat-util.h index 141bb86351e..c1bb32460b6 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -471,6 +471,7 @@ NORETURN void usage(const char *err); NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); @@ -505,6 +506,8 @@ static inline int const_error(void) typedef void (*report_fn)(const char *, va_list params); void set_die_routine(NORETURN_PTR report_fn routine); +void set_die_message_routine(report_fn routine); +report_fn get_die_message_routine(void); void set_error_routine(report_fn routine); report_fn get_error_routine(void); void set_warn_routine(report_fn routine); diff --git a/usage.c b/usage.c index c7d233b0de9..3d4b90bce1f 100644 --- a/usage.c +++ b/usage.c @@ -55,6 +55,12 @@ static NORETURN void usage_builtin(const char *err, va_list params) exit(129); } +static void die_message_builtin(const char *err, va_list params) +{ + trace2_cmd_error_va(err, params); + vreportf("fatal: ", err, params); +} + /* * We call trace2_cmd_error_va() in the below functions first and * expect it to va_copy 'params' before using it (because an 'ap' can @@ -62,10 +68,9 @@ static NORETURN void usage_builtin(const char *err, va_list params) */ static NORETURN void die_builtin(const char *err, va_list params) { - trace2_cmd_error_va(err, params); - - vreportf("fatal: ", err, params); + report_fn die_message_fn = get_die_message_routine(); + die_message_fn(err, params); exit(128); } @@ -109,6 +114,7 @@ static int die_is_recursing_builtin(void) * (ugh), so keep things static. */ static NORETURN_PTR report_fn usage_routine = usage_builtin; static NORETURN_PTR report_fn die_routine = die_builtin; +static report_fn die_message_routine = die_message_builtin; static report_fn error_routine = error_builtin; static report_fn warn_routine = warn_builtin; static int (*die_is_recursing)(void) = die_is_recursing_builtin; @@ -118,6 +124,16 @@ void set_die_routine(NORETURN_PTR report_fn routine) die_routine = routine; } +void set_die_message_routine(report_fn routine) +{ + die_message_routine = routine; +} + +report_fn get_die_message_routine(void) +{ + return die_message_routine; +} + void set_error_routine(report_fn routine) { error_routine = routine; @@ -157,14 +173,23 @@ void NORETURN usage(const char *err) usagef("%s", err); } +#undef die_message +int die_message(const char *err, ...) +{ + va_list params; + + va_start(params, err); + die_message_routine(err, params); + va_end(params); + return 128; +} + void NORETURN die(const char *err, ...) { va_list params; - if (die_is_recursing()) { - fputs("fatal: recursion detected in die handler\n", stderr); - exit(128); - } + if (die_is_recursing()) + exit(die_message("recursion detected in die handler")); va_start(params, err); die_routine(err, params); @@ -200,11 +225,8 @@ void NORETURN die_errno(const char *fmt, ...) char buf[1024]; va_list params; - if (die_is_recursing()) { - fputs("fatal: recursion detected in die_errno handler\n", - stderr); - exit(128); - } + if (die_is_recursing()) + exit(die_message("recursion detected in die_errno handler")); va_start(params, fmt); die_routine(fmt_with_err(buf, sizeof(buf), fmt), params); From patchwork Fri Oct 22 18:19:35 2021 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: 12578387 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7565BC433FE for ; Fri, 22 Oct 2021 18:19:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 51B5761181 for ; Fri, 22 Oct 2021 18:19:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233732AbhJVSWE (ORCPT ); Fri, 22 Oct 2021 14:22:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233685AbhJVSWC (ORCPT ); Fri, 22 Oct 2021 14:22:02 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86449C061764 for ; Fri, 22 Oct 2021 11:19:44 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id m42so3693723wms.2 for ; Fri, 22 Oct 2021 11:19:44 -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=DDWnKkrDWdv/bzfdOo+3U33CbMfvef1TXzgmF9vreFU=; b=GhBwROt7YAqa+uLqUcpNlAKT5Jkxj4rXT2wBxiSoTlXAO4C2Z21u3gW/Ez4uccOJNb NMK5Wfd7pFeMuoOskxuBAS940VMlb8vlv7hLQXRFwoNfSqE7uk/sffAUuSTZqxwwo+FC OhrIxF3m/t1UmjWqfYzSliw3bchC6uQeX3NchC52LFIQr4+dHWRdBwLQLXTtD3S+e3rI sW+FhLKtJjOAud2E9BsNEE9KTY+AKpxyQOH4QrcF2t4n/MaJA0JMWoWZto7kLtKg7Pnp FYsqItP6DBFDc6Lv+C+Q5UwBAPjSfmylCsPnReE6+VzDQbKVqxq6R1D7eVDgTeKRUKel cPpg== 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=DDWnKkrDWdv/bzfdOo+3U33CbMfvef1TXzgmF9vreFU=; b=5/ClY5dse4E86Ewtq7rVzP7SFE9+mzK0Lg6lrBwn2WzZH5/nefhHhg5erkdVDbHRk4 yIb/eajWj/c8NO10ijP473fr5L+fnxt3RyYQNIC4v5yrz/Fxq4NJwC2hrvITIVoIq1YG av3VHYBMms09hIGsOc+c+gfF3LOGQw7ku7i61SVG5sIxK1FprAlKEdPvNI5T/g9cA2Bm ol3gA+6UL9r+vsK3MV/S4BlwIwWjHHTtdt/vw3oJwe9uLrJ0bkir5Xkl4P47ap5dVye7 r5heVBzAU0xzIZC2DkO513DxacHlFamoUVICAqAZInpoRUZ9ygQHlA9Go0bdssOn9LwK YF0g== X-Gm-Message-State: AOAM533sU8K8wQ0dI5nZ11LyzdzP+umKnh3blnRADlczDtXanUr2znI5 G5Tj5vhqGQWYesUEv8514SnNUTn1zX9WDw== X-Google-Smtp-Source: ABdhPJwn65nzdvEFt6EIZGeUSf3ub7mRR/YshyDww1Xvrr6cGWzph8bQ4NbPrB0KgiJU6e41wDSZkQ== X-Received: by 2002:a7b:c744:: with SMTP id w4mr30803026wmk.51.1634926782831; Fri, 22 Oct 2021 11:19:42 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:42 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 2/6] usage.c API users: use die_message() where appropriate Date: Fri, 22 Oct 2021 20:19:35 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code that either called error() and proceeded to exit with 128, or emitted its own "fatal: " messages to use the die_message() function added in a preceding commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-import.c | 13 ++++++++----- builtin/notes.c | 9 +++++---- http-backend.c | 3 ++- parse-options.c | 2 +- run-command.c | 16 +++++----------- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 20406f67754..11cd5b0c56c 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -401,16 +401,19 @@ static void dump_marks(void); static NORETURN void die_nicely(const char *err, va_list params) { + va_list cp; static int zombie; - char message[2 * PATH_MAX]; + report_fn die_message_fn = get_die_message_routine(); - vsnprintf(message, sizeof(message), err, params); - fputs("fatal: ", stderr); - fputs(message, stderr); - fputc('\n', stderr); + if (!zombie) + va_copy(cp, params); + die_message_fn(err, params); if (!zombie) { + char message[2 * PATH_MAX]; + zombie = 1; + vsnprintf(message, sizeof(message), err, cp); write_crash_report(message); end_packfile(); unkeep_all_packs(); diff --git a/builtin/notes.c b/builtin/notes.c index 71c59583a17..2812d1eac40 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -201,11 +201,12 @@ static void prepare_note_data(const struct object_id *object, struct note_data * static void write_note_data(struct note_data *d, struct object_id *oid) { if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) { - error(_("unable to write note object")); + int status = die_message(_("unable to write note object")); + if (d->edit_path) - error(_("the note contents have been left in %s"), - d->edit_path); - exit(128); + die_message(_("the note contents have been left in %s"), + d->edit_path); + exit(status); } } diff --git a/http-backend.c b/http-backend.c index e7c0eeab230..bc853356e73 100644 --- a/http-backend.c +++ b/http-backend.c @@ -661,8 +661,9 @@ static NORETURN void die_webcgi(const char *err, va_list params) { if (dead <= 1) { struct strbuf hdr = STRBUF_INIT; + report_fn die_message_fn = get_die_message_routine(); - vreportf("fatal: ", err, params); + die_message_fn(err, params); http_status(&hdr, 500, "Internal Server Error"); hdr_nocache(&hdr); diff --git a/parse-options.c b/parse-options.c index 6e0535bdaad..c892641d9a1 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1049,7 +1049,7 @@ void NORETURN usage_msg_opt(const char *msg, const char * const *usagestr, const struct option *options) { - fprintf(stderr, "fatal: %s\n\n", msg); + die_message("%s\n", msg); /* The extra \n is intentional */ usage_with_options(usagestr, options); } diff --git a/run-command.c b/run-command.c index 7ef5cc712a9..220cf53deb4 100644 --- a/run-command.c +++ b/run-command.c @@ -340,15 +340,6 @@ static void child_close_pair(int fd[2]) child_close(fd[1]); } -/* - * parent will make it look like the child spewed a fatal error and died - * this is needed to prevent changes to t0061. - */ -static void fake_fatal(const char *err, va_list params) -{ - vreportf("fatal: ", err, params); -} - static void child_error_fn(const char *err, va_list params) { const char msg[] = "error() should not be called in child\n"; @@ -372,9 +363,10 @@ static void NORETURN child_die_fn(const char *err, va_list params) static void child_err_spew(struct child_process *cmd, struct child_err *cerr) { static void (*old_errfn)(const char *err, va_list params); + report_fn die_message_routine = get_die_message_routine(); old_errfn = get_error_routine(); - set_error_routine(fake_fatal); + set_error_routine(die_message_routine); errno = cerr->syserr; switch (cerr->err) { @@ -1082,7 +1074,9 @@ static void *run_thread(void *data) static NORETURN void die_async(const char *err, va_list params) { - vreportf("fatal: ", err, params); + report_fn die_message_fn = get_die_message_routine(); + + die_message_fn(err, params); if (in_async()) { struct async *async = pthread_getspecific(async_key); From patchwork Fri Oct 22 18:19:36 2021 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: 12578389 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C4D7C4332F for ; Fri, 22 Oct 2021 18:19:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5294161183 for ; Fri, 22 Oct 2021 18:19:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233764AbhJVSWF (ORCPT ); Fri, 22 Oct 2021 14:22:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233738AbhJVSWD (ORCPT ); Fri, 22 Oct 2021 14:22:03 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59604C061766 for ; Fri, 22 Oct 2021 11:19:45 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id y132-20020a1c7d8a000000b0032ca5765d6cso849236wmc.0 for ; Fri, 22 Oct 2021 11:19:45 -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=2rDfPSVhqXlv/xDcaBlCU5lLfZWmah2Ci7F721DJCsQ=; b=S572Xch3sIzj7eQeAL/5tQIHjTO5/7Nhu76oE0NByPV5AHlT515xx/n0x4ptzOpyGd DT+Gqi8TP1L2c+ped4HI9yJNDGV3fvoKw+LBu9kctF6rneSA3gcLPEWyc1F1wop32Rt1 HsU8NvCvD80XUHEQC08H9ddKRJdTQ2hg3CT746gXDkTqdU7KmsjzT5oM46MS9vpkks1+ gg6xkaNE9lcgRqxtR8717bZOVyD0EfHMC9hdtdOMU2ugKrwcGnxvUcolP9wmukaCY6dS +it3YeXI6ThA11d19qBYe3ofI2s8YA7lThfmj4jl2PBFsWI8Y8YzYwJdPR1GGHPJmSaz BVTw== 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=2rDfPSVhqXlv/xDcaBlCU5lLfZWmah2Ci7F721DJCsQ=; b=st1f2VI6wRdDWzKwhfPjrVjdf4Q0FofkWw9GUZxmecVm8/4tkntRs7FeZFFWoq2het VZhoA/j1TW/Nc4eXBJB4d+mUYkS70XsFk1jtktyeVztRgCFyYwIrcFxJfQvDI+jGoZlG u+1uQnizk6AOSRMDCv1TkNOBTSpf6ni7duIlA++VjWQ1qYfK8ivF5yygpkGh73bW8pW3 uigvgcIariwi1yzV+9aY7akr1KVFUW7OmzUEv00Q1Jn62eiW03FnM0at5szFWRF8JgEu aU2wNv3lb38kWTpe8n702RL9idhNMVZEkb5hWqEPFfh7CytsXRotpw6uA6ZXCj1ZM/Gg +2sA== X-Gm-Message-State: AOAM533RPb2CUzDkDl8k+wvqv/xzqeRdbQwdxdd8p4cg4UvMZoY8xNaD bdHHH88RQz7iaABsTLgioGUdA9YsN8ZijA== X-Google-Smtp-Source: ABdhPJwtCAzaw6bh/K8G7jTSvWty/EDezhUFHPeiC+BQ4l/oVBVaLLqm5d07zrq5zrbtGxizToqxfQ== X-Received: by 2002:a7b:c40b:: with SMTP id k11mr30887327wmi.181.1634926783701; Fri, 22 Oct 2021 11:19:43 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:43 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 3/6] usage.c + gc: add and use a die_message_errno() Date: Fri, 22 Oct 2021 20:19:36 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change code the "error: " output when we exit with 128 due to gc.log errors to use a "fatal: " prefix instead. This adds a sibling function to the die_errno() added in a preceding commit. Since it returns 128 instead of -1 we'll need to adjust report_last_gc_error(). Let's adjust it while we're at it to not conflate the "should skip" and "exit with this non-zero code" conditions, as the caller is no longer hardcoding "128", but relying on die_errno() to return a nen-zero exit() status. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/gc.c | 21 ++++++++++++--------- git-compat-util.h | 1 + usage.c | 12 ++++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/builtin/gc.c b/builtin/gc.c index 6b3de3dd514..f7deef08974 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -472,19 +472,20 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid) * gc should not proceed due to an error in the last run. Prints a * message and returns -1 if an error occurred while reading gc.log */ -static int report_last_gc_error(void) +static int report_last_gc_error(int *skip) { struct strbuf sb = STRBUF_INIT; int ret = 0; ssize_t len; struct stat st; char *gc_log_path = git_pathdup("gc.log"); + *skip = 0; if (stat(gc_log_path, &st)) { if (errno == ENOENT) goto done; - ret = error_errno(_("cannot stat '%s'"), gc_log_path); + ret = die_message_errno(_("cannot stat '%s'"), gc_log_path); goto done; } @@ -493,7 +494,7 @@ static int report_last_gc_error(void) len = strbuf_read_file(&sb, gc_log_path, 0); if (len < 0) - ret = error_errno(_("cannot read '%s'"), gc_log_path); + ret = die_message_errno(_("cannot read '%s'"), gc_log_path); else if (len > 0) { /* * A previous gc failed. Report the error, and don't @@ -507,7 +508,7 @@ static int report_last_gc_error(void) "until the file is removed.\n\n" "%s"), gc_log_path, sb.buf); - ret = 1; + *skip = 1; } strbuf_release(&sb); done: @@ -610,13 +611,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix) fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n")); } if (detach_auto) { - int ret = report_last_gc_error(); - if (ret < 0) - /* an I/O error occurred, already reported */ - exit(128); - if (ret == 1) + int skip; + int ret = report_last_gc_error(&skip); + + if (skip) /* Last gc --auto failed. Skip this one. */ return 0; + if (ret) + /* an error occurred, already reported */ + exit(ret); if (lock_repo_for_gc(force, &pid)) return 0; diff --git a/git-compat-util.h b/git-compat-util.h index c1bb32460b6..ea0ac80f7db 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -472,6 +472,7 @@ NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)) NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2))); NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int die_message(const char *err, ...) __attribute__((format (printf, 1, 2))); +int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); int error(const char *err, ...) __attribute__((format (printf, 1, 2))); int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2))); void warning(const char *err, ...) __attribute__((format (printf, 1, 2))); diff --git a/usage.c b/usage.c index 3d4b90bce1f..efc2064dde3 100644 --- a/usage.c +++ b/usage.c @@ -233,6 +233,18 @@ void NORETURN die_errno(const char *fmt, ...) va_end(params); } +#undef die_message_errno +int die_message_errno(const char *fmt, ...) +{ + char buf[1024]; + va_list params; + + va_start(params, fmt); + die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params); + va_end(params); + return -1; +} + #undef error_errno int error_errno(const char *fmt, ...) { From patchwork Fri Oct 22 18:19:37 2021 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: 12578391 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34B26C433EF for ; Fri, 22 Oct 2021 18:19:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 13DDB61163 for ; Fri, 22 Oct 2021 18:19:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233790AbhJVSWF (ORCPT ); Fri, 22 Oct 2021 14:22:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233750AbhJVSWE (ORCPT ); Fri, 22 Oct 2021 14:22:04 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EEAFC061764 for ; Fri, 22 Oct 2021 11:19:46 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id 67-20020a1c1946000000b0030d4c90fa87so3151256wmz.2 for ; Fri, 22 Oct 2021 11:19:46 -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=Mnb6KdryDykVIaMkHyjvlyN5vBcTaA+lvHqAgmmU900=; b=R0mZlHApCcThZAfhVYzTZ2h6Rt0lLKGE9EmTBmZmTh4ih6WT7ufJz+nq0r5g+WmXw4 JvV/42toU3Jrqx2MQdQXbd4U2uNWiT88wJyKVl1QpYzst5RZg4u0/gdR44U6qj7qYA+g XBjyxWhVj+TRAFhx0N8Qsv4Qt8ppU/Fj+4zhtI1GE5sAjNZ5c6bd+Xw3PtWmAQatom2+ HY31gmtQ0D6PLvfKye2mIDcYMLiAnj5JgH0RbRh+c+VUCNCZrpeLcJ9osjJOgb5PPWXs GhCxZYagBrUYfGPjDoOkV1VM2g3T4Cvxg8P1bJEXYAdlU0JLnVUf5vpVtfdK2h2QBc2w QgVQ== 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=Mnb6KdryDykVIaMkHyjvlyN5vBcTaA+lvHqAgmmU900=; b=If4lEjheMocjvBvkItvKco8Z4g5S7jli4u/q+kR6QJy9LY0DrikCCm/P6fluxAwVpH C5pTmpPUSNRo+ctJRFTTEvbFFwUC8B1xLn33iD6gJ+dOW/bx7pRaWhp8NGHO6DDf5ZtU yQEPrfSwrxPIspSuVyslRTi302t+i4jaE0fevtitqtQSx6WF6UsuwrkVMzrJ3b13So4N jHvgRNFSk3gCPVzxJ2zhpIMKoIyksvLtTTUK+IiwPpG19aEVh4/oDTzDB9WpcNTQzzVC tt9A+hnHrEoZTZkiumWQT+I0Jkjxvz7wAigi8/iNOYxUSFdOxW5g2r/OD3T4Geglt/eW 4KbA== X-Gm-Message-State: AOAM533cXuPx9FlLSLC7+E3dBi298P1De29yvL6BsG7LUFX8P1Nl2a0x 7f+SegYOu5tnEOusY9XsAywcSntHfEHIgQ== X-Google-Smtp-Source: ABdhPJxdXgPbh9HmrpmA/0wecdB1Ny5xHqKXCCQMm+0+zhCXH8A1Y5v+s7gKTolYwghXqDMd01DASA== X-Received: by 2002:a05:600c:511c:: with SMTP id o28mr30221823wms.81.1634926784913; Fri, 22 Oct 2021 11:19:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:43 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 4/6] config.c: don't leak memory in handle_path_include() Date: Fri, 22 Oct 2021 20:19:37 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak in the error() path in handle_path_include(), this allows us to run t1305-config-include.sh under SANITIZE=leak, previously 4 tests there would fail. This fixes up a leak in 9b25a0b52e0 (config: add include directive, 2012-02-06). Signed-off-by: Ævar Arnfjörð Bjarmason --- config.c | 7 +++++-- t/t1305-config-include.sh | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config.c b/config.c index 2dcbe901b6b..c5873f3a706 100644 --- a/config.c +++ b/config.c @@ -148,8 +148,10 @@ static int handle_path_include(const char *path, struct config_include_data *inc if (!is_absolute_path(path)) { char *slash; - if (!cf || !cf->path) - return error(_("relative config includes must come from files")); + if (!cf || !cf->path) { + ret = error(_("relative config includes must come from files")); + goto cleanup; + } slash = find_last_dir_sep(cf->path); if (slash) @@ -167,6 +169,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc ret = git_config_from_file(git_config_include, path, inc); inc->depth--; } +cleanup: strbuf_release(&buf); free(expanded); return ret; diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh index ccbb116c016..5cde79ef8c4 100755 --- a/t/t1305-config-include.sh +++ b/t/t1305-config-include.sh @@ -1,6 +1,7 @@ #!/bin/sh test_description='test config file include directives' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh # Force setup_explicit_git_dir() to run until the end. This is needed From patchwork Fri Oct 22 18:19:38 2021 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: 12578393 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5211C433F5 for ; Fri, 22 Oct 2021 18:19:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B6DCA60EB8 for ; Fri, 22 Oct 2021 18:19:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233908AbhJVSWM (ORCPT ); Fri, 22 Oct 2021 14:22:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233738AbhJVSWF (ORCPT ); Fri, 22 Oct 2021 14:22:05 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81784C061767 for ; Fri, 22 Oct 2021 11:19:47 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id v127so3674809wme.5 for ; Fri, 22 Oct 2021 11:19:47 -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=U09sdpyXOpQAiokID9yBMbAGXHIjD+Y6V6QVoUGqhxQ=; b=Rrl7Hp00lSjAY5f8vgyfQJhGadFMQWzE2JwCJLnuu8WmtifdrC+6ihQFkmi39OFW3W lDfRYDH2tSd0dQDjd6xMXijwq2bdVFlQDyb0pnuiM2YKyFfnjLeliTcrfyq2Lp4ozSOG EjCFMvPAlATK23K4VVEEZlHw4YhAE4scjgs8e5TDEqySWXONLxuBWyekcDxJeiwi0W2V KRgsPZ9TTP60cDxBfDdPrG42xIOq4WUEGaDvUkgHRkR6/QH253J/vErvnUQlsFBJjRUy p8148ewMJqtsavemEYQu1pFQ+GtL1XhxphOFd1TzqFqtkql/yZDt/0KoBfp/p0iLMC+L 5ncg== 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=U09sdpyXOpQAiokID9yBMbAGXHIjD+Y6V6QVoUGqhxQ=; b=xXCnzLCSYyDCy1/Jc58iYxxUBjbpTCV+QrM6GkazVCyq7LPdkFGeI4PQQPzKdwa7u2 e9xaPRbH5x6aiR086YfyU6dwgpY1B+/GbkNmFzZjQbyeV/f1Ff3Ew7TeoXFxKtSZhQ2I 6oE/NNb4rLIakSAwxrWFIQqPYxrRfPD8Bbt/BAC1edUZYHkqF+NUq28JjZTnrx48+o+K 3B8wK8T6XeG4Q9LETE0AgX+WZL3xjWSioIoYpkhk2JLOXkgcxlDWy3RnfmIf4LLhtaXx n/hvTUEazVcVfk91GurJxHqbOZ+qJ3kUTqByx9CD9VdlUg3OTkNpevvF8bsXsojsn9mR l+Rw== X-Gm-Message-State: AOAM5335wLunD6a+f0QVCQIvf9hiLtwXt1KzNy+dlYkfLabIzVbeVtnN +ZB329aOObwzuaqpetC8121D739nK5fJwg== X-Google-Smtp-Source: ABdhPJwyUy4Jo/BTnCb0JhLqpg09FhUNjO+iaLnQ3tedt1ajBu0jM+jX51tHrNXxztoQNel26+h6+g== X-Received: by 2002:a1c:e906:: with SMTP id q6mr30961498wmc.126.1634926785835; Fri, 22 Oct 2021 11:19:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:45 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 5/6] config.c: free(expanded) before die(), work around GCC oddity Date: Fri, 22 Oct 2021 20:19:38 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org On my GCC version (10.2.1-6), but not the clang I have available t0017 will fail under SANITIZE=leak on optimization levels higher than -O0, which is annoying when combined with the change in 956d2e4639b (tests: add a test mode for SANITIZE=leak, run it in CI, 2021-09-23). We really do have a memory leak here in either case, as e.g. running the pre-image under valgrind(1) will reveal. It's documented SANITIZE=leak (and "address", which exhibits the same behavior) might interact with compiler optimization in this way in some cases. Since this function is called recursively it's going to be especially interesting as an optimization target. Let's work around this issue by freeing the "expanded" memory before we call die(), using a combination of the "goto cleanup" pattern introduced in a preceding commit, and the newly introduced die_message() function. Signed-off-by: Ævar Arnfjörð Bjarmason --- config.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index c5873f3a706..c36e85c2077 100644 --- a/config.c +++ b/config.c @@ -132,6 +132,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc int ret = 0; struct strbuf buf = STRBUF_INIT; char *expanded; + int exit_with = 0; if (!path) return config_error_nonbool("include.path"); @@ -161,17 +162,21 @@ static int handle_path_include(const char *path, struct config_include_data *inc } if (!access_or_die(path, R_OK, 0)) { - if (++inc->depth > MAX_INCLUDE_DEPTH) - die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path, - !cf ? "" : - cf->name ? cf->name : - "the command line"); + if (++inc->depth > MAX_INCLUDE_DEPTH) { + exit_with = die_message(_(include_depth_advice), + MAX_INCLUDE_DEPTH, path, + !cf ? "" : cf->name ? + cf->name : "the command line"); + goto cleanup; + } ret = git_config_from_file(git_config_include, path, inc); inc->depth--; } cleanup: strbuf_release(&buf); free(expanded); + if (exit_with) + exit(exit_with); return ret; } From patchwork Fri Oct 22 18:19:39 2021 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: 12578395 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5EAAC433FE for ; Fri, 22 Oct 2021 18:19:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AEB9B61181 for ; Fri, 22 Oct 2021 18:19:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233939AbhJVSWN (ORCPT ); Fri, 22 Oct 2021 14:22:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233794AbhJVSWG (ORCPT ); Fri, 22 Oct 2021 14:22:06 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34BE7C061348 for ; Fri, 22 Oct 2021 11:19:48 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id v127so3674828wme.5 for ; Fri, 22 Oct 2021 11:19:48 -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=PMLIvTHv2DlatvET7viPvieVl+TCDPn5XNdiVULZneE=; b=lEfkCYe9iMenEWEyTZ07h8ysa1HSdhGeb4j8Q4hskiRFa4GZqBhKk5C9FO/zrRbYHA Vzt7ESnN2FQFp6t8R8Z8SfM2N4Vyk6+Qx0Y2LO5Hu97nvh2WY48GGSKLJ6Urppzv48Tg EWZmBo+/0AemEuy/UTO47xxdYQF6tTxkJggttjLLV5lwmriqx1lQA9tRtr3V6r8XQ5Nw abWqOoon84Wjt7rHSNyD71eh9Tjp8EPf1zoByBUgeEMTq87/3IhPBNl9NrwW9vfdlToU qXDlybmnGgCd5R5uQ5I5LxC8TQs1FHfqZN48U/2kBRUslO8XEIsPqQcFa73xzKdGNUXn YEvA== 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=PMLIvTHv2DlatvET7viPvieVl+TCDPn5XNdiVULZneE=; b=bozUkI0n7LBFAFnV+bfhTl9mSVr85dnQ5i7fov2IuC4sPP2TnQVCUFEMSssQAZf7wy PXQ5TMIyRsCEPiBvoBR3QmGmpBhS7rgG2JbzPWYtRYfCfHozPSdmQDvYjzBMUgzJmpJN HU5vobBNKVuqn0twOy1Ezq44gENI9S32UMfdKAaNwKd4VpK29IQU5OxtGvC1LqPiVjzL tYtdQGD32TBU/BobgGNQFBJiD7jgJJlJYA2B3rdcztWHvmOV48AqLg1SLZ0R92oC4H5M e6/QOlznuE3H1B5oRl9v7BOm6+hotHipHgSEaK4GvdjXiTJFLto6nbZeWITepBezNTKp hMrg== X-Gm-Message-State: AOAM5332gzKf5n9bHk4L9vNwzF14X3lUG+Jhk+F0npUM4+YLo4Ps+PGU xBxalXaVUoJB7dO6Rml30VnP0JEj4OGbyw== X-Google-Smtp-Source: ABdhPJyqcAVdq5sj2B/IQFzyj1XLSdbu3P1vuAYKPhn6quIBynXUUvwBXdXfLF+QTbPq6rzM3gAQSA== X-Received: by 2002:a05:600c:4e94:: with SMTP id f20mr30868007wmq.52.1634926786602; Fri, 22 Oct 2021 11:19:46 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id 3sm8587242wms.5.2021.10.22.11.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Oct 2021 11:19:46 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Andrzej Hunt , =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?b?w4Z2YXIg?= =?utf-8?b?QXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 6/6] refs: plug memory leak in repo_default_branch_name() Date: Fri, 22 Oct 2021 20:19:39 +0200 Message-Id: X-Mailer: git-send-email 2.33.1.1505.g075a284c562 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a memory leak in repo_default_branch_name(), we'll leak memory before exit(128) here. Normally we would not care much about such leaks, we do leak the memory, as e.g. valgrind(1) will report. But the more commonly used SANITIZE=leak mode will use GCC and Clang's LSAN mode will not normally report such leaks. At least one GCC version does that in this case, and having the tests fail under -O3 would be annoying, so let's free() the allocated memory here. This uses a new die_message() function introduced in a preceding commit. That new function makes the flow around such code easier to manage. In this case we can't free(ret) before the die(). In this case only the "free(full_ref)" appears to be needed, but since we're freeing one let's free both, some other compiler or version might arrange this code in such a way as to complain about "ret" too with SANITIZE=leak, and valgrind(1) will do so in any case. Signed-off-by: Ævar Arnfjörð Bjarmason --- refs.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/refs.c b/refs.c index 7f019c2377e..2a816c9561d 100644 --- a/refs.c +++ b/refs.c @@ -577,6 +577,7 @@ char *repo_default_branch_name(struct repository *r, int quiet) const char *config_display_key = "init.defaultBranch"; char *ret = NULL, *full_ref; const char *env = getenv("GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME"); + int exit_with = 0; if (env && *env) ret = xstrdup(env); @@ -591,8 +592,13 @@ char *repo_default_branch_name(struct repository *r, int quiet) full_ref = xstrfmt("refs/heads/%s", ret); if (check_refname_format(full_ref, 0)) - die(_("invalid branch name: %s = %s"), config_display_key, ret); + exit_with = die_message(_("invalid branch name: %s = %s"), + config_display_key, ret); free(full_ref); + if (exit_with) { + free(ret); + exit(exit_with); + } return ret; }