From patchwork Thu Jun 2 12:25:32 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: 12867815 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 39E58C43334 for ; Thu, 2 Jun 2022 12:25:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234811AbiFBMZp (ORCPT ); Thu, 2 Jun 2022 08:25:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233328AbiFBMZo (ORCPT ); Thu, 2 Jun 2022 08:25:44 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10E5913274F for ; Thu, 2 Jun 2022 05:25:43 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id q7so6260606wrg.5 for ; Thu, 02 Jun 2022 05:25:42 -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=1Xb9Rcsd9pK9CK+PbUe3Q1mgSDjc8KrZVyHGiblOA2A=; b=GJWe71ealkt2HESaL5dWSbtOxdXR5+EkJ/wCTVfXKR2R15aFrxlrfn9KJkPBzN5LmP u3sC9p3+89M3uVRF4YMB2ZD30s6HbrhKJ3XzYOMGg1fmz3/QjFsOOkTARMb/XlJCT1q0 Ar17Mu/Bs0HGc2EPb0vF8beVWk7bIMYgVaoSaBDHcsyBpfbArm5sDtVdZ3nFH2m4b50u COzt+03wLb7FGG5blln7xFWrXd/n8KIecUW9qxcnYJuPHwFi5uAwEpaMyIisrlbBGbDv a8KXSkIbEDeiVAq4eeLdicdrdDjWFIqdvmGjsmC5o8pqspgsFivuws5RjACa17qpwMMx y50w== 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=1Xb9Rcsd9pK9CK+PbUe3Q1mgSDjc8KrZVyHGiblOA2A=; b=XpRKKQiivpeTQUeSrjMa9YfaxtPG8daUnTTeYsZ5lt+SJTF5Pfdhkoco799ZzAmxoe UHD1BuIa3y3vvi3Q71hg9AAOffOj465hCcE3GQBsTtzfgOtwompkEUJRdKMQBEaAtXJM JvzXuFSKWV0aKFl9PA2fpovbA0/h4M2CEdGFtC9v3LGIknpTOXKGT+gt7PuBbvskXf+T LuE5wIUFCSTQarZPq0PN0PXgoCzil8rqmRgJ6I5gk1+5jefhIP3SRdnXILfOySVhbutO FT7PDIeEQYEFLafC8rejNe0y281SyZKq23QhfWj4hMiE6MrOBlnmCTpNDaOo5crJ2i+b sKpg== X-Gm-Message-State: AOAM53033fWIS3ohScibYlSQ8tUC0opYUqsscekkFRCIPUGoTGiRZQzR 6JJ3IXfDzT3BrTghJCG21JcT12+lZchIeA== X-Google-Smtp-Source: ABdhPJzLbmaunZkAwkq14vZTl3iR4mJbbOfYITxyjOPCo6lg/jSSuo8yxsct0jOi/aUn7W+P7vp/Nw== X-Received: by 2002:a5d:588d:0:b0:20e:e4f0:e3 with SMTP id n13-20020a5d588d000000b0020ee4f000e3mr3505942wrf.87.1654172741128; Thu, 02 Jun 2022 05:25:41 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:40 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 1/6] common-main.o: move non-trace2 exit() behavior out of trace2.c Date: Thu, 2 Jun 2022 14:25:32 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the exit() wrapper added in ee4512ed481 (trace2: create new combined trace facility, 2019-02-22) so that we'll split up the trace2 logging concerns from wanting to wrap the "exit()" function itself for other purposes. This makes more sense structurally, as we won't seem to conflate non-trace2 behavior with the trace2 code. I'd previously added an explanation for this in 368b5843158 (common-main.c: call exit(), don't return, 2021-12-07), that comment is being adjusted here. Now the only thing we'll do if we're not using trace2 is to truncate the "code" argument to the lowest 8 bits. We only need to do that truncation on non-POSIX systems, but in ee4512ed481 that "if defined(__MINGW32__)" code added in 47e3de0e796 (MinGW: truncate exit()'s argument to lowest 8 bits, 2009-07-05) was made to run everywhere. It might be good for clarify to narrow that down by an "ifdef" again, but I'm not certain that in the interim we haven't had some other non-POSIX systems rely the behavior. On a POSIX system taking the lowest 8 bits is implicit, see exit(3)[1] and wait(2)[2]. Let's leave a comment about that instead. 1. https://man7.org/linux/man-pages/man3/exit.3.html 2. https://man7.org/linux/man-pages/man2/wait.2.html Signed-off-by: Ævar Arnfjörð Bjarmason --- common-main.c | 20 ++++++++++++++++---- git-compat-util.h | 4 ++-- trace2.c | 8 ++------ trace2.h | 8 +------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/common-main.c b/common-main.c index 29fb7452f8a..b6124dd2c2c 100644 --- a/common-main.c +++ b/common-main.c @@ -55,10 +55,22 @@ int main(int argc, const char **argv) result = cmd_main(argc, argv); + /* Not exit(3), but a wrapper calling our common_exit() */ + exit(result); +} + +/* We wrap exit() to call common_exit() in git-compat-util.h */ +int common_exit(const char *file, int line, int code) +{ /* - * We define exit() to call trace2_cmd_exit_fl() in - * git-compat-util.h. Whether we reach this or exit() - * elsewhere we'll always run our trace2 exit handler. + * For non-POSIX systems: Take the lowest 8 bits of the "code" + * to e.g. turn -1 into 255. On a POSIX system this is + * redundant, see exit(3) and wait(2), but as it doesn't harm + * anything there we don't need to guard this with an "ifdef". */ - exit(result); + code &= 0xff; + + trace2_cmd_exit_fl(file, line, code); + + return code; } diff --git a/git-compat-util.h b/git-compat-util.h index 96293b6c43a..1227ff80ca7 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1451,8 +1451,8 @@ int cmd_main(int, const char **); * Intercept all calls to exit() and route them to trace2 to * optionally emit a message before calling the real exit(). */ -int trace2_cmd_exit_fl(const char *file, int line, int code); -#define exit(code) exit(trace2_cmd_exit_fl(__FILE__, __LINE__, (code))) +int common_exit(const char *file, int line, int code); +#define exit(code) exit(common_exit(__FILE__, __LINE__, (code))) /* * You can mark a stack variable with UNLEAK(var) to avoid it being diff --git a/trace2.c b/trace2.c index e01cf77f1a8..0c0a11e07d5 100644 --- a/trace2.c +++ b/trace2.c @@ -202,17 +202,15 @@ void trace2_cmd_start_fl(const char *file, int line, const char **argv) argv); } -int trace2_cmd_exit_fl(const char *file, int line, int code) +void trace2_cmd_exit_fl(const char *file, int line, int code) { struct tr2_tgt *tgt_j; int j; uint64_t us_now; uint64_t us_elapsed_absolute; - code &= 0xff; - if (!trace2_enabled) - return code; + return; trace_git_fsync_stats(); trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT); @@ -226,8 +224,6 @@ int trace2_cmd_exit_fl(const char *file, int line, int code) if (tgt_j->pfn_exit_fl) tgt_j->pfn_exit_fl(file, line, us_elapsed_absolute, code); - - return code; } void trace2_cmd_error_va_fl(const char *file, int line, const char *fmt, diff --git a/trace2.h b/trace2.h index 1b109f57d0a..88d906ea830 100644 --- a/trace2.h +++ b/trace2.h @@ -101,14 +101,8 @@ void trace2_cmd_start_fl(const char *file, int line, const char **argv); /* * Emit an 'exit' event. - * - * Write the exit-code that will be passed to exit() or returned - * from main(). - * - * Use this prior to actually calling exit(). - * See "#define exit()" in git-compat-util.h */ -int trace2_cmd_exit_fl(const char *file, int line, int code); +void trace2_cmd_exit_fl(const char *file, int line, int code); #define trace2_cmd_exit(code) (trace2_cmd_exit_fl(__FILE__, __LINE__, (code))) From patchwork Thu Jun 2 12:25:33 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: 12867818 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 1B997CCA478 for ; Thu, 2 Jun 2022 12:25:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234969AbiFBMZv (ORCPT ); Thu, 2 Jun 2022 08:25:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234768AbiFBMZp (ORCPT ); Thu, 2 Jun 2022 08:25:45 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 267351021D8 for ; Thu, 2 Jun 2022 05:25:44 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id k16so6264412wrg.7 for ; Thu, 02 Jun 2022 05:25: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=nYcrM2+rQj363VFiHNHfuhLYsb6Xz6UT/FeB0q/b5Co=; b=S67x8DFTYdPbSo9F0I/6ujIeWwcReLkCZXlBC4wKxp0ICWx7VeH9VxFR/1ZFQQcgXB 2LtpHC87Axwl8/nW3LW/W27DtSWQMYGNH64hxkujgxk61Xie5wNPR1xlFFh3+lA7S9Qf lRH4ZT1kUDzycOhtLXlEuzm8sBTHRkWx0y+TwIrAlTxhPEAK28uWhsabH++LO6iqi/rY bh019LsPfo90s9epEhhnTWHzrqYTDmcj0K/bupsOq/ma9nADzrLoymKGjwujl22UUM/9 BpX3vUbjL77PYaF9Yna/o+sWbc9RguyrvSm76N6BHTpiXiVH8C1Ij8Y0M8MGNL6vASCe /Zzw== 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=nYcrM2+rQj363VFiHNHfuhLYsb6Xz6UT/FeB0q/b5Co=; b=iGtHFLNKEDhGk9LAg4wnPojfU9o6eKvD0R7t7DOnQMSsPcZiDe7ns/UBW8tmNGDZoA WHkq8XuPTRxg45DW6wYy1cFS157rhWmqkA7EzryWcE8oKGyAvekVsFPMjwAuAjlZ9bhk YeautOCr+lzObGeGe/jUt89siAsnApXL+e5Eo/O9peCmaCA71VvYqxmksbDscOV25ZLX ra5NFQRURV3FSc69L/0P6Y5cAXZTeZr7xKea5jnAuTLKt3MqQ6nCoWcBgVtW/R3HbnYH ITPp1utXeVTZ8e/g6JaYJQrWETa274igOmGwp5cWvdoUl5Nn5E5Pl2xTyZcylFQ+7Jlo fepA== X-Gm-Message-State: AOAM531HjCI0LHYVxbY0WAUDrSVqLauRzl1eqY2COHJhMbyr4c0kk+E2 TFvxAWGJPKhiInoJV4gdE7YdqGIUx0jMpg== X-Google-Smtp-Source: ABdhPJxUbsvlm4XVKZFgSa4ogGdIVfC0VKPz6kaP8pumi5FVr10FJsyZOQmA7GL3Qy11vQnpBihrFg== X-Received: by 2002:a5d:4906:0:b0:20f:fc2a:987a with SMTP id x6-20020a5d4906000000b0020ffc2a987amr3446477wrq.228.1654172742351; Thu, 02 Jun 2022 05:25:42 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:41 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 2/6] usage.c: add a non-fatal bug() function to go with BUG() Date: Thu, 2 Jun 2022 14:25:33 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a bug() function to use in cases where we'd like to indicate a runtime BUG(), but would like to defer the BUG() call because we're possibly accumulating more bug() callers to exhaustively indicate what went wrong. We already have this sort of facility in various parts of the codebase, just in the form of ad-hoc re-inventions of the functionality that this new API provides. E.g. this will be used to replace optbug() in parse-options.c, and the 'error("BUG:[...]' we do in a loop in builtin/receive-pack.c. Unlike the code this replaces we'll log to trace2 with this new bug() function (as with other usage.c functions, including BUG()), we'll also be able to avoid calls to xstrfmt() in some cases, as the bug() function itself accepts variadic sprintf()-like arguments. Any caller to bug() can follow up such calls with BUG_if_bug(), which will BUG() out (i.e. abort()) if there were any preceding calls to bug(), callers can also decide not to call BUG_if_bug() and leave the resulting BUG() invocation until exit() time. There are currently no bug() API users that don't call BUG_if_bug() themselves after a for-loop, but allowing for not calling BUG_if_bug() keeps the API flexible. As the tests and documentation here show we'll catch missing BUG_if_bug() invocations in our exit() wrapper. Signed-off-by: Ævar Arnfjörð Bjarmason --- .../technical/api-error-handling.txt | 24 +++++- Documentation/technical/api-trace2.txt | 4 +- common-main.c | 8 ++ git-compat-util.h | 10 +++ t/helper/test-trace2.c | 29 ++++++- t/t0210-trace2-normal.sh | 76 +++++++++++++++++++ usage.c | 33 ++++++-- 7 files changed, 174 insertions(+), 10 deletions(-) diff --git a/Documentation/technical/api-error-handling.txt b/Documentation/technical/api-error-handling.txt index 8be4f4d0d6a..70bf1d3e522 100644 --- a/Documentation/technical/api-error-handling.txt +++ b/Documentation/technical/api-error-handling.txt @@ -1,12 +1,34 @@ Error reporting in git ====================== -`BUG`, `die`, `usage`, `error`, and `warning` report errors of +`BUG`, `bug`, `die`, `usage`, `error`, and `warning` report errors of various kinds. - `BUG` is for failed internal assertions that should never happen, i.e. a bug in git itself. +- `bug` (lower-case, not `BUG`) is supposed to be used like `BUG` but + prints a "BUG" message instead of calling `abort()`. ++ +A call to `bug()` will then result in a "real" call to the `BUG()` +function, either explicitly by invoking `BUG_if_bug()` after call(s) +to `bug()`, or implicitly at `exit()` time where we'll check if we +encountered any outstanding `bug()` invocations. ++ +If there were no prior calls to `bug()` before invoking `BUG_if_bug()` +the latter is a NOOP. The `BUG_if_bug()` function takes the same +arguments as `BUG()` itself. Calling `BUG_if_bug()` explicitly isn't +necessary, but ensures that we die as soon as possible. ++ +If you know you had prior calls to `bug()` then calling `BUG()` itself +is equivalent to calling `BUG_if_bug()`, the latter being a wrapper +calling `BUG()` if we've set a flag indicating that we've called +`bug()`. ++ +This is for the convenience of APIs who'd like to potentially report +more than one "bug", such as the optbug() validation in +parse-options.c. + - `die` is for fatal application errors. It prints a message to the user and exits with status 128. diff --git a/Documentation/technical/api-trace2.txt b/Documentation/technical/api-trace2.txt index f4a8a690878..77a150b30ee 100644 --- a/Documentation/technical/api-trace2.txt +++ b/Documentation/technical/api-trace2.txt @@ -465,8 +465,8 @@ completed.) ------------ `"error"`:: - This event is emitted when one of the `BUG()`, `error()`, `die()`, - `warning()`, or `usage()` functions are called. + This event is emitted when one of the `BUG()`, `bug()`, `error()`, + `die()`, `warning()`, or `usage()` functions are called. + ------------ { diff --git a/common-main.c b/common-main.c index b6124dd2c2c..c531372f3ff 100644 --- a/common-main.c +++ b/common-main.c @@ -59,6 +59,13 @@ int main(int argc, const char **argv) exit(result); } +static void check_bug_if_BUG(void) +{ + if (!bug_called_must_BUG) + return; + BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()"); +} + /* We wrap exit() to call common_exit() in git-compat-util.h */ int common_exit(const char *file, int line, int code) { @@ -70,6 +77,7 @@ int common_exit(const char *file, int line, int code) */ code &= 0xff; + check_bug_if_BUG(); trace2_cmd_exit_fl(file, line, code); return code; diff --git a/git-compat-util.h b/git-compat-util.h index 1227ff80ca7..ce007102f76 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1320,9 +1320,19 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size, /* usage.c: only to be used for testing BUG() implementation (see test-tool) */ extern int BUG_exit_code; +/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */ +extern int bug_called_must_BUG; + __attribute__((format (printf, 3, 4))) NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...); #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__) +__attribute__((format (printf, 3, 4))) +void bug_fl(const char *file, int line, const char *fmt, ...); +#define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__) +#define BUG_if_bug(...) do { \ + if (bug_called_must_BUG) \ + BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \ +} while (0) #ifdef __APPLE__ #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c index 59b124bb5f1..180c7f53f31 100644 --- a/t/helper/test-trace2.c +++ b/t/helper/test-trace2.c @@ -198,7 +198,7 @@ static int ut_006data(int argc, const char **argv) return 0; } -static int ut_007bug(int argc, const char **argv) +static int ut_007BUG(int argc, const char **argv) { /* * Exercise BUG() to ensure that the message is printed to trace2. @@ -206,6 +206,28 @@ static int ut_007bug(int argc, const char **argv) BUG("the bug message"); } +static int ut_008bug(int argc, const char **argv) +{ + bug("a bug message"); + bug("another bug message"); + BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required"); + return 0; +} + +static int ut_009bug_BUG(int argc, const char **argv) +{ + bug("a bug message"); + bug("another bug message"); + /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */ + return 0; +} + +static int ut_010bug_BUG(int argc, const char **argv) +{ + bug("a bug message"); + BUG("a BUG message"); +} + /* * Usage: * test-tool trace2 @@ -222,7 +244,10 @@ static struct unit_test ut_table[] = { { ut_004child, "004child", "[]" }, { ut_005exec, "005exec", "" }, { ut_006data, "006data", "[ ]+" }, - { ut_007bug, "007bug", "" }, + { ut_007BUG, "007bug", "" }, + { ut_008bug, "008bug", "" }, + { ut_009bug_BUG, "009bug_BUG","" }, + { ut_010bug_BUG, "010bug_BUG","" }, }; /* clang-format on */ diff --git a/t/t0210-trace2-normal.sh b/t/t0210-trace2-normal.sh index 37c359bd5a2..80e76a4695e 100755 --- a/t/t0210-trace2-normal.sh +++ b/t/t0210-trace2-normal.sh @@ -168,6 +168,82 @@ test_expect_success 'BUG messages are written to trace2' ' test_cmp expect actual ' +test_expect_success 'bug messages with BUG_if_bug() are written to trace2' ' + test_when_finished "rm trace.normal actual expect" && + test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \ + test-tool trace2 008bug 2>err && + cat >expect <<-\EOF && + a bug message + another bug message + an explicit BUG_if_bug() following bug() call(s) is nice, but not required + EOF + sed "s/^.*: //" actual && + test_cmp expect actual && + + perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" actual && + cat >expect <<-EOF && + version $V + start _EXE_ trace2 008bug + cmd_name trace2 (trace2) + error a bug message + error another bug message + error an explicit BUG_if_bug() following bug() call(s) is nice, but not required + exit elapsed:_TIME_ code:99 + atexit elapsed:_TIME_ code:99 + EOF + test_cmp expect actual +' + +test_expect_success 'bug messages without explicit BUG_if_bug() are written to trace2' ' + test_when_finished "rm trace.normal actual expect" && + test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \ + test-tool trace2 009bug_BUG 2>err && + cat >expect <<-\EOF && + a bug message + another bug message + had bug() call(s) in this process without explicit BUG_if_bug() + EOF + sed "s/^.*: //" actual && + test_cmp expect actual && + + perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" actual && + cat >expect <<-EOF && + version $V + start _EXE_ trace2 009bug_BUG + cmd_name trace2 (trace2) + error a bug message + error another bug message + error on exit(): had bug() call(s) in this process without explicit BUG_if_bug() + exit elapsed:_TIME_ code:99 + atexit elapsed:_TIME_ code:99 + EOF + test_cmp expect actual +' + +test_expect_success 'bug messages followed by BUG() are written to trace2' ' + test_when_finished "rm trace.normal actual expect" && + test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \ + test-tool trace2 010bug_BUG 2>err && + cat >expect <<-\EOF && + a bug message + a BUG message + EOF + sed "s/^.*: //" actual && + test_cmp expect actual && + + perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" actual && + cat >expect <<-EOF && + version $V + start _EXE_ trace2 010bug_BUG + cmd_name trace2 (trace2) + error a bug message + error a BUG message + exit elapsed:_TIME_ code:99 + atexit elapsed:_TIME_ code:99 + EOF + test_cmp expect actual +' + sane_unset GIT_TRACE2_BRIEF # Now test without environment variables and get all Trace2 settings diff --git a/usage.c b/usage.c index b738dd178b3..79900d0287f 100644 --- a/usage.c +++ b/usage.c @@ -290,18 +290,24 @@ void warning(const char *warn, ...) /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */ int BUG_exit_code; -static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) +static void BUG_vfl_common(const char *file, int line, const char *fmt, + va_list params) { char prefix[256]; - va_list params_copy; - static int in_bug; - - va_copy(params_copy, params); /* truncation via snprintf is OK here */ snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line); vreportf(prefix, fmt, params); +} + +static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params) +{ + va_list params_copy; + static int in_bug; + + va_copy(params_copy, params); + BUG_vfl_common(file, line, fmt, params); if (in_bug) abort(); @@ -317,11 +323,28 @@ static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_lis NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...) { va_list ap; + + bug_called_must_BUG = 0; + va_start(ap, fmt); BUG_vfl(file, line, fmt, ap); va_end(ap); } +int bug_called_must_BUG; +void bug_fl(const char *file, int line, const char *fmt, ...) +{ + va_list ap, cp; + + bug_called_must_BUG = 1; + + va_copy(cp, ap); + va_start(ap, fmt); + BUG_vfl_common(file, line, fmt, ap); + va_end(ap); + trace2_cmd_error_va(fmt, cp); +} + #ifdef SUPPRESS_ANNOTATED_LEAKS void unleak_memory(const void *ptr, size_t len) { From patchwork Thu Jun 2 12:25:34 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: 12867817 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 5752EC433EF for ; Thu, 2 Jun 2022 12:25:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234966AbiFBMZu (ORCPT ); Thu, 2 Jun 2022 08:25:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232880AbiFBMZq (ORCPT ); Thu, 2 Jun 2022 08:25:46 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49E811021EE for ; Thu, 2 Jun 2022 05:25:45 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id p10so6243065wrg.12 for ; Thu, 02 Jun 2022 05:25: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=iN4T6crDo24ZXbBVpTBnPv/JI3comcXsGR9aPYOIN4E=; b=H/GRJGumZlC13Rz+H8ssQ/Ks0es6KtkzpLJ+d1xaVNtvAv0WgZxpSccPDgxd7R449D CWaN3Lc6mFNxfaJTIfEbv17cWIp4KU+V+qdb+UQTIOIt/7E7k3RssjqJrbW1DID42w3Q 2tJdqU556Xc2Gp3LR/VvGopo9+OQuIhPIXlhsskspoFstO6jPIPLCakEySuScH3SLqJm NPURkIc90YpjsqZ4uOnfr4IX2b8vVRSj20MVKJP4qLo1XWeMRPYW5sMnPBiwMnEUmlFn 91cFTxcD73NDz7A6DFDCFNfH3y3C1JP/xNG8egdM0nuVQJeCYxMxmLIqb54EJ/JyWInz s43Q== 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=iN4T6crDo24ZXbBVpTBnPv/JI3comcXsGR9aPYOIN4E=; b=SONqXg/pBbLntVetoS953Zz6ycoaeEdO/o8HwTGysdSXAfSDSssm5SFGWzNnjcP7yA lkcAeqrMil53rAn6g8vW4f7WFk0eBwfJ15t681Xp0KawY/hALmVQJU0i+AA6/FokkCcV FpBVfLgiitApZ32g48KUlOE8k0Z0LH7wFiOnxg3jlIMGU85iof0bul4FvSJRc/vjbUfO C7R2k2QJFAZXKS2n4QmbezTcvDKz8Um6/DH/FAG2ElEzAccVjzl86QA4XG4R0mFE+j3b xnWoPJPJ7fLkcwoqTd5IKjXuK8lKHsAsc8FjABULepT9XcwiueGEPW5TxU4K3a71dqpc 4mhA== X-Gm-Message-State: AOAM533F6GxT4TeHrJ8iVDjgSJZ+2GU2R363bU2R+FuYYVRXNUZbTAT3 D5CzVEosJpo85GcDU9DHebU326ix6/AEzQ== X-Google-Smtp-Source: ABdhPJz83VdRJLGPJ+8B3WqoR3Rb8tDpzISzNYtG8DAsgaiuT6KLBRLxISCzYxty432gkHnV7DkgnQ== X-Received: by 2002:a5d:6c64:0:b0:20f:f413:8af8 with SMTP id r4-20020a5d6c64000000b0020ff4138af8mr3456005wrz.129.1654172743613; Thu, 02 Jun 2022 05:25:43 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:42 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 3/6] parse-options.c: use new bug() API for optbug() Date: Thu, 2 Jun 2022 14:25:34 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we run into bugs in parse-options.c usage it's good to be able to note all the issues we ran into before dying. This use-case is why we have the optbug() function introduced in 1e5ce570ca3 (parse-options: clearer reporting of API misuse, 2010-12-02) Let's change this code to use the new bug() API introduced in the preceding commit, which cuts down on the verbosity of parse_options_check(). There are existing uses of BUG() in adjacent code that should have been using optbug() that aren't being changed here. That'll be done in a subsequent commit. This only changes the optbug() callers. Since this will invoke BUG() the previous exit(128) code will be changed, but in this case that's what we want, i.e. to have encountering a BUG() return the specific "BUG" exit code. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/parse-options.c b/parse-options.c index 6e57744fd22..78b46ae9698 100644 --- a/parse-options.c +++ b/parse-options.c @@ -14,15 +14,15 @@ enum opt_parsed { OPT_UNSET = 1<<1, }; -static int optbug(const struct option *opt, const char *reason) +static void optbug(const struct option *opt, const char *reason) { - if (opt->long_name) { - if (opt->short_name) - return error("BUG: switch '%c' (--%s) %s", - opt->short_name, opt->long_name, reason); - return error("BUG: option '%s' %s", opt->long_name, reason); - } - return error("BUG: switch '%c' %s", opt->short_name, reason); + if (opt->long_name && opt->short_name) + bug("switch '%c' (--%s) %s", opt->short_name, + opt->long_name, reason); + else if (opt->long_name) + bug("option '%s' %s", opt->long_name, reason); + else + bug("switch '%c' %s", opt->short_name, reason); } static const char *optname(const struct option *opt, enum opt_parsed flags) @@ -441,28 +441,27 @@ static void check_typos(const char *arg, const struct option *options) static void parse_options_check(const struct option *opts) { - int err = 0; char short_opts[128]; memset(short_opts, '\0', sizeof(short_opts)); for (; opts->type != OPTION_END; opts++) { if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) && (opts->flags & PARSE_OPT_OPTARG)) - err |= optbug(opts, "uses incompatible flags " - "LASTARG_DEFAULT and OPTARG"); + optbug(opts, "uses incompatible flags " + "LASTARG_DEFAULT and OPTARG"); if (opts->short_name) { if (0x7F <= opts->short_name) - err |= optbug(opts, "invalid short name"); + optbug(opts, "invalid short name"); else if (short_opts[opts->short_name]++) - err |= optbug(opts, "short name already used"); + optbug(opts, "short name already used"); } if (opts->flags & PARSE_OPT_NODASH && ((opts->flags & PARSE_OPT_OPTARG) || !(opts->flags & PARSE_OPT_NOARG) || !(opts->flags & PARSE_OPT_NONEG) || opts->long_name)) - err |= optbug(opts, "uses feature " - "not supported for dashless options"); + optbug(opts, "uses feature " + "not supported for dashless options"); switch (opts->type) { case OPTION_COUNTUP: case OPTION_BIT: @@ -471,7 +470,7 @@ static void parse_options_check(const struct option *opts) case OPTION_NUMBER: if ((opts->flags & PARSE_OPT_OPTARG) || !(opts->flags & PARSE_OPT_NOARG)) - err |= optbug(opts, "should not accept an argument"); + optbug(opts, "should not accept an argument"); break; case OPTION_CALLBACK: if (!opts->callback && !opts->ll_callback) @@ -494,10 +493,9 @@ static void parse_options_check(const struct option *opts) } if (opts->argh && strcspn(opts->argh, " _") != strlen(opts->argh)) - err |= optbug(opts, "multi-word argh should use dash to separate words"); + optbug(opts, "multi-word argh should use dash to separate words"); } - if (err) - exit(128); + BUG_if_bug("invalid 'struct option'"); } static void parse_options_start_1(struct parse_opt_ctx_t *ctx, From patchwork Thu Jun 2 12:25:35 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: 12867820 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 CC06AC433EF for ; Thu, 2 Jun 2022 12:26:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234981AbiFBM0A (ORCPT ); Thu, 2 Jun 2022 08:26:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234946AbiFBMZr (ORCPT ); Thu, 2 Jun 2022 08:25:47 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D314132A1F for ; Thu, 2 Jun 2022 05:25:46 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id k16so6264522wrg.7 for ; Thu, 02 Jun 2022 05:25: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=3XWtRhtFL273UXa0MGijePi+vbWbLXE4pvSbgYoY8FA=; b=Kqa3JRgMuAkefKqW4UHl4sypFiiGvL9i54yI62O/Lm0/2qTfSGxLuG2NL5yjN7Dw17 9zHa68Kracg0I7QelzMC/VPIro1IYnhHbIk2nuFgOgiq5wqCjDf/I4uAAkNc0+MM11Ig S/XEQ3dcLCKRAjZ8K918ms5KZTGZURWRdXYFvg0aUNaHMqoBd4Ag+LcC2uIPjHjA62w0 VWRGS3MKRjgiAmmzi3D4XWSHTD6vvkwyk2AE7/Lg54DZsm5XdN+s41Os99KoNMB6IVla mQcxFKYBBGiTceYW0ZTOgP+QjFL51KLPb1++H0p6giYsWvIHqY5KaLjTh77P9+C5RD6R jJMQ== 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=3XWtRhtFL273UXa0MGijePi+vbWbLXE4pvSbgYoY8FA=; b=D44wd8YmobMiEHzBXAdfMKaooZikQ/SYNZjA7t3HL3Cgycfe4LGlPrRQPH8H4o3P/P v1xvNxuiYHZ/lzJ/Aibn3X/+yg4QX5wB/q1lxLQDgVbNcJWyNTzq1ivQwfdGVZV7mElf hC/E7w7QiZWxrotO+Y7cqTnH9jmUU+dWieJOJ6VnSm04WBn5CDK/obUixuiGcE97G5nH 7EiCmFhWStEHDNtqC/kaes1LABRGcyKfW00qLkR/oJvt+GMRC6kajulk4n0ozbFgcWE4 Z9rRzJsx806f9bIf8oiD7xPXbAqNxvQHfgD//8zJhDVyqxkLyDtYvvKoqnyDBQKzh3/e Bb2w== X-Gm-Message-State: AOAM530nHncEHIrSE/VyEq0vbFmqwSuc3kTAzD3g1i5EXZsmpmHELaSM 6bTIwR2ApZs/+/6u1x+r2wekss/VIoO16A== X-Google-Smtp-Source: ABdhPJzFGuNUO4uNiLg3wucKMarKbw80UH+ghnYQ2c/lqtnKfsXPzSJjpEHv06tK7C6QUrHvAghbHA== X-Received: by 2002:adf:e19a:0:b0:210:157c:7b3c with SMTP id az26-20020adfe19a000000b00210157c7b3cmr3433249wrb.121.1654172744806; Thu, 02 Jun 2022 05:25:44 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:44 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 4/6] parse-options.c: use optbug() instead of BUG() "opts" check Date: Thu, 2 Jun 2022 14:25:35 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the assertions added in bf3ff338a25 (parse-options: stop abusing 'callback' for lowlevel callbacks, 2019-01-27) to use optbug() instead of BUG(). At this point we're looping over individual options, so if we encounter any issues we'd like to report the offending option. Signed-off-by: Ævar Arnfjörð Bjarmason --- parse-options.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/parse-options.c b/parse-options.c index 78b46ae9698..edf55d3ef5d 100644 --- a/parse-options.c +++ b/parse-options.c @@ -474,20 +474,21 @@ static void parse_options_check(const struct option *opts) break; case OPTION_CALLBACK: if (!opts->callback && !opts->ll_callback) - BUG("OPTION_CALLBACK needs one callback"); - if (opts->callback && opts->ll_callback) - BUG("OPTION_CALLBACK can't have two callbacks"); + optbug(opts, "OPTION_CALLBACK needs one callback"); + else if (opts->callback && opts->ll_callback) + optbug(opts, "OPTION_CALLBACK can't have two callbacks"); break; case OPTION_LOWLEVEL_CALLBACK: if (!opts->ll_callback) - BUG("OPTION_LOWLEVEL_CALLBACK needs a callback"); + optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs a callback"); if (opts->callback) - BUG("OPTION_LOWLEVEL_CALLBACK needs no high level callback"); + optbug(opts, "OPTION_LOWLEVEL_CALLBACK needs no high level callback"); break; case OPTION_ALIAS: - BUG("OPT_ALIAS() should not remain at this point. " - "Are you using parse_options_step() directly?\n" - "That case is not supported yet."); + optbug(opts, "OPT_ALIAS() should not remain at this point. " + "Are you using parse_options_step() directly?\n" + "That case is not supported yet."); + break; default: ; /* ok. (usually accepts an argument) */ } From patchwork Thu Jun 2 12:25:36 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: 12867819 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 B5BA7C43334 for ; Thu, 2 Jun 2022 12:25:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234971AbiFBMZ6 (ORCPT ); Thu, 2 Jun 2022 08:25:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234894AbiFBMZr (ORCPT ); Thu, 2 Jun 2022 08:25:47 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F2AB13274F for ; Thu, 2 Jun 2022 05:25:46 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id q7so6260606wrg.5 for ; Thu, 02 Jun 2022 05:25: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=WhyCczOzNCyndWdhBfvIb3qUffK7mzN60WVMVJTld0k=; b=dHOgnsbWr1M8AHf/LgEhdAXG9Veu/HGVJBgYe/E+3G/N7cwV4EvLeS37LLjlhnrvGm x+aH7bExBIU92iOFAwblWx4f4DV7uvrzxRb7p678+gUXvAU+VETIXOzTPCArvmzsfNON w0o3onztUgVHl1xUD9uVixXrAJowCLJFZV8eoz6sAl8Dgq4BxJwKa9YVzqvFp+Yq+Mtx ILfI93fwjuHVIFihHhhv1IX+HrDoOKxwrqFgIfr1Pmp4F6NWEy6TFJn4NNcDuVU6FUv+ snyCQ97VuYG0ZeMjasYhR+93UJ0fWbbDT+Fg0mi0PFGdHeMG5BOlSqPafJ+9miLECoTm xWuA== 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=WhyCczOzNCyndWdhBfvIb3qUffK7mzN60WVMVJTld0k=; b=4nZYwbdJsPiaOMWtu0EaYi+SDf1DBlYn2SOyToIX//pborXl/NCeb7ca6AY5Qs4m1C aJLZ9IBX84ZIktOz2DoLr0UxaFBNRb9g61Uff9yGEHKNy70mP6z8sqCvey+wfu+OH9/i KivdLEYuKRGExiZU7HrV+EoVmOQSG78qIUtKs3v8M6J/RfipevO2Hx4cJcrmgURAIzrz J51k36OoXcTWPg9cccfxlbv+o8zhylUM5PD0DqJawqtPPQtP5R8r2Kr/x0sb6aTNYYeV S8JX3X9aGo1nyW8QFXgcITL6ssErlIY2i0p6uFjUFtkKevDt6UgyVwpeClIEPa5muw0o SHyg== X-Gm-Message-State: AOAM531RFJvqkC5Y4VVZaEJoqraXSvoeiETw5CvTVFKqw+zVexnCicuq 4eV1eis3+Vhh/TzTlkwvHtsQFqa+IigMsg== X-Google-Smtp-Source: ABdhPJxje6Z2g5DfV6b+3FEwnmXQNxKCryKickeelrYJ7r5/uVIOl8O8+9HZNNPVcsuLFl0zj39XPg== X-Received: by 2002:a5d:598e:0:b0:20f:c2fd:de44 with SMTP id n14-20020a5d598e000000b0020fc2fdde44mr3507779wri.178.1654172745831; Thu, 02 Jun 2022 05:25:45 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:45 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 5/6] receive-pack: use bug() and BUG_if_bug() Date: Thu, 2 Jun 2022 14:25:36 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Amend code added in a6a84319686 (receive-pack.c: shorten the execute_commands loop over all commands, 2015-01-07) and amended to hard die in b6a4788586d (receive-pack.c: die instead of error in case of possible future bug, 2015-01-07) to use the new bug() function instead. Let's also rename the warn_if_*() function that code is in to BUG_if_*(), its name became outdated in b6a4788586d. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/receive-pack.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index daa153d0446..4a62327dee9 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1810,21 +1810,17 @@ static int should_process_cmd(struct command *cmd) return !cmd->error_string && !cmd->skip_update; } -static void warn_if_skipped_connectivity_check(struct command *commands, +static void BUG_if_skipped_connectivity_check(struct command *commands, struct shallow_info *si) { struct command *cmd; - int checked_connectivity = 1; for (cmd = commands; cmd; cmd = cmd->next) { - if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) { - error("BUG: connectivity check has not been run on ref %s", - cmd->ref_name); - checked_connectivity = 0; - } + if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) + bug("connectivity check has not been run on ref %s", + cmd->ref_name); } - if (!checked_connectivity) - BUG("connectivity check skipped???"); + BUG_if_bug("connectivity check skipped???"); } static void execute_commands_non_atomic(struct command *commands, @@ -2005,7 +2001,7 @@ static void execute_commands(struct command *commands, execute_commands_non_atomic(commands, si); if (shallow_update) - warn_if_skipped_connectivity_check(commands, si); + BUG_if_skipped_connectivity_check(commands, si); } static struct command **queue_command(struct command **tail, From patchwork Thu Jun 2 12:25:37 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: 12867821 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 DC799C43334 for ; Thu, 2 Jun 2022 12:26:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234984AbiFBM0C (ORCPT ); Thu, 2 Jun 2022 08:26:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234988AbiFBMZ5 (ORCPT ); Thu, 2 Jun 2022 08:25:57 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0C6B1EEBAF for ; Thu, 2 Jun 2022 05:25:48 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id h5so6298155wrb.0 for ; Thu, 02 Jun 2022 05:25: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=HI0hPeJ9aPK59FN3Uwdr6A54KEAUuPB/OxxJItpI0sk=; b=UcMH0gyYA4dLMjStm2BQuCGI4SiqzR2A5SdrgG6sN61qj2099uB4nB/gELOT8qIt29 SPj1XLHeYMnq39YxpLD5aojyh8UlFaAUVieKRMNhNC8sfi7rfT9pe9oR7CadPrEX3mN+ gBtA5ak+QAFA5nlw6xRcIrBvuPKtXFwJEWVc7mS0HpDGijhZmbo08IqLBwu0rkwRHjsp xPY9Zjwtb6j9Bxk4gFFzCL1wcXeCPplhke+uhTMPVTEw+4WjIrsVsHvSU95/P5qCI7Vs 67rcthRBlwOMjRBvU1gOhqy+iWdGWLv/5ZuchzanjYJbXenFrisPy7e5tlXhOjFWsfql fo2A== 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=HI0hPeJ9aPK59FN3Uwdr6A54KEAUuPB/OxxJItpI0sk=; b=OXed08leTWBBo7dkFbAbkCGGUXUD4sSkpwk8MdjyYRTZsqdkYzbUzqcjmjaR/D3kjt V0fcczT1sYQMhLMtSKpzQFkIuGMHYiWxUxKpZ+KIGEEZPBO9mRYy2KmF/hGB5RdKxET9 he+RJlzBt8AL3p6qhQY8tyvQ2Nd7HCaKTgQVeNbA0GHY4j1NEZ5Bt4nu1EWfTooMX4lv SG/KZutxS6QJxu/3NWZlUzRirdIbuSbe+q1JBVExSwz+RBBneNCBBJT8fbX9G2P6UjpH x5SuzS266ErUaowoJWTpZXH8oSLV2nivOS/aNR85UkghMVCkzlYhhoLiA/IJVT6b+YpB YVhw== X-Gm-Message-State: AOAM5336c1+rqURQn4aeW0PJInVB0fjsV9gLbOVw70CcI4/BvTHwCyMP ehQa57Q+JzPnexn78aPdmVo0iFpvMN4yGA== X-Google-Smtp-Source: ABdhPJwo3hDibqSzUM1ieQSAF/D0JSCJ0pg6gY5+qkRDVdUboiFXH9r1HIGlpnL0G/kqkJj2iVx0Bg== X-Received: by 2002:a05:6000:1c2:b0:213:3b79:bd7 with SMTP id t2-20020a05600001c200b002133b790bd7mr2031971wrx.634.1654172746853; Thu, 02 Jun 2022 05:25:46 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id l6-20020a5d4bc6000000b0020e615bab7bsm4305100wrt.7.2022.06.02.05.25.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 05:25:46 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Bagas Sanjaya , Abhradeep Chakraborty , Josh Steadmon , Glen Choo , Andrei Rybak , Emily Shaffer , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= Subject: [PATCH v3 6/6] cache-tree.c: use bug() and BUG_if_bug() Date: Thu, 2 Jun 2022 14:25:37 +0200 Message-Id: X-Mailer: git-send-email 2.36.1.1103.gb3ecdfb3e6a In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change "BUG" output originally added in a97e4075a16 (Keep rename/rename conflicts of intermediate merges while doing recursive merge, 2007-03-31), and later made to say it was a "BUG" in 19c6a4f8369 (merge-recursive: do not return NULL only to cause segfault, 2010-01-21) to use the new bug() function. This gets the same job done with slightly less code, as we won't need to prefix lines with "BUG: ". More importantly we'll now log the full set of messages via trace2, before this we'd only log the one BUG() invocation. We don't replace the last "BUG()" invocation with "BUG_if_bug()", as in this case we're sure that we called bug() earlier, so there's no need to make it a conditional. While we're at it let's replace "There" with "there" in the message, i.e. not start a message with a capital letter, per the CodingGuidelines. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache-tree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cache-tree.c b/cache-tree.c index 6752f69d515..63953bf7772 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -692,12 +692,12 @@ struct tree* write_in_core_index_as_tree(struct repository *repo) { ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL); if (ret == WRITE_TREE_UNMERGED_INDEX) { int i; - fprintf(stderr, "BUG: There are unmerged index entries:\n"); + bug("there are unmerged index entries:"); for (i = 0; i < index_state->cache_nr; i++) { const struct cache_entry *ce = index_state->cache[i]; if (ce_stage(ce)) - fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce), - (int)ce_namelen(ce), ce->name); + bug("%d %.*s", ce_stage(ce), + (int)ce_namelen(ce), ce->name); } BUG("unmerged index entries when writing inmemory index"); }